Monitoring plugin:
[root@RHEL5 ~]# tail -n 2 /etc/nagios/resource.cfg
#Add by Kingcraft $USER5$=/usr/local/nagios/customexec
[root@RHEL5 ~]# vi /usr/local/nagios/customexec/check_traffic.sh
#!/bin/sh # Author: zhai_kang,20131029 #snmpwalk -v 2c 172.17.27.1 -c VIP-B ifInOctets.10024 #check_traffic.sh -v 2c 172.17.27.1 -c VIP-B ifInOctets.10024 -W 400 -C 600 #unit:KB/s #Parameter VER=$2 IP=$3 COMM=$5 OID=$6 WARN=$8 CRIT=${10} #In flow(KB/s) In1=`snmpwalk -v $VER $IP -c $COMM $OID | awk '{print $NF}'` sleep 1 In2=`snmpwalk -v $VER $IP -c $COMM $OID | awk '{print $NF}'` if [[ $In1 -gt $In2 ]] then Ina=$(($In1 - $In2)) else Ina=$(($In2 - $In1)) fi if [[ $Ina -ge 1024 ]] then In="`echo "scale=3;$Ina/1024" | bc`" elif [[ `echo 1.024 $Ina | awk '{if($1<=$2)print 1;else print 0}'` -eq 1 ]] then In="0`echo "scale=3;$Ina/1024" | bc`" else In=0 fi #Obtain Return Data SIGN_C=`echo $CRIT $In | awk '{if($1<=$2)print 1;else print 0}'` SIGN_W=`echo $WARN $In | awk '{if($1<=$2)print 1;else print 0}'` SIGN_O=`echo 0 $In | awk '{if($1<=$2)print 1;else print 0}'` if [[ $SIGN_C -eq 1 ]] then line=`echo "SNMP Critical : In = $In KB/s"` RC=2 elif [[ $SIGN_W -eq 1 ]] then line=`echo "SNMP Warning : In = $In KB/s"` RC=1 elif [[ $SIGN_O -eq 1 ]] then line=`echo "SNMP OK : In = $In KB/s"` RC=0 else line="SNMP Unknown" RC=3 fi echo $line \| Interface_Traffic="$In;$WARN;$CRIT" exit $RC
Custom drawing templates:
[root@RHEL5 ~]# vi /usr/local/pnp4nagios/share/templates/check_traffic.php
<?php #-----The Color of The Three Primary Colors----- # red #FF0000 # green #00FF00 # blue #0000FF # yellow #FFFF00 # black #000000 # deepred #330000 #------------------Image Title------------------ $opt[1] = "--title \"Interface traffic For $hostname / $servicedesc\" "; #-------------Interface Traffic------------ $def[1] = "DEF:var1=$RRDFILE[1]:$DS[1]:AVERAGE " ; #$def[1] .= "HRULE:$WARN[1]#FFFF11 "; $def[1] .= "LINE1:var1#FF0000:\"In \" " ; $def[1] .= "GPRINT:var1:MIN:\"%3.4lg KB/s MIN \\t\" "; $def[1] .= "GPRINT:var1:MAX:\"%3.4lg KB/s MAX \\t\" "; $def[1] .= "GPRINT:var1:AVERAGE:\"%3.4lg KB/s AVERAGE \\n\" "; #-------------5 Minutes Average Load----------- #$def[1] .= "DEF:var2=$RRDFILE[2]:$DS[2]:AVERAGE " ; #$def[1] .= "HRULE:$WARN[1]#FFFF11 "; #$def[1] .= "LINE2:var2#00FF00:\"5-Mins \" " ; #$def[1] .= "GPRINT:var2:MIN:\"%3.4lg %s$UNIT[1] MIN \\t\" "; #$def[1] .= "GPRINT:var2:MAX:\"%3.4lg %s$UNIT[1] MAX \\t\" "; #$def[1] .= "GPRINT:var2:Average:\"%3.4lg %s$UNIT[1] AVERAGE \\n\" "; #------------15 Minutes Average Load----------- #$def[1] .= "DEF:var3=$RRDFILE[3]:$DS[3]:AVERAGE " ; #$def[1] .= "LINE3:var3#000000:\"15-Mins \" " ; #$def[1] .= "GPRINT:var3:MIN:\"%3.4lg %s$UNIT[1] MIN \\t\" "; #$def[1] .= "GPRINT:var3:MAX:\"%3.4lg %s$UNIT[1] MAX \\t\" "; #$def[1] .= "GPRINT:var3:AVERAGE:\"%3.4lg %s$UNIT[1] AVERAGE \\n\" "; ?>
Define the monitoring command:
[root@RHEL5 ~]# vi /etc/nagios/objects/commands.cfg
define command{ command_name check_traffic command_line $USER5$/check_traffic.sh -v $ARG1$ $HOSTADDRESS$ -c $ARG2$ $ARG3$ -W $ARG4$ -C $ARG5$ }
[root@RHEL5 ~]# vi /etc/nagios/objects/templates.cfg
define host{ name host-pnp register 0 action_url /pnp4nagios/graph?host=$HOSTNAME$ } define service{ name srv-pnp register 0 action_url /pnp4nagios/graph?host=$HOSTNAME$&srv=$SERVICEDESC$ }
Definition monitor file:
[root@RHEL5 ~]# vi /etc/nagios/Cisco/HYQH-SH.cfg
define host{ use generic-switch,host-pnp host_name HYQH.SH alias HYQH.SH address 172.17.21.250 } define service{ use generic-Flows,srv-pnp host_name HYQH.SH service_description Fa 0/0 check_command check_traffic!2c!SH!ifInOctets.1!10240!20480 }
Restart:
[root@RHEL5 ~]# service nagios restart
转载于:https://blog.51cto.com/kingcraft/1320651