Bash script to monitor bandwidth usage of a linux server

Scenario :-

One of my linux server was causing bandwidth chock in our DC . So I have decided to monitor the bandwidth usage ,using the following server. I used the command ” vnstat ” to produce the traffic in and out report. This script will check for the bandwidth usage 3 times before sending the e-mail alert. Bandwidth usage should cross the threshold value in all these three checks . If the server is added to nagios then I have a better solution here : Bandwidth Monitoring using Nagios

1)  vi /usr/local/scripts/check_bandwidth.sh

#!/bin/bash
#Monitory Bandwidth Developed by Adminlogs.info
#Tested and verified with CentOS and RedHat 5
hostname=`hostname`
vnstat -tr > /tmp/monitor
i=0
in=`cat /tmp/monitor | grep rx | grep -v kbit | awk '{print $2}' | cut  -d . -f1`
inx=$(($in + $i))
out=`cat /tmp/monitor | grep tx | grep -v kbit | awk '{print $2}' | cut  -d . -f1`
outx=$(($out + $i))
 
##### Second Test after 2 minutes
sleep 120
vnstat -tr > /tmp/monitor2
ini2=`cat /tmp/monitor2 | grep rx | grep -v kbit | awk '{print $2}' | cut  -d . -f1`
inx2=$(($in2 + $i))
out2=`cat /tmp/monitor2 | grep tx | grep -v kbit | awk '{print $2}' | cut  -d . -f1`
outx2=$(($out2 + $i))

#### Third Test after 4 minutes
sleep 120
vnstat -tr > /tmp/monitor3
ini3=`cat /tmp/monitor3 | grep rx | grep -v kbit | awk '{print $2}' | cut  -d . -f1`
inx3=$(($in2 + $i))
out3=`cat /tmp/monitor3 | grep tx | grep -v kbit | awk '{print $2}' | cut  -d . -f1`
outx3=$(($out3 + $i))

#### condition checking
[ $outx -ge 10 ] && [ $outx2 -ge 10 ] && [ $outx3 -ge 10 ]
out4=$?
if [ $out4 -eq 0 ]
then
cat /tmp/monitor /tmp/monitor2 /tmp/monitor2 >> /tmp/monitor_result
mail -s "Outbond Bandwidth usage is critical on $hostname"  admin[at]adminlogs[dot]info  < /tmp/monitor_result
fi
#### clearing old results
> /tmp/monitor
> /tmp/monitor2
> /tmp/monitor3
> /tmp/monitor_result

If you have a nagios server then I have a better solution, check here : Bandwidth Monitoring using Nagios

2) Add the scripts to crontab ( run every 10 minutes )

*/10  * * * * /bin/bash /usr/local/scripts/check_bandwidth.sh  > /dev/null 2>&1