1、centos6.2操作系统监控本机tcp连接数物理内存cpu使用率web并发数

查看本机tcp连接状态

netstat-n|awk'/^tcp/{++s[$NF]}END{for(ains)printa,s[a]}'

LISTEN:侦听来自远方的TCP端口的连接请求
SYN-SENT:再发送连接请求后等待匹配的连接请求
SYN-RECEIVED:再收到和发送一个连接请求后等待对方对连接请求的确认
ESTABLISHED:代表一个打开的连接
FIN-WAIT-1:等待远程TCP连接中断请求,或先前的连接中断请求的确认
FIN-WAIT-2:从远程TCP等待连接中断请求
CLOSE-WAIT:等待从本地用户发来的连接中断请求
CLOSING:等待远程TCP对连接中断的确认
LAST-ACK:等待原来的发向远程TCP的连接中断请求的确认
TIME-WAIT:等待足够的时间以确保远程TCP接收到连接中断请求的确认
CLOSED:没有任何连接状态
在nagios的插件库里面写下脚本

写脚本ip_cons
#!/bin/bash

#tcpconnet
if[$#!=2];then
echo"Usage:$0-wnum1-cnum2"
exit
fi
ip_conns=`netstat-n|grepESTABLISHED|wc-l`
if[$ip_conns-lt$1];then
echo"OK-connetcountsis$ip_conns"
exit0
fi
if[$ip_conns-ge$1-a$ip_conns-lt$2];then
echo"Warning-connetcountsis$ip_conns"
exit1
fi
if[$ip_conns-ge$2];then
echo"Critical-connetcountsis$ip_conns"
exit2
fi

修改配置文件commands.cfg

commands.cfg添加命令
definecommand{
command_nameip_cons
command_line/usr/local/nagios/libexec/ip_cons25(2是警告值5是临界值可自定义这里只是做实验)
}

之后修改本机的配置文件localhost.cfg

defineservice{
usegeneric-service
host_namelocalhost
service_descriptionip_cons
check_commandip_cons
notifications_enabled1
}

或者是写一个services.cfg文件里里面

defineservice{
host_namelocalhost
service_descriptionip_cons
check_commandip_cons
max_check_attempts5
normal_check_interval3
retry_check_interval2
check_period24x7
notification_interval10
notification_period24x7
notification_optionsw,u,c,r
contact_groupsadmins
}

写入services.cfg文件之后需要在nagios.cfg里面添加

cfg_file=/etc/nagios/objects/services.cfg

之后查看配置文件是否出错/usr/local/nagios/bin/nagios-v/usr/local/nagios/etc/nagios.cfg

没有出错的话就重新启动nagios服务/usr/local/nagios/bin/nagios-d/usr/local/nagios/etc/nagios.cfg

2、centos6.2操作系统监控其他主机tcp连接数物理内存cpu使用率web并发数

写监控脚本
#!/bin/bash
if[$#!=2];then
echo"Usage:$0-wnum1-cnum2"
exit
fi
ip_conns=`netstat-n|grepESTABLISHED|wc-l`
if[$ip_conns-lt$1];then
echo"OK-connetcountsis$ip_conns"
exit0
fi
if[$ip_conns-ge$1-a$ip_conns-lt$2];then
echo"Warning-connetcountsis$ip_conns"
exit1
fi
if[$ip_conns-ge$2];then
echo"Critical-connetcountsis$ip_conns"
exit2
fi
在nrpe.cfg里面填写监控命令
command[ip_cons]=/usr/local/nagios/libexec/ip_cons59
之后在监控机器上的配置文件里面配置
defineservice{
usegeneric-service
host_name192.168.122.3
service_descriptionip_cons
check_commandcheck_nrpe!ip_cons
notifications_enabled1
}

或者是写入services.cfg文件

defineservice{
host_name192.168.122.3
service_descriptionip_cons
check_commandcheck_nrpe!ip_cons
max_check_attempts5
normal_check_interval3
retry_check_interval2
check_period24x7
notification_interval10
notification_period24x7
notification_optionsw,u,c,r
contact_groupsadmins
}

之后重新启动nagios服务即可

内存监控的做法基本上和以上做法相同,想来要做这个监控的都是运维的同行,应该可以理解下面就只留下内存的监控脚本

#!/bin/bash
#memory

if[$#!=2];then
echo"Usage:$0-wnum1-cnum2"
exit
fi
total_mem=`free-m|grepMem|awk'{print$2}'`
free_mem=`free-m|grepMem|awk'{print$4}'`
used_mem=`free-m|grepMem|awk'{print$3}'`
if[$free_mem-gt$1];then
echo"OK-totalmemory$total_memMBused$used_memMBfree$free_memMB"
exit0
fi
if[$free_mem-ge$2-a$free_mem-le$1];then
echo"Warning-totalmemory$total_memMBused$used_memMBfree$free_memMB"
exit1
fi
if[$free_mem-lt$2];then
echo"Critical-totalmemory$total_memMBused$used_memMBfree$free_memMB"
exit2
fi

cpu使用率的监控跟上面tcp监控方法类似留下监控脚本一份

#!/bin/bash
if[$#!=2];then
echo"Usage:$0-wnum1-cnum2"
exit
fi
used_cpu=`vmstat|sed-n3p|awk-F""'{print$13}'`
if[$used_cpu-lt$1];then
echo"OK-used_cpuis$used_cpu%"
exit0
fi
if[$used_cpu-ge$1-a$used_cpu-lt$2];then
echo"Warning-used_cpuis$used_cpu%"
exit1
fi
if[$used_cpu-ge$2];then
echo"Critical-used_cpuis$used_cpu%"
exit2
fi

此shell脚本中这条命令used_cpu=`vmstat|sed-n3p|awk-F""'{print$13}'`是监控系统使用的cpu输出默认是整数要是想监控剩余cpu的话将以上shell脚本的此条命令修改成used_cpu=`vmstat|sed-n3p|awk-F""'{print$15}'`

web并发数如果是apache做的站点的话倒是可以直接监控httpd的进程数就可以获得并发数nginx的话就不行了可以使用一下脚本

web并发数
#!/bin/bash
#if[$#!=2];then
#echo"Usage:$0-wnum1-cnum2"
#exit
#fi
#w=2
#c=5
ip_conns=`netstat-n|grep'^tcp'|grepESTABLISHED|grep80|wc-l`
if[$ip_conns-lt$1];then
echo"OK-connetcountsis$ip_conns"
exit0
fi
if[$ip_conns-ge$1-a$ip_conns-lt$2];then
echo"Warning-connetcountsis$ip_conns"
exit1
fi
if[$ip_conns-ge$2];then
echo"Critical-connetcountsis$ip_conns"
exit2
fi

监控mysql主从状态
#!/bin/bash
io=`/usr/local/mysql/bin/mysql-uroot-pXXXX-e"showslavestatus\G"|grepSlave_IO_Running|awk'{print$2}'`
sql=`/usr/local/mysql/bin/mysql-uroot-pXXXX-e"showslavestatus\G"|grepSlave_SQL_Running|awk'{print$2}'`
if[[$io=Yes&&$sql=Yes]];then
echo"OK-mysql-replicationisrunning"
exit0
fi
if[[$io=No||$sql=No]];then
echo"Critical-mysql-replicationisnotworking,I/OorSQLisnotworkingproperly"
exit2
fi