#!/bin/bash

#* Version 1.0 *#

if [[ `which vnstat` == $! ]]; then
	echo;
	echo "Sorry which can't find vnstat"
	echo;
	echo "vnstat can be installed by running these two commands"
	echo;
	echo "yum install -y epel-release"
	echo "yum install -y vnstat"
	echo;
	echo "NOTE: yum may not be your package installer"
exit 3;
fi

 

####  Parsing user options  ####
function HELP() {
	echo;
	echo "Usage: band -i interface -t sampletime -d trafficdirection -w warnthresh -c critthresh"
	echo;
	echo;
	echo " OPTIONAL: -i interface"
	echo "                  default eth0 - defined in vnstat.conf"
	echo " OPTIONAL: -t sampletime"
	echo "                  default 5 secs - defined in vnstat.conf"
	echo " OPTIONAL: -d {in|both|out}"
	echo "                  traffic coming in, out, or both, default is both"
	echo " NEED: -w RXbit/s:TXbit/s"
	echo "                  warning threshold  NOTE: This is in bits per second"
	echo " NEED: -c RXbit/s:TXbit/s"
	echo "                  critical threshold  NOTE: You must define warning threshold to modify critical threshold"
	echo " OPTIONAL: -h nagioshostname"
	echo "			this is the hostname in nagios to send passive check to"
	echo " OPTIONAL: -s nagiosservice"
	echo "			this is the service in nagios to send passice check to"
	echo;
	echo "Comming Soon - because it hurts my head to think about this"
	echo;
	echo " OPTIONAL: -b {bits|bytes}"
	echo "                  default bits - this will change from bits to bytes"
	echo " OPTIONAL: -u {K|M|G}"
	echo "			default is unabbreviated bits - this will abbreviate to Killobits, Megabits, Gigabits"
}

while [[ $# > 0 ]]; do
  key="$1"
  shift
  case $key in
	-i) interface=$1; shift;;
	-t) sampletime=$1; shift;;
	-d) direction=$1; shift;;
	-w) warn=$1; shift;;
	-c) crit=$1; shift;;
	-h) nhost=$1; shift;;
	-s) nserv=$1; shift;;
	*) HELP; exit 3;;
  esac
done

rxwarn="`echo $warn|cut -f1 -d":"`"
rxcrit="`echo $crit|cut -f1 -d":"`"
txwarn="`echo $warn|cut -f2 -d":"`"
txcrit="`echo $crit|cut -f2 -d":"`"

###########################################################
##			Using getopts			 ##
## Here we check to make sure user has entered tresholds ##
OPT=""
extraParams=""
if [ -n "$interface" ]; then
        OPT="$OPT -i$interface"
	vnface="-i $interface"
fi
if [[ $sampletime = 1 ]]; then
	echo "Time must be more than 1 second"
	exit 3;
fi
if [ -n "$sampletime" ]; then
        OPT="$OPT -t$sampletime"
fi
if [ -n "$direction" ]; then
        OPT="$OPT -d$direction"
fi
if [ -n "$warn" ]; then
        OPT="$OPT -w$warn"
	warnthresh=$warn
else
	echo "Need to specify warning threshold with -w #:#"
	echo "Recommended start treshold is 20000"
	echo "NOTE: This is in bit/s"
	echo;
	exit 3;
fi
if [ -n "$crit" ]; then
        OPT="$OPT -c$crit"
	critthresh=$crit
else
	echo "Need to specify critical threshold with -c #:#"
	echo "Recommended start threshold is 80000"
	echo "NOTE: This is in bit/s"
	echo;
	exit 3;
fi



####  Finding valid interfaces for error handling  ####
faces=(`ifconfig|grep 'Link encap'|awk '{print $1}'`)

foundface=0

for i in "${faces[@]}"
do
    if [[ $i == $interface || $interface == "" ]]; then
        foundface=1
    fi
done

if [ $foundface == 0 ]; then
	echo "Sorry that is not an interface on this machine"
	echo;
	echo "Availble Interfaces:"
	printf '%s\n' "${faces[@]}"
fi

#### Gathering data using vnstat ####
vnstat=`vnstat -tr $sampletime $vnface`

########  Down Traffic Formating  ########
band="`echo "$vnstat"|grep rx|awk '{print $2}'`"

bando=`bc -l <<< "$band * 1000"`
## I use no decimals when calculating ##
bandf=${bando%.*}
## Kbit/s conversion ##
kband=`bc -l <<< "scale = 2; $bandf/1000"`

######## Up Traffic Formating  ########
up="`echo "$vnstat"|grep tx|awk '{print $2}'`"

upo=`bc -l <<< "$up * 1000"`
## I use no decimals when calculating ##
upf=${upo%.*}
## Kbit/s conversion ##
kup=`bc -l <<< "scale = 2; $upf/1000"`


###########################################################
########        if statements for tresholds        ########
###########################################################
if [[ $direction == "in" || $direction == "both" || $direction == "" ]];then
####  Down If  ####
if [[ $bandf -gt 0 && $bandf -lt $rxwarn ]]; then
	echo "Ok - Machine RX idling @ $kband Kb/s"
	RXEXITCODE=0
elif [[ $bandf -ge $rxwarn && $bandf -lt $rxcrit ]]; then
	echo "Warning - Machine using RX bandwidth @ $kband Kb/s"
	RXEXITCODE=1
elif [[ $bandf -ge $rxcrit ]]; then
	echo "Critical - Machine using a lot of RX bandwidth @ $kband Kb/s"
	RXEXITCODE=2
else
	echo "Unkown - Unkown RX output"
	RXEXITCODE=3
fi
fi


if [[ $direction == "out" || $direction == "both" || $direction == "" ]];then
####  Up If  ####
if [[ $upf -ge 0 && $upf -lt $txwarn ]]; then
        echo "Ok - Machine TX idling @ $kup Kb/s"
        TXEXITCODE=0
elif [[ $upf -ge $txwarn && $upf -lt $txcrit ]]; then
        echo "Warning - Machine using TX bandwidth @ $kup Kb/s"
        TXEXITCODE=1
elif [[ $upf -ge $txcrit ]]; then
        echo "Critical - Machine using a lot of TX bandwidth @ $kup Kb/s"
        TXEXITCODE=2
else
        echo "Unkown - Unkown TX output"
        TXEXITCODE=3
fi
fi


####  Nagios Exitcode Handling  ####
if [[ $RXEXITCODE == 0 && $TXEXITCODE == 0 ]];then
	EXITCODE="0"
	nagiosmsg="OK - RX: $kband and TX: $kup Kb/s"
elif [[ $RXEXITCODE == 2 || $TXEXITCODE == 2 ]];then
	EXITCODE="2"
	nagiosmsg="Critical - RX: $kband and TX: $kup Kb/s"
elif [[ $RXEXITCODE -le 1 || $TXEXITCODE -le 1 ]];then
	EXITCODE="1"
	nagiosmsg="Warning - RX: $kband and TX: $kup Kb/s"
else
	EXITCODE="3"
	nagiosmsg="Unkown RX @ $kband and TX @ $kup"
fi

echo -e "$nhost;$nserv;$EXITCODE;$nagiosmsg" | /usr/sbin/send_nsca -H 172.16.0.64 -p 5667 -d ";" -c /etc/nagios/send_nsca.cfg
exit $EXITCODE
