#!/bin/bash
# Written By: Duane Larson
# Created: 3/13/2008
# Description: Used to monitor Slot Modules in Cisco 6500 and 4500 Switches
#
# License: This nagios plugin comes with ABSOLUTELY NO WARRANTY. You may redistribute copies of
# the plugins under the terms of the GNU General Public License. For more information about these 
# matters, see the GNU General Public License.
# Version: 1.0

# Varialbles
usage="Usage: check_cisco_modules -H <hostname> -C <community>" 
community=""
hostname=""
debug=0
nagios_status=0

monitor_devices()
{

	num_of_modules=`/usr/bin/snmpwalk -v 2c -c $community $hostname 1.3.6.1.2.1.47.1.1.1.1.13.1 | cut -c53` 
	
	x=1

	while [ "$x" -ne "$num_of_modules" ]
	do
  		module_status[$x]=`/usr/bin/snmpwalk -v 2c -c $community $hostname 1.3.6.1.4.1.9.9.117.1.2.1.1.2."$x"000 |  cut -c52`
		
		if [ ${module_status[$x]} -ne 2 ]
		then
			#Service is Critical and needs to be set to Critical in Nagios
			nagios_status=2

		fi	

		((x += 1))
	done


}


output_status()
{

	if [ $nagios_status = 0 ]
	then
		echo -n "OK - No Bad Modules"
		exit 0
	else
		y=1
		
		echo -n "Failed: Modules Down - Slots:"

        	while [ "$y" -ne "$num_of_modules" ]
        	do
                	#echo ${module_status[$y]}
			if [ ${module_status[$y]} = 2 ]
			then
				echo -n " $y"				

			
			fi
			((y += 1))
        		done
		exit 2
	fi
}




## Main Method
while getopts ":C:H:v" options;
do
	case $options in
		C ) community="$OPTARG";;
		H ) hostname="$OPTARG";;
    		v ) debug=1;;
    		? ) echo $usage
         		exit 1;;

	esac
done


# Debugging
if [ $debug -eq 1 ]
then
  echo "Debug: community: $community";
  echo "Debug: hostname: $hostname";
fi

# Error Checking
if [ "$community" == "" ] || [ "$hostname" == "" ]
then
  echo $usage
  exit 1
fi



monitor_devices
output_status
