Thursday, 22 May 2008

Modem Download Volume Logging

There doesn't seem to be an straightforward way to keep track of volume downloads and uploads over a modem on Ubuntu or any Linux - especially using NetworkManager.

Volume is important these days when on a broadband package with download limits; helps too when checking the bill; and helps to calm the nerves and perhaps use more of your limit than otherwise.

Based on a discussion on the forums (http://ubuntuforums.org/showthread.php?t=381138) I have mostly solved the problem with a simple script and the help of Wanda the Fish.

A simple script, called "logup" is placed in "/etc/network/if-up.d". It runs on interface up and adds to the /var/log/volume.log.tmp every 5 minutes (making sure that the logs are nearly accurate even on a system crash or power down without logging off first). A second script called "logdown" is placed in "/etc/network/if-down.d". It completes the current session log if the interface is shut down calmly.

On the Gnome desktop I've added Wanda the fish and changed her command to "tac /var/log/volume.log" (tac is cat in reverse, by the way).

/etc/network/if-up.d/logup

#!/bin/bash

interface="ppp0"
log="/var/log/volume.log"
tmp="$log.tmp"

echo >> $log

date >> $log

tail -1 $tmp >> $log
rm $tmp

total=0
for part in `cat $log | grep $interface`
do let "total+=part"
done
let "total/=(1024*1024)"
echo "Total: $total MB" >> $log

while true; do cat /proc/net/dev | grep $interface | tr ":" " " | tr -s " " |cut -d' ' -f2,3,11 >> $tmp; sleep 5m; done &


/etc/network/if-down.d/logdown

#!/bin/bash

interface="ppp0"
log="/var/log/volume.log"
tmp="$log.tmp"

cat /proc/net/dev | grep $interface | tr ":" " " | tr -s " " |cut -d' ' -f2,3,11 >> $tmp

0 comments:

iantheteacher

Planet ILUG