Last active
August 29, 2015 14:19
-
-
Save hafthanhf/5aa916e72a5514f6764d to your computer and use it in GitHub Desktop.
check_bandwidth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#Ussage: Check input bandwidth on each switch port | |
########## ./check_bw $IP_of_switch $port_No_with_slash $max_threshold $min_threshold##### | |
########## Version 2.0 ######################################## | |
################################################################ | |
########### Created by Thanh.ha ############################## | |
######## ########################### | |
############################################################### | |
############################################################### | |
print_usage() { | |
echo "Usage: ./check_bw -H Address -p port_number - I value_in_Kbps -i value_in_Kbps -O value_in_Kbps -o value_in_Kbps" | |
echo " ./$SCRIPTNAME -H ADDRESS" | |
echo " ./$SCRIPTNAME -p STRING" | |
echo " ./$SCRIPTNAME -I STRING" | |
echo " ./$SCRIPTNAME -i INTEGER" | |
echo " ./$SCRIPTNAME -O INTEGER" | |
echo " ./$SCRIPTNAME -o INTEGER" | |
echo " ./$SCRIPTNAME -h" | |
echo " ./$SCRIPTNAME -V" | |
echo "Example: ./check_bw -H 10.255.0.251 -p g0/2 -I 100000 -i 100 -O 100000 -o 100" | |
} | |
print_version() { | |
echo "$SCRIPTNAME" version "$VERSION" | |
echo "********* NOTE *********************************" | |
echo "This plugin is ONLY used with cisco 2960 devices" | |
echo "This nagios plugins comes with ABSOLUTELY NO WARRANTY." | |
echo "You may redistribute copies of the plugins under the terms of the GNU General Public License v2." | |
echo "Written by hafthanhf" | |
} | |
print_help() { | |
print_version | |
echo "" | |
print_usage | |
} | |
# Plugin return statements | |
NORMAL=0 | |
WARNING=1 | |
CRITICAL=2 | |
UNKNOWN=3 | |
#OID_CPUIDLE=".1.3.6.1.4.1.2021.11.11.0" | |
#OID_TOTALMEMORY=".1.3.6.1.4.1.2021.4.5.0" | |
#OID_FREEMEMORY=".1.3.6.1.4.1.2021.4.11.0" | |
OID_sysDescr=SNMPv2-MIB::sysDescr.0 | |
OID_inrate_junos="1.3.6.1.4.1.2636.3.3.1.1.1" | |
OID_outrate_junos="1.3.6.1.4.1.2636.3.3.1.1.4" | |
# Default variables | |
VERSION=2.0 | |
DESCRIPTION="Unknown" | |
STATE="$STATE_UNKNOWN" | |
STATE_MESSAGE="UNKNOWN" | |
# Default options | |
COMMUNITY="********" | |
HOSTNAME="127.0.0.1" | |
#VERSION="2c" | |
#WARNING=0 | |
#CRITICAL=0 | |
PATH1=/usr/local/nagios/libexec/ | |
while getopts H:p:I:i:O:o:hV OPT | |
do | |
case "$OPT" in | |
H) HOSTNAME="$OPTARG" ;; | |
p) PORTNo="$OPTARG" ;; | |
I) MAXinTH="$OPTARG" ;; | |
i) MINinTH="$OPTARG" ;; | |
O) MAXoutTH="$OPTARG" ;; | |
o) MINoutTH="$OPTARG" ;; | |
h) print_help | |
exit "$UNKNOWN" ;; | |
V) print_version | |
exit "$UNKNOWN" ;; | |
esac | |
done | |
#Start to processing | |
################# CISCO INFO ############ | |
########################################### | |
getvalue_cisco () { | |
#Get input rate | |
bw_in=`/usr/bin/expect $PATH1/get_bw_value $HOSTNAME $PORTNo | grep "input rate" | awk '{print $5}'` | |
#Get Ouput rate | |
bw_out=`/usr/bin/expect $PATH1/get_bw_value $HOSTNAME $PORTNo | grep "output rate" | awk '{print $5}'` | |
bw_in=`expr $bw_in / 1000 ` | |
bw_out=`expr $bw_out / 1000 ` | |
} | |
################# QUIDWAY INFO ############ | |
########################################### | |
getvalue_quidway () { | |
#Get input rate | |
bw_in=`/usr/bin/expect $PATH1/get_huawei_info $HOSTNAME $PORTNo | grep "input rate" | awk '{print $6}'` | |
#Get Ouput rate | |
bw_out=`/usr/bin/expect $PATH1/get_huawei_info $HOSTNAME $PORTNo | grep "output rate" | awk '{print $6}'` | |
bw_in=`expr $bw_in / 1000 ` | |
bw_out=`expr $bw_out / 1000 ` | |
#echo $bw_in | |
#echo $bw_out | |
} | |
################# JUNOS INFO ############ | |
########################################### | |
getvalue_junos () { | |
bw_in=`snmpwalk -v 2c -c $COMMUNITY $HOSTNAME $OID_inrate_junos.$ifoid | awk '{print $4}'` | |
bw_out=`snmpwalk -v 2c -c $COMMUNITY $HOSTNAME $OID_outrate_junos.$ifoid | awk '{print $4}'` | |
bw_in=`expr $bw_in / 1000 ` | |
bw_out=`expr $bw_out / 1000 ` | |
} | |
#Get system infomation | |
sysinfo=`snmpwalk -v 2c -c $COMMUNITY $HOSTNAME $OID_sysDescr | awk 'NR==1 {print $4}'` | |
#Get Interface's OID | |
ifoid=`snmpwalk -v 2c -c $COMMUNITY $HOSTNAME IF-MIB::ifDescr | grep $PORTNo$ | awk -F '[{. }]' '{print $2}'` | |
#Get Interface's description | |
channel=`snmpwalk -v 2c -c $COMMUNITY $HOSTNAME IF-MIB::ifAlias | grep $ifoid | awk '{print $4 $5 $6}'` | |
################# INFO ############ | |
########################################### | |
case "$sysinfo" in | |
Cisco) getvalue_cisco ;; | |
Juniper) getvalue_junos ;; | |
Quidway) getvalue_quidway ;; | |
esac | |
###Processing | |
if [ $bw_in -ge 0 ] && [ $bw_out -ge 0 ] ; then | |
if [ $bw_in -le $MAXinTH ] && [ $bw_in -ge $MINinTH ]; then | |
if [ $bw_out -lt $MAXoutTH ] && [ $bw_out -gt $MINoutTH ] ; then | |
echo -e "The $channel channel is normal\nTraffic on the port $PORTNo at the moment:\nInput: $bw_in Kbps\nOutput: $bw_out Kbps" | |
exit 0 | |
elif [ $bw_out -ge $MAXoutTH ] ; then | |
echo -e "Somethings worng! Output bandwidth of the $channel channel is so high, please check this direction!\nTraffic on the port $PORTNo at the moment: \n Output bandwidth: $bw_out Kbps\n Input bandwidth: $bw_in Kbps" | |
exit $CRITICAL | |
else | |
echo -e "Somethings worng! Output bandwidth of the $channel channel is so low, please check this direction!\nTraffic on the port $PORTNo at the moment: \n Output bandwidth: $bw_out Kbps\n Input bandwidth: $bw_in Kbps" | |
exit $CRITICAL | |
fi | |
elif [ $bw_in -gt $MAXinTH ] ; then | |
echo -e "Somethings worng! Input bandwidth of the $channel channel is so high, please check this direction!\nTraffic on the port $PORTNo at the moment: \n Output bandwidth: $bw_out Kbps\n Input bandwidth: $bw_in Kbps" | |
exit $CRITICAL | |
else | |
echo -e "Somethings worng! Input bandwidth of the $channel channel is so low, please check this direction!\nTraffic on the port $PORTNo at the moment: \n Output bandwidth: $bw_out Kbps\n Input bandwidth: $bw_in Kbps" | |
exit $CRITICAL | |
fi | |
else | |
echo "Cannot receive information, pls wait for rechecking ....." | |
exit $UNKNOWN | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment