#!/bin/bash
# Author: Katharina Drexel
# Date: Jul 03 2014
# Reason: #16477719

usage ()
{
	echo "Usage: $0 -h <hostname> -c <snmp community> -m <ifinbroadcast|ifinmulticast> -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
	ifinbroadcast)	oid=".1.3.6.1.2.1.31.1.1.1.3.";;
	ifinmulticast)	oid=".1.3.6.1.2.1.31.1.1.1.2.";;
	*)		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 )))
if [ $counter == 0 ]; then
	increase=0
else
	increase=$(echo $(( $delta * 100 / $counter )))
fi

echo $counter > $tmpfile

status=$STATE_UNKNOWN #default

if [ $delta -eq 0 ]; then
	echo "Broadcast did not increase"
	status=$STATE_OK
  # nach reboot
else
	if [ $increase -le 5 ]; then 
		echo "normal broadcast traffic"
		status=$STATE_OK
	elif [[(5 -lt $increase) && ($increase -lt 10)]]; then
		echo "increasing broadcast traffic"
		status=$STATE_WARNING
	elif [ $increase -le 10 ]; then
		echo "high broadcast traffic"
		status=$STATE_CRITICAL
	else
		status=$STATE_UNKNOWN
		echo "Strange things happening"
	fi
fi

exit $status
