#!/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 " ];thenecho -e " error:parameter not enough.\n "usagefiif [ -z "$warn" ];thenwarn= "5000,5000" fiif [ -z "$crit" ];thencrit= "10000,10000" Fiif [ -z "$version" ];thenversion=2cfi}getresult () {index= ' snmpwalk -v $version -c $community $IP IF-MIB::ifDescr | grep "${nicdesc}$" |awk -f '. ' ' {print $2} ' |awk ' {print $1} ' tempfile= '/tmp/traffic.${ip}-$index ' while [ 1 ]doif [ -f "$tempfile" ];thenvalue= ' 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} ' in_traffic=$ (($now _in_traffic - $last _in_traffic)) out_traffic=$ (( $now _out_traffic - $last _out_traffic)) second=$ (($now _time - $last _time)) in_result=$ (($in _ traffic / $second / 1024 * 8)) out_result=$ (($out _traffic / $second / 1024 * 8)) elsenow_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} ' in_result=0out_result=0fiecho ' $now _time $now _in_traffic $now _out_traffic " > $tempfileif [ $? -ne 0 ];thenecho "$tempfile write fail." exit 2fiif [ $in _result -le 0 -o $out _result -le 0 ]; Thensleep 10elsebreakfidone#warn vaulein_warn= ' echo $warn |awk -F ', ' ' { print $1} ' out_warn= ' echo $warn |awk -F ', ' ' {print $2} ' #crit valuein _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 $in _crit ]; Thenexit 2fiif [ $in _result -ge $in _warn -o $out _result -ge $out _ warn ];thenexit 1fiexit 0}while getopts h:p:n:w:c:v: argsdocase $args InH) ip= "$OPTARG";; P) community= "$OPTARG";; W) warn= "$OPTARG";; C) crit= "$OPTARG";; V) version= "$OPTARG";; N) nicdesc= "$OPTARG";;?) Usageesacdonedefaultvaluegetresult
This article is from the "Bamboo Yin" blog, please be sure to keep this source http://ftlynx.blog.51cto.com/2833447/1429865