Skip to content

Instantly share code, notes, and snippets.

@hafthanhf
Last active August 29, 2015 14:19
Show Gist options
  • Save hafthanhf/1325809c663bdd9677ff to your computer and use it in GitHub Desktop.
Save hafthanhf/1325809c663bdd9677ff to your computer and use it in GitHub Desktop.
check all bgp session on router (tested with Cisco and Juniper)
#!/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
OID_hostname=SNMPv2-MIB::sysName.0
COMMUNITY=*****************
DBname=************
DBtable=***********
VERSION="1.1"
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."
echo "add HKIX feature: it will not be critical if there a only some HKIX connections die"
}
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 -l logical-system for Junos router \n -V Print version"
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"
}
# Arguments
while getopts H:l:v:hV OPT
do
case "$OPT" in
H) HOSTNAME="$OPTARG" ;;
l) LS="$OPTARG";;
h) print_help
exit $STATE ;;
V) print_version
exit $STATE ;;
esac
done
############################
######---Main----###########
############################
sysinfo=`snmpwalk -v 2c -c $COMMUNITY $HOSTNAME $OID_sysDescr | awk 'NR==1 {print $4}'`
hostname=`snmpwalk -v 2c -c $COMMUNITY $HOSTNAME $OID_hostname | awk 'NR==1 {print $4}'`
################# INFO ############
###########################################
#echo "############ $LS ########"
#echo $sysinfo
if [ "$LS" == "physical" ]; then
# Get BGP status
#/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_$sysinfo $HOSTNAME | grep "Active" | awk '{print "\47" $1 "\47, "}' | sed 's/,*$//'
pf2=`echo $(/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_$sysinfo $HOSTNAME | grep "Active\|Connect" |grep -v AS | awk '{print "\47" $1 "\47, "}') | sed 's/,*$//'`
else
pf2=`echo $(/usr/bin/expect /usr/local/nagios/libexec/plugins/get_bgp_ls_$sysinfo $HOSTNAME $LS | grep "Active\|Connect" | grep -v AS | awk '{print "\47" $1 "\47, "}' ) | sed 's/,*$//'`
fi
export NF=`echo $pf2 | wc -w`
if [ "$pf2" != "" ]; then
#determine this is HKIX group or not#
group=`mysql -u hafthanhf -e "select group_BGP from $DBname.$DBtable where peer_BGP in ($pf2);" | awk '{if(NR>1)print}' |column -t `
nd=`echo "$group" | wc -l`
for i in $group then
do
if [ "$i" == "HKIX" ]; then
hkix=1
else
hkix=0
break
fi
done
if [ "$hkix" = 0 ] && [ "$nd" -lt "10" ]; then
STATE=1
# echo "warning"
else
STATE=2
# echo "critical"
fi
#####################################
echo "There are $NF BGP connections failed on $hostname:$LS router"
mysql -u hafthanhf -e "select group_BGP,peer_BGP,name_BGP,Email from $DBname.$DBtable where peer_BGP in ($pf2);" | column -t
exit $STATE;
else
echo -e "Everything is fine----"
# echo $fullinfo
STATE=0
exit 0;
fi
cat /tmp/bgp_$HOSTNAME_$LS.log
exit $STATE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment