#!/bin/bash OK=0 CRITICAL=2 STATE=0 PLUGIN_PATH=/usr/local/nagios/libexec/plugins outputfile=/var/log/bgp.log OID_sysDescr=SNMPv2-MIB::sysDescr.0 COMMUNITY=******* DBname="mngbgp" DBtable="mng_bgp" VERSION="2.0" print_version() { echo "$SCRIPTNAME" version "$VERSION" 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." } print_help() { print_version echo "" print_usage echo "" echo "**** Check BGP connection on a router ****" echo "" echo -e "-H ADDRESS - Hostname to query (default: 127.0.0.1)\n This should be a rotuer's iP address\n -V Print version\n -d physical/logical system name " echo "-h Print this help" echo "Befor using this plugin, you can create a mysql database that have a 4 field table: id | group_BGP | name_BGP | peer_BGP" } debug () { print_version } # Arguments while getopts H:d:v:hV OPT do case "$OPT" in H) HOSTNAME="$OPTARG" ;; d) DEVICE="$OPTARG";; h) print_help exit $STATE ;; V) print_version exit $STATE ;; esac done ############################ ######---Main----########### ############################ #Get system infomation sysinfo=`snmpwalk -v 2c -c $COMMUNITY $HOSTNAME $OID_sysDescr | awk 'NR==1 {print $4}'` ################# INFO ############ ########################################### if [ "$DEVICE" == "physical" ]; then # Get BGP status info=`echo $(expect $PLUGIN_PATH/get_bgp_$sysinfo $HOSTNAME | grep Active | grep -v AS | awk '{print "\47" $1 "\47, "}')|sed 's/,*$//'` # remove 10 first lines, remove 2 last line, get the line that have Active only, print the firt collumn, replace the , character. else info=`echo $(expect $PLUGIN_PATH/get_bgp_ls_$sysinfo $HOSTNAME $DEVICE | sed '1,10d' | sed ':a;$!N;1,3ba;P;$d;D' | grep Active | awk '{print "\47" $1 "\47, "}')|sed 's/,*$//'` fi #echo $(expect $PLUGIN_PATH/get_bgp_ls_$sysinfo $HOSTNAME $DEVICE | sed ':a;$!N;1,3ba;P;$d;D') #echo $(expect $PLUGIN_PATH/get_bgp_$sysinfo $HOSTNAME | sed ':a;$!N;1,3ba;P;$d;D') if [ "$info" != "" ]; then STATE=2 echo -e "OOPs :-( Something in the way....\n This infomation is on $DEVICE router" mysql -u hafthanhf -e "select group_BGP,peer_BGP,name_BGP,Email from $DBname.$DBtable where peer_BGP in ($info);" | column -t else echo "OK :--------)" # echo $fullinfo STATE=0 fi echo -e "\n\n----hafthanhf----" exit $STATE