Hello All!
FILE ATTACHED
This is my first post to Evilzone, I just wanted to share what I have made.
Only reason I'm sharing this is because I could not, for the life of me, find
a simple program that sends alerts when bandwidth goes above a limit.
Originally I had everything in the one file; it was set-up as an if statement to
send an alert if the result from vnstat was above X. But since I use nagios
where I am implementing this, I set it up to use exit codes.
It works alone as well. It uses vnstat to retrieve the actual traffic.
Also you need bc to do calculations that are in decimal.
I hope this isn't too elementary for you guys.
Thanks
#!/bin/bash
#* Version 0.9 *#
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 " OPTIONAL: -w RXbit/s:TXbit/s"
echo " warning threshold NOTE: This is in bits per second"
echo " OPTIONAL: -c RXbit/s:TXbit/s"
echo " critical threshold NOTE: You must define warning threshold to modify critical threshold"
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;;
*) 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[@]}"
exit
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"
elif [[ $RXEXITCODE == 2 || $TXEXITCODE == 2 ]];then
EXITCODE="2"
nagiosmsg="Critical"
elif [[ $RXEXITCODE -le 1 || $TXEXITCODE -le 1 ]];then
EXITCODE="1"
nagiosmsg="Warning"
else
EXITCODE="3"
nagiosmsg="Unkown"
fi
exit $EXITCODE