#!/bin/sh
# FILE: "/home/Agassiz/langseth/scc/nagios/customScripts/check_cisco_cpu.sh"
# LAST MODIFICATION: "Thu, 22 Jul 2004 14:44:15 -0500 (root)"
# (C) 2004 by Ryan Langseth, <langseth@cs.und.edu>
# $Id:$
#
#
#  This Nagios plugin was created to check the cpu currently being used on a cisco 6509 router
#
# create by Ryan Langseth for 
# Scientific Computing Center (scc) at University of North Dakota
# Send me bug reports, questions and comments about this plugin.
# Latest version of this software:
# https://portal.aero.und.edu/twiki/bin/view/Know/DebianLinuxNagiosScriptsCheckCiscoCPU
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307
#

PROGNAME=`basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION=`echo '$Revision: .2 $' | sed -e 's/[^0-9.]//g'`

. $PROGPATH/utils.sh

print_usage() {
  echo "Usage:"
  echo "  $PROGNAME -o <OID> -H <hostname> -w <warning level> -c <critical level> [-p <community string>] "
  echo "  $PROGNAME -h "

  
}

print_help() {
  print_revision $PROGNAME $REVISION
  echo ""
  print_usage
  
	echo "Check current bandwidth usage for specific meter on a 6509 cisco router"
	echo ""
	echo "Options:"
	echo "	-o the OID to check"
	echo "	-p the community string (defaults to 'public')"
	echo "	-H host name"
	echo "	-w warning level"
	echo "	-c critical level"
	echo "	-h display help screen"
	echo ""
	echo ""
  support
  exit $STATE_UNKNOWN
}



STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

HOST=`hostname`

COMMSTR="public"
OID=.1.3.6.1.4.1.9.2.1.58.0
SNMPWALK=`which snmpwalk`
#SNMPHOST=""
critLvl=0
warnLvl=1

while getopts ":o:p:H:w:c:h" Option
do
# o is the OID for the snmp check
# p is the community String
# H is the Host name
# w is the warning level 
# c is the critical level 
# h is to display the help screen

	case $Option in 
		o ) OID=$OPTARG;;
		p ) COMMSTR=$OPTARG;;
		H ) SNMPHOST=$OPTARG;;
		w ) warnLvl=$OPTARG;;
		c ) critLvl=$OPTARG;;
		h ) print_help;;
		* ) echo "unimplemented option";;
		
		esac
done
	
	shift $((OPTIND - 1))

if [ $warnLvl -ge  $critLvl ] ; then 
	echo ""
	echo "Warning Level must be less than Critical Level"
	echo ""
	
	print_help
fi


if [ ! $SNMPHOST ] ; then 
	echo " Error - No SNMP Host given "
	echo ""
	print_help
	echo ""
fi

if [ $SNMPWALK == "" ] ; then
	`echo "WARNING - snmpwalk not installed on $HOST"`
	exit $STATE_UNKNOWN
fi

#Query the host for counter 
FIRST=`$SNMPWALK $SNMPHOST $COMMSTR $OID`
FIRST=`echo $FIRST | awk '{print $3}'`
if [ ! $FIRST  ] ; then
	echo "WARNING - No response from snmp query"
	exit $STATE_WARNING
fi



FOO=`expr $FIRST ">=" $critLvl`
if [ $FOO == "1" ] ; then
	echo "CRITICAL - CPU usage is $FIRST %" 
	exit $STATE_CRITICAL
fi

FOO=`expr $FIRST ">=" $warnLvl`
if [ $FOO == "1" ] ; then
	echo "WARNING - CPU usage is $FIRST %" 
	exit $STATE_WARNING
fi

echo "OK - CPU usage is $FIRST %"
exit $STATE_OK
echo $OUTPUT
