#!/bin/bash
# Author: Katharina Drexel
# Date: Oct 15 2010
# Reason: #10305182 + #610510

usage ()
{
	echo "Usage: $0 -h <hostname> -c <snmp community> -m <ifinerrors | ifouterrors | ifindiscards | ifoutdiscards> -i <ifindex>"
}
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

while getopts c:h:i:m: opt
do	case $opt in
	c)	commstr="$OPTARG";;	
	h)	hostname="$OPTARG";;
	i)	ifindex="$OPTARG";;
	m) 	mibtype="$OPTARG";;
	[?])	usage
		exit 1;;
	esac
done

if [[ ( -z $commstr ) || ( -z $hostname ) || ( -z $ifindex ) || ( -z $mibtype ) ]]
then
	usage
	exit 42
fi

case $mibtype in
	ifinerrors)	oid=".1.3.6.1.2.1.2.2.1.14.";;
	ifouterrors)	oid=".1.3.6.1.2.1.2.2.1.20.";;
	ifindiscards)	oid=".1.3.6.1.2.1.2.2.1.13.";;
	ifoutdiscards)	oid=".1.3.6.1.2.1.2.2.1.19.";;
	*)		echo "Wrong Parameter for -m"
			usage
			exit 42;;
esac

counter=$(snmpget -Ov -v2c -c $commstr $hostname $oid$ifindex | awk '{print $2}')
case $counter in
	[0-9]*)	;;
	*)	echo "Wrong Interface Number given: $ifindex"
		exit 43;;
esac 
	

tmpfile=/tmp/$mibtype\_$hostname\_$ifindex
if [ ! -f $tmpfile ]; then
	echo $counter > $tmpfile
fi

valuebefore=$(cat $tmpfile)
delta=$(echo $(( $counter - $valuebefore )))

status=$STATE_UNKNOWN #default

if [ $delta -eq 0 ]; then
	echo "Errors did not increase"
	status=$STATE_OK
# nach reboot
elif [ $delta -lt 0 ]; then
	echo "Errors did not increase significantly"
	status=$STATE_OK
	echo $counter > $tmpfile
#elif [ 0 -lt $delta -lt 6 ]; then
elif [[(0 -lt $delta) && ($delta -lt 6)]]; then
	echo "Errors increasing between 1 and 5"
	status=$STATE_WARNING
	echo $counter > $tmpfile
elif [ $delta -gt 5 ]; then
	echo "Errors increasing > 5"
	status=$STATE_CRITICAL
	echo $counter > $tmpfile
else
	status=$STATE_UNKNOWN
	echo "Strange things happening"
fi

exit $status
