Skip to content

Instantly share code, notes, and snippets.

@hafthanhf
Last active August 29, 2015 14:15
Show Gist options
  • Save hafthanhf/4ded1940d67b477da9d8 to your computer and use it in GitHub Desktop.
Save hafthanhf/4ded1940d67b477da9d8 to your computer and use it in GitHub Desktop.
check_port_switch
#!/bin/bash
OK=0
CRITICAL=2
STATE=0
PLUGIN_PATH="/usr/local/nagios/libexec/"
OID_sysDescr=SNMPv2-MIB::sysDescr.0
OID_hostname=SNMPv2-MIB::sysName.0
COMMUNITY=bimat
VERSION="1.1"
author="hafthanhf"
portdown="/tmp/pd_$host_ip"
admindown="/tmp/ad_$host_ip"
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 -e "Change log:\n Fix SuchIntance alert\n work on Cisco devices: 2960, 2960G, 3750, EX4200, EX3200 "
echo "Writen by $author - hanoi 10/4/2014"
}
print_help() {
echo "**** Check physical port on a network device ****"
echo -e " -H ADDRESS - Hostname to query (default: 127.0.0.1)\n -h print this help \n -V Print version"
echo "********************************"
echo "Exp: ./check_port -H 10.255.0.xxx"
echo "Befor using this plugin, you must shutdown all unuse port or it will make unnecessary alerts"
echo "**********Tested devices: Cisco 2960, 2960G, 3750, 3750G*********"
}
# Arguments
while getopts H:hV OPT
do
case "$OPT" in
H) host_ip="$OPTARG" ;;
h) print_help
exit $STATE ;;
V) print_version
exit $STATE ;;
*) print_help
exit 0 ;;
esac
done
############################
######---Main----###########
############################
COMMAND="snmpwalk -v 2c -c $COMMUNITY $host_ip"
#touch $portdown
#touch $admindown
#Get system infomation
#sysinfo=` $COMMAND $OID_sysDescr | awk 'NR==1 {print $4}'`
#hostname=`$COMMAND $OID_hostname | awk 'NR==1 {print $4}'`
################# INFO ############
###########################################
#echo "############ $LS ########"
#echo $sysinfo
pd=`$COMMAND IF-MIB::ifOperStatus | grep down | grep -v ".51" | awk -F '[. ]' '{print $2}'`
echo "$pd" > /tmp/pd_$host_ip
#echo "Port admin down list:"
ad=`$COMMAND IF-MIB::ifAdminStatus | grep down | awk -F '[. ]' '{print $2}'`
echo "$ad" > /tmp/ad_$host_ip
re=`diff /tmp/pd_$host_ip /tmp/ad_$host_ip | grep "<" | awk '{print $2}'`
if [ "$re" = "" ]; then
echo "It seems to be OK!"
exit 0
else
echo "Warning, these port down has recently down, plz check them: "
#x=`echo "$re" `
for i in $re
do
alias=`$COMMAND IF-MIB::ifAlias.$i | awk '{print $4 $5}'`
descr=`$COMMAND IF-MIB::ifDescr.$i | awk '{print $4 $5}'`
echo "$descr $alias"
done
exit 2
fi
@hafthanhf
Copy link
Author

This script is writen as a nagios plugin to check switch's port state. Has been tested and work properly with some cisco and juniper devices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment