Skip to content

Instantly share code, notes, and snippets.

@hafthanhf
Created March 3, 2015 15:06
Show Gist options
  • Save hafthanhf/afb4d40bf7d0a3d4f39a to your computer and use it in GitHub Desktop.
Save hafthanhf/afb4d40bf7d0a3d4f39a to your computer and use it in GitHub Desktop.
check a lacp link state, if one of the link is down, the script will generate alarm.
#!/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_lag=1.2.840.10006.300.43.1.1.1
COMMUNITY=bimat
VERSION="1.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 Gen eral Public License v2."
echo "Author: hafthanhf"
echo "Hanoi - 2014"
}
print_help() {
print_version
echo ""
echo "**** Check the status of LACP link ****"
echo -e "This script opertates based on checking the speed of a LACP port, \nif one of its link goes down, the speed will decrease respectively.\nBased on that the Script gives the alarm exactly"
echo ""
echo -e "-H ADDRESS - Hostname to query (default: 127.0.0.1)\n -p Port nam e \n -n number of link in a LACP bundle \n -V Print version"
echo "-h Print this help **** have fun"
echo "EXP: this command use to check a lacp link on Junos device, with 2 physical links:"
echo "./check_lacp -H 10.10.10.10 -p ae2 -n 2"
}
# Arguments
while getopts H:p:n:hV OPT
do
case "$OPT" in
H) HOSTNAME="$OPTARG" ;;
p) portname="$OPTARG";;
n) amount="$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}'`
if [ "$amount" -gt "4" ]; then
echo "Cannot check, too many link"
exit 3
fi
#SPEED=`expr $amount \* 1000000000`
case "$sysinfo" in
Cisco) lacp="Port-channel" ;;
Juniper) lacp="ae" ;;
esac
#echo $lacp
#echo $sysinfo
portid=`snmpwalk -v 2c -c $COMMUNITY $HOSTNAME IF-MIB::ifDescr | grep -w "\ $port name$" | awk -F '[{. }]' '{print $2}'`
#echo $portid
portalias=`snmpwalk -v 2c -c $COMMUNITY $HOSTNAME IF-MIB::ifAlias | grep "$portid " `
#echo $portalias
#echo "List Port-channel:"
#snmpwalk -v 2c -c $COMMUNITY $HOSTNAME IF-MIB::ifDescr | grep $lacp | awk '{prin t $4}'
speed=`snmpwalk -v 2c -c $COMMUNITY $HOSTNAME IF-MIB::ifSpeed.$portid | awk '{pri nt $4}'`
if [ "$speed" -lt 999999999 ]; then
baseband=100000000
else
baseband=1000000000
fi
refnum=`expr $speed / $baseband`
SPEED=`expr $speed / 1000000`
echo "Interface Speed: $SPEED Mbps"
if [ "$refnum" -lt "$amount" ]; then
echo "One of the link is down, plz check this link "
exit 2
else echo "link OK"
exit 0
fi
################# INFO ############
###########################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment