#!/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
需要注意的地方:
創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站建設(shè)、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的恩施土家網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
使用IF-MIB::ifDescr,使用網(wǎng)卡名稱(chēng)來(lái)獲取網(wǎng)卡對(duì)應(yīng)的索引值,然后使用IF-MIB::ifInOctets.{index}和IF-MIB::ifOutOctets.{index}來(lái)獲取網(wǎng)卡的進(jìn)出流量。
使用snmpwalk -v 2c -c public 127.0.0.1 IF-MIB::ifDescr 來(lái)查看有哪些接口,找到自己對(duì)應(yīng)的。
要保證使用snmpwalk能抓取到數(shù)據(jù)。所以腳本中使用死循環(huán)來(lái)判斷。
由于使用的snmp的32位計(jì)數(shù)器,所以當(dāng)達(dá)到最大值時(shí),計(jì)數(shù)器會(huì)從頭開(kāi)始。這樣取兩次間隔時(shí)會(huì)出現(xiàn)負(fù)數(shù),所以腳本中有判斷兩次間隔的值一定要大于0。同時(shí)使用sleep 10來(lái)延遲10秒再取值,同時(shí) 這個(gè)間隔時(shí)間最好不要小于10s,因?yàn)槭褂胹nmpwalk抓取數(shù)據(jù)時(shí),間隔太小會(huì)導(dǎo)致抓取到的值是一樣的。
臨時(shí)文件的權(quán)限,如果先手動(dòng)運(yùn)行就會(huì)就root用戶,導(dǎo)致放在naigos中的時(shí)候不能寫(xiě)臨時(shí)文件。
被監(jiān)控機(jī)要安裝snmp服務(wù)。
本腳本實(shí)用于linux 和windows。同時(shí)滿足pnp4nagios的繪圖。當(dāng)然要自己定義模板(見(jiàn)下面)
本人使用:
定義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 服務(wù):
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