Keeping track of bandwidth usage is a good thing for a variety of reasons, particularly in situations where bandwidth accounting is required, such as paying for Internet services that are dependent upon the amount of bandwidth used.

One great tool for Linux is vnStat, which is available from http://humdi.net/vnstat/ (http://humdi.net/vnstat/). Some Linux distributions may bundle vnStat, but it is more likely that you will have to build it from source.

Begin by downloading the vnStat source tarball from the Web site and unarchiving it:

$ curl -O http://humdi.net/vnstat/vnstat-1.4.tar.gz
$ tar xvzf vnstat-1.4
$ cd vnstat-1.4
$ make
$ sudo make install

If you're building on a 64-bit platform, use make 64bit instead.

Next, create a cronjob to execute vnStat every five minutes to collect the bandwidth usage data. For instance, create /etc/cron.d/vnstat with the contents:

0-55/5 * * * * if [ -x /usr/local/bin/vnstat ] && [ `ls
/var/lib/vnstat/ | wc -l` -ge 1 ]; then /usr/local/bin/vnstat -u; fi

This assumes vnStat is installed in /usr/local and also assumes that /etc/cron.d is a location for system-run cron jobs (which is the default for most Linux distributions).

Next, initialise the database for each interface you wish to track:

# /usr/local/bin/vnstat -u -i eth0
# /usr/local/bin/vnstat -u -i eth1

Once this is done, the cron job will take care of the rest. Wait for the cron job to execute a few times and then you can view the data using:

# vnstat -i eth1 --short

rx / tx / total / estimated
eth1:
yesterday 82.15 MB / 809.36 MB / 891.52 MB
today 83.20 MB / 704.07 MB / 787.28 MB / 907 MB

A PHP front-end exists, which is very easy to configure, allowing you to view the data in a nicely presented Web graph; you can download this from the vnStat Web site. As well, a number of options are available so you can view traffic data for the current days and months.

Open Sourcery This was published in Open Sourcery, check every Monday for more stories

Related links

Comments

1

Barry Scheepers - 18/08/07

Super helpful! Got it running first time. Now if you could only set usage boundaries and have it email you when you're close to capping your cap...

Thanx alot though!

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

1

Barry Scheepers - 18/08/07

Super helpful! Got it running first time. Now if you could only set usage boundaries and have it email you when ... more

Log in


Sign up | Forgot your password?

What's on?