#!/bin/bash
#
# RDNM by Subban
#
# Rolling Duration Net Meter
#
# Simple script to output the total download
# so you can track your quota more easily.
# Only useful in conjuntion with RDNMcron which
# collects the days total and outputs a log file.
#
# This script depends on vnstat being installed and working
# The following 2 lines should get it setup, correct the eth0
# depending on your active network card.
#
# sudo aptitude install vnstat
# sudo vnstat -u -i eth0
#
# For the love of god check your wire is plugged in eth0
# or adjust the location in the second command accordingly.
# I wasted a day and half trying to "fix" vnstat to find
# the cable was in the wrong farking hole.
#
#
basename=~/.rdnm
total=0
count=0
combined=0
rolling=0
part=0
#
# check if vnstat is installed, spank user if not.
#
if [ -z "$(which vnstat)" ]; then
echo "$0: error: vnstat is not installed"
exit 1
fi
if [ "$1" = "-l" ]; then
echo -e "\nDays 1-10 \t Days 11-20 \t Days 21-30"
tail -30 $basename/bwdata > $basename/last30.temp
tac $basename/last30.temp | pr -tl 12 --columns=3
echo -e ""
elif [ "$1" = "-h" ]; then
echo -e "\n rdnm is a script to track the Demon internet 30 day download quota."
echo -e " The data file lives as a txt file in ~/.rdnm/ called bwdata"
echo -e "\n\tUseage:\n\t-------"
echo -e "\trdnm\t= Shows the current totals"
echo -e "\trdnm -l\t= List the last 30 days figures"
echo -e "\trdnm -h\t= Displays this help"
echo -e "\n"
exit 0
fi
if [ -d $basename ]; then
touch $basename/bwdata
touch $basename/temp
else
mkdir $basename
touch $basename/bwdata
touch $basename/temp
fi
#
# calculate total from the logfile (upto 30days for me)
#
rolling=`tail -30 $basename/bwdata`
echo $rolling > $basename/temp
for part in `cat $basename/temp`
do
total=$(echo "scale=2; ${total}+${part}" | bc)
count=$(($count+1))
done
#
# get todays current total
#
vndata=`vnstat -s`
leftcrop=${vndata#*today}
#
# Try and snip off the MiB
# If data is in GiB, this will fail, but we don't mind because
# Andabata helped me fix it in the next code chunk
#
todaytotal=${leftcrop%% MiB*}
#
# Now check if the $todaytotal from above has GiB in, if it does
# we amputate it, and then multiply the value by 1024, to make it
# into megabytes, which we prefer.
#
echo $todaytotal|grep -q GiB &&
todaytotal=${todaytotal%% GiB*} &&
todaytotal=$(echo "scale=2; ${todaytotal}*1024" | bc)
#
# Tally up the current values for todays leeching and the previous
# 30 days ISP abuse for a combined total.
#
combined=$(echo "scale=2; ${todaytotal}+${total}" | bc)
printf "\n"
printf "\t Bandwidth usage breakdown\n"
printf "\t%-10s ..... %10s MiB\n" "$count day" $total
printf "\t%-10s ..... %10s MiB\n" "Todays" $todaytotal
printf "\t%-10s ..... %10s MiB\n\n" "combined" $combined
#echo -e "Logged ($count day) rolling download total is \t$total MiB"
#echo -e "Current download today only \t\t$todaytotal MiB"
#echo -e "combined total for log and today \t\t$combined MiB"
# tell the system we finished ok
exit 0