linf 发表于 2013-5-31 08:43:26

nagios 监控tcp连接数 物理内存

1、centos6.2 操作系统 监控本机tcp连接数 物理内存查看本机tcp连接状态 netstat -n |awk '/^tcp/ {++s[$NF]} END {for (a in s) print a,s}'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.cfgcommands.cfg添加命令
define command{
command_name ip_cons
command_line /usr/lib64/nagios/plugins/ip_cons 2 5 (2 是警告值 5是临界值可自定义这里只是做实验)
}之后修改本机的配置文件localhost.cfgdefine service{
      use                           generic-service
      host_name                     localhost
      service_description             ip_cons
      check_command                   ip_cons
      notifications_enabled         1
      }或者是写一个services.cfg文件里里面define service{
      host_namelocalhost
      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.cfg2、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=/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_name192.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 MBused $used_mem MB free $free_mem MB"
exit 2
fi

hncys 发表于 2013-5-31 08:52:56

长得真有创意,活得真有勇气!

benzhou 发表于 2013-5-31 09:18:44

丑,但是丑的特别,也就是特别的丑!

obeckham 发表于 2013-5-31 09:30:08

如果回帖是一种美德,那我早就成为圣人了!

搜鞥都哦 发表于 2013-5-31 09:41:27

人生自古谁无死,啊个拉屎不用纸!

samdung 发表于 2013-5-31 12:18:57

精典之极就是精斑!!!

mrbear 发表于 2013-5-31 13:16:33

走过了年少,脚起了水泡
页: [1]
查看完整版本: nagios 监控tcp连接数 物理内存