1、centos6.2 操作系统 监控本机tcp连接数 物理内存 查看本机tcp连接状态 netstat -n |awk '/^tcp/ {++s[$NF]} END {for (a in s) print a,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 #tcp connet
if [ $# != 2 ];then
echo "Usage:$0 -w num1 -c num2"
exit
fi
ip_conns=`netstat -n |grep ESTABLISHED|wc -l`
if [ $ip_conns -lt $1 ];then
echo "OK -connet counts is $ip_conns"
exit 0
fi
if [ $ip_conns -ge $1 -a $ip_conns -lt $2 ];then
echo "Warning -connet counts is $ip_conns"
exit 1
fi
if [ $ip_conns -ge $2 ];then
echo "Critical -connet counts is $ip_conns"
exit 2
fi 修改配置文件commands.cfg commands.cfg添加命令
define command{
command_name ip_cons
command_line /usr/lib64/nagios/plugins/ip_cons 2 5 (2 是警告值 5是临界值可自定义这里只是做实验)
} 之后修改本机的配置文件localhost.cfg define service{
use generic-service
host_name localhost
service_description ip_cons
check_command ip_cons
notifications_enabled 1
} 或者是写一个services.cfg文件里里面 define service{
host_name localhost
service_description ip_cons
check_command ip_cons
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups admins
} 写入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连接数 物理内存 写监控脚本
#!/bin/bash
if [ $# != 2 ];then
echo "Usage:$0 -w num1 -c num2"
exit
fi
ip_conns=`netstat -n |grep ESTABLISHED|wc -l`
if [ $ip_conns -lt $1 ];then
echo "OK -connet counts is $ip_conns"
exit 0
fi
if [ $ip_conns -ge $1 -a $ip_conns -lt $2 ];then
echo "Warning -connet counts is $ip_conns"
exit 1
fi
if [ $ip_conns -ge $2 ];then
echo "Critical -connet counts is $ip_conns"
exit 2
fi
在nrpe.cfg 里面填写监控命令
command[ip_cons]=/usr/lib64/nagios/plugins/ip_cons 5 9
之后在监控机器上的配置文件里面配置
define service{
use generic-service
host_name 192.168.122.3
service_description ip_cons
check_command check_nrpe!ip_cons
notifications_enabled 1
} 或者是写入services.cfg文件 define service{
host_name 192.168.122.3
service_description ip_cons
check_command check_nrpe!ip_cons
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups admins
} 之后重新启动nagios服务即可 内存监控的做法基本上和以上做法相同,想来要做这个监控的都是运维的同行,应该可以理解下面就只留下内存的监控脚本 #!/bin/bash
#memory if [ $# != 2 ];then
echo "Usage:$0 -w num1 -c num2"
exit
fi
total_mem=`free -m |grep Mem|awk '{print $2}'`
free_mem=`free -m |grep Mem|awk '{print $4}'`
used_mem=`free -m |grep Mem|awk '{print $3}'`
if [ $free_mem -gt $1 ];then
echo "OK - total memory $total_mem MB used $used_mem MB free $free_mem MB "
exit 0
fi
if [ $free_mem -ge $2 -a $free_mem -le $1 ];then
echo "Warning - total memory $total_mem MB used $used_mem MB free $free_mem MB"
exit 1
fi
if [ $free_mem -lt $2 ];then
echo "Critical - total memory $total_mem MB used $used_mem MB free $free_mem MB"
exit 2
fi
|