#!/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,使用網(wǎ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 <?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\" " ?>
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站題目:nagios監(jiān)控網(wǎng)卡流量腳本-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://jinyejixie.com/article6/depdig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、關(guān)鍵詞優(yōu)化、網(wǎng)站內(nèi)鏈、手機(jī)網(wǎng)站建設(shè)、ChatGPT、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容