您好,登錄后才能下訂單哦!
#!/bin/bash # #Time : 2014-06-23 #Author : ftlynx #Function : use NET-SNMP get NIC traffic on nagios. Usage(){ echo "Usage: check_traffic.sh [options]" echo " -H Host IP." echo " -P net-snmp community string." echo " -N NIC desc." echo " -W nagios warning value. Format: 200,300. 200 is in traffic. 300 is out traffic. unit:Kb. Default: 5000,5000" echo " -C nagios crit value. Reference -W. Default: 10000,10000" echo " -V net-snmp version. Default 2c." exit 2 } DefaultValue(){ if [ -z "$IP" -o -z "$nicdesc" -o -z "$community" ];then echo -e "Error:Parameter not enough.\n" Usage fi if [ -z "$warn" ];then warn="5000,5000" fi if [ -z "$crit" ];then crit="10000,10000" fi if [ -z "$version" ];then version=2c fi } GetResult(){ while [ 1 ] do index=`snmpwalk -v $version -c $community $IP IF-MIB::ifDescr | grep "${nicdesc}$" |awk -F '.' '{print $2}' |awk '{print $1}'` if [ $? -ne 0 ];then echo "Error: snmpwalk wrong." exit 2 fi if [ -z "$index" ];then continue else break fi done tempfile="/tmp/traffic.${IP}-$index" while [ 1 ] do if [ -f "$tempfile" ];then value=`cat $tempfile` last_time=`echo "$value" | awk '{print $1}'` last_in_traffic=`echo "$value" |awk '{print $2}'` last_out_traffic=`echo "$value" |awk '{print $3}'` now_time=`date +%s` now_in_traffic=`snmpwalk -v $version -c $community $IP IF-MIB::ifInOctets.${index} |awk '{print $NF}'` now_out_traffic=`snmpwalk -v $version -c $community $IP IF-MIB::ifOutOctets.${index} |awk '{print $NF}'` if [ -z "$now_in_traffic" -o -z "$now_out_traffic" ];then sleep 10 continue fi in_traffic=$(($now_in_traffic - $last_in_traffic)) out_traffic=$(($now_out_traffic - $last_out_traffic)) second=$(($now_time - $last_time)) else now_time=`date +%s` now_in_traffic=`snmpwalk -v $version -c $community $IP IF-MIB::ifInOctets.${index} |awk '{print $NF}'` now_out_traffic=`snmpwalk -v $version -c $community $IP IF-MIB::ifOutOctets.${index} |awk '{print $NF}'` if [ -z "$now_in_traffic" -o -z "$now_out_traffic" ];then sleep 10 continue fi in_traffic=0 out_traffic=0 fi echo "$now_time $now_in_traffic $now_out_traffic" > $tempfile if [ $? -ne 0 ];then echo "$tempfile write fail." exit 2 fi if [ $in_traffic -le 0 -o $out_traffic -le 0 ];then sleep 10 continue else in_result=$(($in_traffic / $second / 1024 * 8)) out_result=$(($out_traffic / $second / 1024 * 8)) break fi done #warn vaule in_warn=`echo $warn |awk -F ',' '{print $1}'` out_warn=`echo $warn |awk -F ',' '{print $2}'` #crit value in_crit=`echo $crit | awk -F ',' '{print $1}'` out_crit=`echo $crit | awk -F ',' '{print $2}'` echo "IN: ${in_result}Kbps[${in_warn}Kbps][${in_crit}Kbps] OUT: ${out_result}Kbps[${out_warn}Kbps][${out_crit}Kbps] | IN=${in_result}Kb; OUT=${out_result}Kb;" if [ $in_result -ge $in_crit -o $out_result -ge $out_crit ];then exit 2 fi if [ $in_result -ge $in_warn -o $out_result -ge $out_warn ];then exit 1 fi exit 0 } while getopts H:P:N:W:C:V: args do case $args in H) IP="$OPTARG" ;; P) community="$OPTARG" ;; W) warn="$OPTARG" ;; C) crit="$OPTARG" ;; V) version="$OPTARG" ;; N) nicdesc="$OPTARG" ;; ?) Usage esac done DefaultValue GetResult
需要注意的地方:
使用IF-MIB::ifDescr,使用網卡名稱來獲取網卡對應的索引值,然后使用IF-MIB::ifInOctets.{index}和IF-MIB::ifOutOctets.{index}來獲取網卡的進出流量。
使用snmpwalk -v 2c -c public 127.0.0.1 IF-MIB::ifDescr 來查看有哪些接口,找到自己對應的。
要保證使用snmpwalk能抓取到數據。所以腳本中使用死循環來判斷。
由于使用的snmp的32位計數器,所以當達到最大值時,計數器會從頭開始。這樣取兩次間隔時會出現負數,所以腳本中有判斷兩次間隔的值一定要大于0。同時使用sleep 10來延遲10秒再取值,同時 這個間隔時間最好不要小于10s,因為使用snmpwalk抓取數據時,間隔太小會導致抓取到的值是一樣的。
臨時文件的權限,如果先手動運行就會就root用戶,導致放在naigos中的時候不能寫臨時文件。
被監控機要安裝snmp服務。
本腳本實用于linux 和windows。同時滿足pnp4nagios的繪圖。當然要自己定義模板(見下面)
本人使用:
定義nagiso命令:
define command{ command_name check_traffic command_line $USER1$/check_traffic.sh -H $HOSTADDRESS$ -P $ARG1$ -W $ARG2$ -C $ARG3$ -N $ARG4$ #ARG1 is snmp-community; ARG2 is warn; ARG3 is crit; ARG4 is NIC Name }
定義nagios 服務:
windows 的
define service{ use XXX host_name XXX service_description wan_traffic check_command check_traffic!public!5000,5000!9000,9000!"Net Device PV Driver #2" }
linux的
define service{ use XXX host_name XXX service_description wan_traffic check_command check_traffic!public!10000,12000!14000,14000!eth2 }
pnp4nagios的模板
[root@nagios pnp4nagios]# cat share/templates/check_traffic.php <?php # ## Copyright (c) 2006-2010 Joerg Linge (http://www.pnp4nagios.org) ## Plugin: check_iftraffic.pl (COUNTER) # $opt[1] = "--vertical-label \"Traffic\" -b 1024 --title \"Interface Traffic for $hostname / $servicedesc\" "; $def[1] = "DEF:var1=$RRDFILE[1]:$DS[1]:AVERAGE " ; $def[1] .= "DEF:var2=$RRDFILE[2]:$DS[2]:AVERAGE " ; $def[1] .= "LINE1:var1#003300:\"in\" " ; $def[1] .= "GPRINT:var1:LAST:\"%7.2lf %SKb/s last\" " ; $def[1] .= "GPRINT:var1:AVERAGE:\"%7.2lf %SKb/s avg\" " ; $def[1] .= "GPRINT:var1:MAX:\"%7.2lf %SKb/s max\\n\" " ; $def[1] .= "LINE1:var2#00ff00:\"out\" " ; $def[1] .= "GPRINT:var2:LAST:\"%7.2lf %SKb/s last\" " ; $def[1] .= "GPRINT:var2:AVERAGE:\"%7.2lf %SKb/s avg\" " ; $def[1] .= "GPRINT:var2:MAX:\"%7.2lf %SKb/s max\" " ?>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。