监控机:192.168.10.133
被监控机:192.168.10.107
系统:centos 5.5_x64
1、在监控端安装和测试snmp
参考:http://151wqooo.blog.51cto.com/2610898/1176730
2、获取被监控端的网卡信息(用于脚本中的-I参数)
[root@wqk1 mnt]# ./check_traffic.sh -V 2c -C public -H 192.168.10.107 -L
List Interface for host 192.168.10.107.
Interface index 1 orresponding to lo
Interface index 2 orresponding to eth0 //使用这个网卡接口
Interface index 3 orresponding to sit0
3、在监控端和被监控端放置脚本
cd /usr/local/nagios/libexec
chmod +x /usr/local/nagios/libexec/check_traffic.sh
./check_traffic.sh -h
./check_traffic.sh -V 2c -C public -H 192.168.10.107 -I 2 -w 200,300 -c 400,500 -K –B 定义in和out值分别超过200K、300K警告,超过400K,500k严重警告。(第一次运行没有输出,30s后第二次运行才有输出;-I后面的2对应 上面获得的被监控端的网卡信息)
OK - It's the first time for this plugins run. We'll get the data from the next time.
第一次执行,history data file(/var/tmp/check_traffic_${Host}_${Interface}.hist_dat)不存在,因此会由此提示,可以忽略,再执行一次可以正正常获取
4、在监控端command文件里面定义check_traffic命令
# 'check_traffic' command definition
define command{
command_name check_traffic
command_line $USER1$/check_traffic.sh -V 2c -C public -H $HOSTADDRESS$ -I $ARG1$ -w $ARG2$ -c $ARG3$ -K -B 监控单位可以自定义
}
5、在监控端localhost.cfg中定义主机
vim /usr/local/nagios/etc/objects/localhost.cfg
define host {
host_name wqk_centos-107
alias centos-107
address 192.168.10.107
check_command check-host-alive
notification_options d,u,r
check_interval 1
max_check_attempts 2
contact_groups admins
notification_interval 10
notification_period 24x7
}
6、在监控端localhost.cfg中定义服务
define service {
service_description check_traffic
check_period 24x7
normal_check_interval 2
retry_check_interval 1
max_check_attempts 5
notification_options w,u,c,r
check_command check_nrpe!check_traffic //与command文件中的名字一样
7、检查测试
cd /usr/local/nagios/libexec
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
8、重启服务
service nagios restart
******************
1、在被监控端安装和测试snmp
2、插件
chmod +x /usr/local/nagios/libexec/check_traffic.sh
3、定义命令
编辑nrpe配置文件:
vim /usr/local/nagios/etc/nrpe.cfg
添加:
command[check_traffic]=/usr/local/nagios/libexec/check_traffic.sh -V 2c -C public -H 127.0.0.1 -I 2 -w 200,300 -c 400,500 -K –B
4、重启服务
ps aux | grep nrpe
kill掉nrpe 进程
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d //启动nrpe服务
5、远程测试
在监控端
./check_nrpe -H 192.168.10.107 -c check_traffic
The check interval must greater than 30 Seconds. But now it's 1. Please retry it later.
[root@wqk1 libexec]# ./check_nrpe -H 192.168.10.107 -c check_traffic
OK - The Traffic In is 0.0KB, Out is 0.0KB, Total is 0.0KB. The Check Interval is 60s |In=0.0KB;200;400;0;0 Out=0.0KB;300;500;0;0 Total=0.0KB;500;900;0;0 Interval=60s;1200;1800;0;0
第一次运行没有输出,30s后第二次运行才有输出
[root@wqk1 mnt]# ./check_traffic.sh -V 2c -C public -H 192.168.10.107 -I 2 -w 200,300 -c 400,500 -K –B
OK - The Traffic In is 1Kbps, Out is 0.0Kbps, Total is 1Kbps. The Check Interval is 1165s |In=1Kbps;200;400;0;0 Out=0.0Kbps;300;500;0;0 Total=1Kbps;500;900;0;0 Interval=1165s;1200;1800;0;0
[root@wqk1 mnt]# ./check_traffic.sh -V 2c -C public -H 192.168.10.107 -I 2 -w 0,0 -c 400,500 -K –B
Warning - The Traffic In is 0.0Kbps, Out is 0.0Kbps, Total is 0.0Kbps. The Check Interval is 54s |In=0.0Kbps;0;400;0;0 Out=0.0Kbps;0;500;0;0 Total=0.0Kbps;0;900;0;0 Interval=54s;1200;1800;0;0
[root@wqk1 mnt]# ./check_traffic.sh -V 2c -C public -H 192.168.10.107 -I 2 -w 300,400 -c 400,500 -K –B
OK - The Traffic In is 0.0Kbps, Out is 0.0Kbps, Total is 0.0Kbps. The Check Interval is 68s |In=0.0Kbps;300;400;0;0 Out=0.0Kbps;400;500;0;0 Total=0.0Kbps;700;900;0;0 Interval=68s;1200;1800;0;0
[root@wqk1 mnt]# ./check_traffic.sh -V 2c -C public -H 192.168.10.107 -I 2 -w 500,600 -c 800,900 -K –B
OK - The Traffic In is 0.0Kbps, Out is 0.0Kbps, Total is 0.0Kbps. The Check Interval is 68s |In=0.0Kbps;500;800;0;0 Out=0.0Kbps;600;900;0;0 Total=0.0Kbps;1100;1700;0;0 Interval=68s;1200;1800;0;0
对比红色数字字体——为监控设置的阀值,后面为实际流量值,这就是监控点。
==================
执行命令报错:
./check_nrpe -H 192.168.10.249 -c check_traffic -t 30
Unknown - Read or Write File /var/tmp/check_traffic_127.0.0.1_2.hist_dat_root__64 Error with user uid=500(nagios) gid=500(nagios) groups=500(nagios) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023.
解决:
是因为以非nagios用户身份,手动测试执行过该脚本(也就是command[check_traffic]=/usr/local/nagios/libexec/check_traffic.sh -V 2c -C public -H ***.***.***.*** -I 2 -w 200,300 -c 400,500 -K -B这个操作),请在正式使用该脚本前,删除被监控端/var/tmp下对应测试生成的/var/tmp/check_traffic_${Host}_${Interface}.hist_dat文件,否则会造成nagios用户无法读写该文件的错误
附上使用的脚本: