Page 1 of 1

bandwidth monitoring and no output from plugin

Posted: Thu Jun 30, 2016 11:53 pm
by hafthanhf
Hi all,
I wrote this script to check bandwidth of network devices's interface. I run it from command line successfully, the output is perfect, but when I add the scipt on nagios as a service, some time I receive this alert: no output from plugin with the UNKNOW state of the service. this cause of many notification about flapping state between UNKNOW and OK state.
Thank for any advice :)
this is my script:

Code: Select all

[size=85]#!/bin/bash
#Ussage: Check input bandwidth on each switch port
########## ./check_bw $IP_of_switch $port_No_with_slash $max_threshold $min_threshold#####
########## Version 3.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 "fix UNKNOWN error"
  echo "********* NOTE *********************************"
  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
  print_usage
}

# Plugin return statements
NORMAL=0
WARNING=1
CRITICAL=2
UNKNOWN=3

OID_sysDescr=SNMPv2-MIB::sysDescr.0 
OID_bw_in_junos="1.3.6.1.4.1.2636.3.3.1.1.1"
OID_bw_out_junos="1.3.6.1.4.1.2636.3.3.1.1.4"
# Default variables

VERSION=2.5
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/
PATH2=/usr/local/nagios/var
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
################# SYSTEM INFO   ############
###########################################
#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
portname=`snmpwalk -v 2c -c $COMMUNITY $HOSTNAME IF-MIB::ifAlias.$ifoid |  awk -F '[{:}]' '{print $4}'`
###########log#############
timelog=$PATH2/bwlog/timelog_$HOSTNAME"_"$ifoid
outlog=$PATH2/bwlog/byteout_$HOSTNAME"_"$ifoid
inlog=$PATH2/bwlog/bytein_$HOSTNAME"_"$ifoid
################# Traffic INFO   ############
###########################################
byteout=`snmpwalk -v 2c -c $COMMUNITY $HOSTNAME IF-MIB::ifOutOctets.$ifoid | awk -F '[{:}]' '{ print $4 }'`
bytein=`snmpwalk -v 2c -c $COMMUNITY $HOSTNAME IF-MIB::ifInOctets.$ifoid | awk -F '[{:}]' '{ print $4 }' `
date1=`cat $(echo $timelog)`
date2=`date +%s`
timediff=$(( $date2 - $date1 ))
#bytein1=`cat $inlog`
#byteout1=`cat $outlog`
indiff=$(($bytein - `cat $inlog`))
### debug###
#echo $indiff
outdiff=$(($byteout - `cat $outlog`))
#echo $outdiff
echo $(date +%s) > $timelog
echo "$byteout" > $outlog
echo "$bytein" > $inlog
bw_in=`expr $indiff \* 8 / $timediff / 1000`
bw_out=`expr $outdiff \* 8 / $timediff / 1000`
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
                        /usr/bin/printf "%b" "Cong $PORTNo : $channel hoat dong binh thuong\n\n Bang thong tren cong $PORTNo luc nay la:\nInput: $bw_in Kbps\nOutput: $bw_out Kbps"
                        exit 0
                        elif [ $bw_out -ge $MAXoutTH ] ; then
			/usr/bin/printf "%b" "Canh bao! Bang thong output tren: $PORTNo :  $channel cao qua, co the dang nghen duong truyen, lien he to GPON de tro giup \n\n Bang thong tren cong $PORTNo luc nay la: \n Output bandwidth: $bw_out Kbps\n Input bandwidth: $bw_in Kbps"
                                exit $CRITICAL
                        else
				echo -e "Canh bao! Bang thong output tren: $PORTNo : $channel dang thap qua, co the bi dut UPLINK!\nBang thong tren cong $PORTNo luc nay la: \n Output bandwidth: $bw_out Kbps\n Input bandwidth: $bw_in Kbps"
	                        exit $CRITICAL
                fi
        elif [  $bw_in -gt $MAXinTH ] ; then
				echo -e "Canh bao! Bang thong input tren: $PORTNo :  cao qua, co the dang nghen duong truyen, lien he to GPON de tro giup\nBang thong tren cong $PORTNo luc nay la: \n Output bandwidth: $bw_out Kbps\n Input bandwidth: $bw_in Kbps"
                exit $CRITICAL
        else
				echo -e "Canh bao! Bang thong input tren: $PORTNo : $channel dang thap qua, co the bi dut UPLINK!\nBang thong tren cong $PORTNo luc nay la: \n Output bandwidth: $bw_out Kbps\n Input bandwidth: $bw_in Kbps"
                exit $CRITICAL
        fi
else
#       echo -e "Bang thong tren cong $PORTNo luc nay la: \n Output bandwidth: $bw_out Kbps\n Input bandwidth: $bw_in Kbps"
#	echo "Khong lay duoc thong tin tren thiet bi, doi mot chut ....."
        exit $UNKNOWN
fi[/size]

Re: bandwidth monitoring and no output from plugin

Posted: Fri Jul 01, 2016 1:37 am
by Box293
What is the output when running the script as the nagios user?

Code: Select all

su nagios 
/usr/local/nagios/libexec/your_script.sh
echo $?

Re: bandwidth monitoring and no output from plugin

Posted: Fri Jul 01, 2016 9:05 pm
by hafthanhf
Thankyou Box293,
sometimes, the output is correct as below:

Code: Select all

Canh bao! Bang thong input tren: e0/1/1 :  cao qua, co the dang nghen duong truyen, lien he to GPON de tro giup
Bang thong tren cong e0/1/1 luc nay la: 
 Output bandwidth: 133854 Kbps
 Input bandwidth: 4638 Kbps
But sometimes :

Code: Select all

Timeout: No Response from 172.20.28.200
Do you have any idea to solve this?

Re: bandwidth monitoring and no output from plugin

Posted: Sun Jul 03, 2016 11:27 pm
by Box293
It looks like your plugin is timing out talking to the device, you could try adding the timeout argument (-t xxx) to your snmpwalk commands.

Also instead of using the snmpwalk commands you could use snmpget instead.