This post will explore how to get bandwidth usage alert from your system by the email, this simple script developed by shell. Before execute it you need to install vnstat and mail packages.
VNSTAT Command for getting bandwdith usage report.
MAIL Send alert mail from the system.
Create a file name bandwidth.sh and add the below code and give execute permission to run.
# touch bandwidth.sh
# chmod +x bandwidth.sh
# chmod +x bandwidth.sh
#!/bin/bash
stat=`vnstat | tail -1 | awk '{print $8}'`
IP=`/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'`
TO_ADDRESS="alerts@thelinuxfaq.com"
SUBJECT="Bandwidth AVG Used"
#set bandwith limit in GiB size,
band=10
#get bandwith reports
if [ $(echo "$stat > $band" | bc) -ne 0 ]
then
echo $stat
echo > /tmp/bandwidth.txt
vnstat > /tmp/bandwidth.txt
mail -s "$SUBJECT - $stat GiB in :$HOSTNAME-$IP" $TO_ADDRESS < /tmp/bandwidth.txt
else
echo "None of the condition met"
fi
Hi, This script is measuring and alerting Rx or Tx traffic or the sum of both?? Regards.
yes, Its calculated both Rx or Tx traffic, check this port, : i have explianed to this post : monitor bandwidth usage with vnstat in linux
What traffic he use for alert check? monthly, daily or total? I need a script that alert me if iam over 10TB in month.
yes, its daily (yesterday & today), both rx and tx usage.
I think that script will notify you if you have more than 10 MiB traffic