公司机房新增了一台H3C S5024P交换机做为内网之间机器互通用 。其本身支持通过web查看,不过由于对该机没有配置公网IP ,同时每次都登录查看感觉很不方便。所认就想到利用nagios 和snmp来进行监控。可以进行snmp监控的nagios插件很多,不过也可以自己根据snmp协议来写 。既然有现成的,就懒得写了,直接拿来“石头”的shell版的check_traffice.sh来监控交换机流量并配置告警。
一、安装
1、安装snmp包并配置snmpd.conf文件
yum -y install net-snmp*
<br />
直接一条命令就可以完成所需的snmp包的安装。接着修改/etc/snmpd.conf文件为如下内容:
# sec.name source community com2sec notConfigUser 127.0.0.1(localhost也可不改) XXXX(共同体名) # group context sec.model sec.level prefix read write notif access notConfigGroup "" any noauth exact mib2 none none #改system为mib2 view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc #去掉前面的"#"注释符
<br />
2、上传插件
上传check_traffic.sh至/App/nagios/libexec目录修改属主并赋予可执行权限。
chown nagios.nagios check_traffic.sh chmod +x check_traffic.sh
<br />
3、获取网卡index值
[root@web3 ~]# /App/nagios/libexec/check_traffic.sh -V 2c -C public -H 192.168.8.254 -L List Interface for host 192.168.8.254. Interface index 262 orresponding to InLoopBack0 Interface index 390 orresponding to Aux0/0 Interface index 518 orresponding to GigabitEthernet0/1 Interface index 646 orresponding to GigabitEthernet0/2 Interface index 774 orresponding to GigabitEthernet0/3 Interface index 902 orresponding to GigabitEthernet0/4 ………………………………………………………………………… Interface index 3334 orresponding to Copper0/23 Interface index 3462 orresponding to Copper0/24 Interface index 3590 orresponding to Fiber0/23 Interface index 3718 orresponding to Fiber0/24 Interface index 3846 orresponding to Vlan-interface1
再通过脚本检测下是否可以获取流量数据
/App/nagios/libexec/check_traffic.sh -V 2c -C public -H 192.168.8.254 -I 646 -w 1200,1500 -c 1700,1800 -K -b OK - The Traffic In is 356Kbps, Out is 414Kbps, Total is 770Kbps. The Check Interval is 30s |In=356Kbps;1200;1700;0;0 Out=414Kbps;1500;1800;0;0 Total=770Kbps;2700;3500;0;0 Interval=30s;1200;1800;0;0
<br />
注:-I 后面的为index值 ;-w后面的是分别是in和out的告警值 ,-c同样是in和out的严重告警值 ;后面的-k -b可以不加,其默认值是-k -b 。也可以改为 -M -b ,其是流量计数单位;该脚本原理是取两个时间段之间值的差,再和时间间隔相除 。其中会用到临时文件,其临时文件存话/var/tmp/目录,以check_traffic开头 。
二、nagios中定义
在command.cfg中增加如下配置:
define command{ command_name check_traffic # command_line $USER1$/check_traffic.sh -V 2c -C nagios -H $HOSTADDRESS$ -I $ARG1$ -w $ARG2$ -c $ARG3$ -K -b command_line $USER1$/check_traffic.sh -V 2c -C nagios -H $ARG4$ -I $ARG1$ -w $ARG2$ -c $ARG3$ -M -b -$ARG5$ }
<br />
在switch.cfg文件中增加如下配置:
define service{ use local-service,srv-pnp host_name SD-Intranet service_description host35 check_command check_traffic!646!200,100!300,200!192.168.8.254 notifications_enabled 1 normal_check_interval 5 retry_interval 1 }
<br />
三、交换配置
默认交换有一个public的共同体,只有读的权限,不过对于我们取SNMP消息完全够用了。如果想要写的权限,也可以新增其他共同体。如果想开启snmp trap,同样也可以在交换机上设置。具体如下图:
<a href="https://www.361way.com/wp-content/uploads/2013/06/switch.png"><img src="https://www.361way.com/wp-content/uploads/2013/06/switch.png" alt="switch" width="493" height="323" class="alignnone size-full wp-image-2580" /></a>
<a href="https://www.361way.com/wp-content/uploads/2013/06/switch2.png"><img src="https://www.361way.com/wp-content/uploads/2013/06/switch2.png" alt="switch2" width="594" height="257" class="alignnone size-full wp-image-2581" /></a>
四、重载nagios配置文件生效
1、检查文件是否有错
[root@nagios ~]/App/nagios/bin/nagios -v /App/nagios/etc/nagios.cfg
<br />
2、重载生效
[root@nagios ~]/etc/init.d/nagios reload
<br />
五、增加pnp4nagios模板
模板文件内容如下:
'#FF0000', 'green' => '#00FF00', 'blue' => '#0000FF', 'yellow' => '#FFFF00', 'black' => '#000000', 'deepred' => '#330000', ); $def[1] = "DEF:var1=$rrdfile:$DS[1]:AVERAGE " ; $def[1] .= "DEF:var2=$rrdfile:$DS[2]:AVERAGE " ; $def[1] .= "HRULE:$WARN[1]#FFFF00 "; $def[1] .= "HRULE:$CRIT[1]#FF0000 "; $def[1] .= "AREA:var1$colors[green]:"In " " ; $def[1] .= "GPRINT:var1:LAST:"%6.2lf last" " ; $def[1] .= "GPRINT:var1:AVERAGE:"%6.2lf avg" " ; $def[1] .= "GPRINT:var1:MAX:"%6.2lf max\n" "; $def[1] .= "LINE:var2$colors[blue]:"Out " " ; $def[1] .= "GPRINT:var2:LAST:"%6.2lf last" " ; $def[1] .= "GPRINT:var2:AVERAGE:"%6.2lf avg" " ; $def[1] .= "GPRINT:var2:MAX:"%6.2lf Total\n" " ; /* $def[1] .= "CDEF:total=var1,var2,+ " ; $def[1] .= "LINE1:total$colors[black]:"Total " " ; */ ?>
<br />
把以上模板命名成和command.cfg里定义的一样的文件,如我的是check_traffic.php放到/App/pnp4nagios/share/templates下 。最张效果图如下:
《nagios snmp流量监控之check_traffic.sh》有1条评论