#!/bin/bash

### check_cisco_traffic_usage
# Check Traffic Usage of an Interface on a Cisco Device
# 
# This is a bit tricky, because cisco's snmp counter are independent from the cli output.
# The snmp counters can't be reset at runtime, they online reset themself at reboot.
# So in addition to the snmp values we get, we need to calculate....
#
# We save the last check results/sums in a text file so make sure you set "tmpfile" below.
# Please create it, fill in 4 zeros and make sure it is writable by the user that nagios runs as
# 
#	$ touch /home/nagios/check_snmp_cisco_traffic.txt
#	$ echo 0 0 0 0 > /home/nagios/check_snmp_cisco_traffic.txt
#	$ chown nagios:nagios /home/nagios/check_snmp_cisco_traffic.txt
# 
# This plugin currently only uses HC/64bit counter, because 32bit begin from zero too often.
# You also have to find out the interface number yourself, i don't, yet, want the script to
# search for it every runtime. Just snmpwalk your device like this:
#
# $ snmpwalk -v2c -c community-string HOST 1.3.6.1.2.1.31.1.1.1.1
# IF-MIB::ifName.1 = STRING: Fa0
#          This -^- is the interface-number
#
# Version 0.1, Copyright (c) 2008 by Michael Boehm <dudleyperkins_AT_gmail.com>
#
# TODO: -definitively add multiple check support with prefix+name_of_txt_file (should be easy)
#	-testing
#	-maybe accepting Interface descriptions as argument
#	-maybe include check for 32bit counter
###

### License Information:
# 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 (or with Nagios);  if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA
###

#----the-very-important-tempfile--------#
# if you use suffix: $1 this will be the IP adress of the given device
# so different txt files for different devices are possible

tmpfile=/home/nagios/check_snmp_cisco_traffic_$1.txt

if [ ! -f $tmpfile ]; then
   echo -e "couldn't find temp file!! --> Please read the text in this plugin";
   exit 3 
fi

#----if-help-is-needed------------------#

if [ "$1" = "help" ]; then
   echo -e "\nCheck Traffic Usage of an Interface on a Cisco Device\n";
   echo -e "Version 0.1, Copyright (c) 2008 by Michael Boehm <dudleyperkins_AT_gmail.com>\nLast Modified: 2008-09-22\n";
   echo -e "--------------------------------------------------";
   echo -e "Usage: ./check_cisco_traffic_usage <host> <snmp-string> <if-number> <warn> <crit>";
   echo -e "--------------------------------------------------\n";
   echo -e "<host>\t\tHostname or IP Address";
   echo -e "<snmp-string>\tthe snmp community string";
   echo -e "<if-number>\tIs easily found out, snmpwalk your device like this:";
   echo -e "\t\t--> snmpwalk -v2c -c community-string HOST 1.3.6.1.2.1.31.1.1.1.1";
   echo -e "\t\t--> IF-MIB::ifName.1 = STRING: Fa0";
   echo -e "\t\t\t     This -^- is the interface-number";
   echo -e "<warn>\t\twarning value in MB";
   echo -e "<crit>\t\tcritical value in MB\n" && exit 0;
fi

#----short-help-less-arguments----------#

if [ ! "$#" -gt "4" ]; then
   echo -e "\nWarning: Not enough command line arguments.\n";
   echo -e "Check Traffic Usage of an Interface on a Cisco Device";
   echo -e "Version 0.1, Copyright (c) 2008 by Michael Boehm <dudleyperkins_AT_gmail.com>\nLast Modified: 2008-09-22\n";
   echo -e "--------------------------------------------------";
   echo -e "Usage: ./check_citrix <host> <snmp-string> <if-number> <warn> <crit> \n  or just \nUsage: ./check_cisco_traffic_usage help";
   echo -e "--------------------------------------------------\n" && exit 3;
fi

#----variables-set-at-runtime-----------#

host=$1
snmpstring=$2
interface=$3
warn=$4
crit=$5

# current value
trafbyteIn=`snmpget -v2c -c $snmpstring $host 1.3.6.1.2.1.31.1.1.1.6.$interface |awk {'print $4'}`
trafbyteOut=`snmpget -v2c -c $snmpstring $host 1.3.6.1.2.1.31.1.1.1.10.$interface  |awk {'print $4'}`

# last value
traflastIn=`cat $tmpfile |awk {'print $1'}`
traflastOut=`cat $tmpfile |awk {'print $3'}`

# sum value
trafsumIn=`cat $tmpfile |awk {'print $2'}`
trafsumOut=`cat $tmpfile |awk {'print $4'}`

#----calculation-In----------------------#

if [ $trafbyteIn -gt $traflastIn ]; then
   trafdiffIn=$(echo "$trafbyteIn"-"$traflastIn"|bc)
   trafsumIn=`echo "$trafsumIn"+"$trafdiffIn"|bc`

elif [ $trafbyteIn -lt $traflastIn ]; then
   # this counter cannot be reset, unless the system was restarted
   # so no calculation here - we assume the current value IS the diff
   trafdiffIn=$trafbyteIn
   trafsumIn=`echo "$trafsumIn"+"$trafdiffIn"|bc`

#else 
   # no need to do something if equal values

fi

#----calculation-Out---------------------#

if [ $trafbyteOut -gt $traflastOut ]; then
   trafdiffOut=$(echo "$trafbyteOut"-"$traflastOut"|bc)
   trafsumOut=`echo "$trafsumOut"+"$trafdiffOut"|bc`
   
elif [ $trafbyteOut -lt $traflastOut ]; then
   # this counter cannot be reset, unless the system was restarted
   # so no calculation here - we assume the current value IS the diff
   trafdiffOut=$trafbyteOut
   trafsumOut=`echo "$trafsumOut"+"$trafdiffOut"|bc`

#else
   # no need to do something if equal values

fi

#----write-values-for-next-run-----------#

# if any variable is 0 or has no value at all better do nothing and quit unknown
for X in "$trafbyteIn" "$trafsumIn" "$trafbyteOut" "$trafsumOut"
do
   if [ -z $X ]; then
	echo "Traffic UNKNOWN - $trafmb MB in Sum|traffic=$trafmb;$warn;$crit;0; In=$trafmbIn;;;0; Out=$trafmbOut;;;0;"
	exit 3
   fi
   if [ $X = 0 ]; then
	echo "Traffic UNKNOWN - $trafmb MB in Sum|traffic=$trafmb;$warn;$crit;0; In=$trafmbIn;;;0; Out=$trafmbOut;;;0;"
	exit 3
   fi
done

echo $trafbyteIn $trafsumIn $trafbyteOut $trafsumOut > $tmpfile

#----human-readable-for-output-----------#

trafmbIn=`echo "scale=2; $trafsumIn"/1024/1024|bc`
trafmbOut=`echo "scale=2; $trafsumOut"/1024/1024|bc`
trafsumInOut=`echo "$trafsumIn"+"$trafsumOut"|bc`
trafmb=`echo "scale=2; $trafsumInOut"/1024/1024|bc`
trafmb1=`echo "$trafsumInOut"/1024/1024|bc`

#----output-and-exit-code----------------#

if [ $trafmb1 -ge $crit ]; then
   echo "Traffic CRITICAL - $trafmb MB in Sum|traffic=$trafmb;$warn;$crit;0; In=$trafmbIn;;;0; Out=$trafmbOut;;;0;"
   EXIT=2
elif [ $trafmb1 -ge $warn ]; then
   echo "Traffic WARNING - $trafmb MB in Sum|traffic=$trafmb;$warn;$crit;0; In=$trafmbIn;;;0; Out=$trafmbOut;;;0;"
   EXIT=1
elif [ $trafmb1 -lt $warn ]; then
   echo "Traffic OK - $trafmb MB in Sum|traffic=$trafmb;$warn;$crit;0; In=$trafmbIn;;;0; Out=$trafmbOut;;;0;"
   EXIT=0
else
   echo "Traffic UNKNOWN - $trafmb MB in Sum|traffic=$trafmb;$warn;$crit;0; In=$trafmbIn;;;0; Out=$trafmbOut;;;0;"
   EXIT=3
fi

exit $EXIT
