#!/bin/sh
# bandwith.sh - print a bandwidth report
# run from cron at 11:59 PM
# with sorting the traffic and other domains
DIR=/var/cpanel/bandwidth
DIRTMP=/tmp
tmpdomains=tmpdomains
outfile=outfile
outfile2=outfile2
tmpwork=tmpwork
tmpwork2=tmpwork2
admin=admin@hosting.com # change this to your email address
cd $DIRTMP
# remove tmp files before start but leave them on server after finishing
rm $tmpdomains $tmpwork $tmpwork2 $outfile $outfile2
# Set the date in the format the bandwidth reports use ->
# do not use next if your date fotmat in bandwidth report is "4.10.2008"
##### DATE=`date '+%m.%e.%Y'| sed -e 's: ::'`
dateSepearator=".";
declare -i day;
declare -i month;
declare -i year;
day=$(/bin/date +%d);
month=$(/bin/date +%m);
year=$(/bin/date +%Y);
DATE=$month$dateSepearator$day$dateSepearator$year;
######echo $DATE
cd $DIR
ls *.* |grep -v ".rrd" > $DIRTMP/$tmpdomains
cd $DIRTMP
for i in `cat $tmpdomains`; do
grep "$DATE" $DIR/$i > $tmpwork
awk 'BEGIN {FS = "=" }
/all=/ { ALL = ($2/1048576) }
/http=/ { HTTP = ($2/1048576) }
/ftp=/ {FTP = ($2/1048576) }
/pop3=/ { POP = ($2/1048576) }
/imap=/ { IMAP = ($2/1048576) }
/smtp=/ { SMTP = ($2/1048576) }
END {
printf("%40s\t%10d\t%10d\t%10d\t%10d\t%10d\t%10d\n", DOMAIN,ALL,HTTP,FTP,POP,IMAP,SMTP)
}' $tmpwork DOMAIN=$i >> $tmpwork2
done
# Sort the traffic by ALL //may use --key=1/2/3/4/5 and etc.
sort --key=2 -nr $tmpwork2 > $tmpwork
echo "Top20 Bandwidth report for `uname -n` $DATE in KB" > $outfile2
echo " " >> $outfile2
echo ""|awk '{ printf("%40s\t%10s\t%10s\t%10s\t%10s\t%10s\t%10s\n", "DOMAIN","ALL","HTTP","FTP","POP","IMAP","SMTP") }' >> $outfile2
cat $tmpwork |head -n 20 >> $outfile2
# Create a header for our report
echo "Bandwidth report for `uname -n` $DATE in KB" > $outfile
echo " " >> $outfile
echo ""|awk '{ printf("%40s\t%10s\t%10s\t%10s\t%10s\t%10s\t%10s\n", "DOMAIN","ALL","HTTP","FTP","POP","IMAP","SMTP") }' >> $outfile
# Append the sorted traffic
cat $tmpwork >> $outfile
# clean up temp files
### rm $tmpfile $tmpdomains $tmpwork $tmpwork2
# email the report
mail -s "Bandwidth report for $HOSTNAME" $admin < $outfile
mail -s "Bandwidth report for $HOSTNAME" $admin < $outfile2
### rm $outfile
#
# End
#