#!/bin/bash
#
# RDNMcron by Subban
#
# Execute from a cron with :
# 59 23 * * * /home/subbass/bin/RDNMcrom
#
# This equals 11:59pm. When it runs it will take the days total
# from vnstat and log it, you can later parse the data in the log
# with `rdnm` in a console to check your current quota used.
#
# This script depends on vnstat being installed and working
# The following line should get it setup, correct the eth0
# depending on your active network card.
#
# vnstat -u -i eth0
#
basename=~/.rdnm
todaytotal=0
leftcrop=0
sanitised=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 [ -d $basename ]; then
touch $basename/bwdata
touch $basename/temp
else
mkdir $basename
touch $basename/bwdata
touch $basename/temp
fi
vndata=`vnstat -s`
# remove all text from the left upto and including 'today'
leftcrop=${vndata#*today}
#remove text from the right upto the last 'MiB'
#
# GiB or MiB depending on output from VNSTAT
#
# total=$(echo "scale=2; ${total}+${part}" | bc)
#
todaytotal=${leftcrop%% MiB*}
echo $todaytotal|grep -q GiB &&
todaytotal=${todaytotal%% GiB*} &&
todaytotal=$(echo "scale=2; ${todaytotal}*1024" | bc)
#echo $todaytotal|grep -q GiB && todaytotal=$(( ${leftcrop%% GiB*} * 1024 ))
## this next section is obsolete but left intact, now I use bc in rdnm to
## calculate with the decimal places intact.
#
#this bit removes anything to the right of the decimal point
#sanitised=${todaytotal%%.*}
#echo $sanitised >> $basename/bwdata
echo $todaytotal >> $basename/bwdata
# tell the system we finished ok
exit 0