Ivor O’Connor

November 11, 2008

Howto: Logging Internet Bandwidth Useage

Filed under: Uncategorized — ioconnor @ 5:51 am

EVDO’s 5GB monthly limit seems very small. However it’s always best to verify. Since I could not google a solution I wrote the following code. This will log my internet use to a file called “/var/log/bandwidth-messages”. After I’ve accumulated enough information I’ll massage it to something useful. I think I’d like to see a daily total and a rolling total of the last 31 days. I don’t play computer games, download movies, etc.. However I do listen to Pandora Radio, DemocracyNow, NPR, and a few odd radio stations. So who knows what my useage will turn out to be. Here’s the code to log internet useage:

#!/bin/bash

SHUTDOWNFILE=/etc/init.d/bandwidth-shutdown.sh
SHUTDOWNFILELN0=/etc/rc0.d/K60bandwidth-shutdown.sh
SHUTDOWNFILELN1=/etc/rc1.d/K60bandwidth-shutdown.sh
SHUTDOWNFILELN6=/etc/rc6.d/K60bandwidth-shutdown.sh
STARTUPFILE=/etc/init.d/bandwidth-startup.sh
STARTUPFILELN2=/etc/rc2.d/bandwidth-startup.sh
rm -f $SHUTDOWNFILE
rm -f $SHUTDOWNFILELN0
rm -f $SHUTDOWNFILELN1
rm -f $SHUTDOWNFILELN6
rm -f $STARTUPFILE
rm -f $STARTUPFILELN2
rm -f /var/log/bandwidth-messages

(
cat <<‘EOF’
#!/bin/bash
dt=`date ‘+%Y %a %b %e %T’`
blah=`ifconfig eth0 | grep “RX bytes”`
echo “$dt $blah system shut down” >> /var/log/bandwidth-messages
exit 0
EOF
) > $SHUTDOWNFILE
chmod a+x $SHUTDOWNFILE
chown root:root $SHUTDOWNFILE
ln -s $SHUTDOWNFILE $SHUTDOWNFILELN0
ln -s $SHUTDOWNFILE $SHUTDOWNFILELN1
ln -s $SHUTDOWNFILE $SHUTDOWNFILELN6

(
cat <<‘EOF’
#!/bin/bash
dt=`date ‘+%Y %a %b %e %T’`
blah=`ifconfig eth0 | grep “RX bytes”`
echo “$dt $blah system started ” >> /var/log/bandwidth-messages
exit 0
EOF
) > $STARTUPFILE
chmod a+x $STARTUPFILE
chown root:root $STARTUPFILE
ln -s $STARTUPFILE $STARTUPFILELN2

touch /var/log/bandwidth-messages
chmod 777 /var/log/bandwidth-messages

It works fine under Ubuntu 8.04. Save it away in a file and type “sudo file-name” and start recording. The output looks like this:
2008 Mon Nov 10 21:32:03 RX bytes:8925613 (8.5 MB) TX bytes:603283 (589.1 KB) system shut down
2008 Mon Nov 10 21:33:11 RX bytes:0 (0.0 B) TX bytes:94 (94.0 B) system started

If anybody sees this post and knows a better way please post it. I feel like I’m wasting time reinventing the wheel.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: