InfoInfo - vnStat

From PeformIQ Upgrade
Jump to navigation Jump to search

Monitor bandwidth with vnStat

Vincent Danen, TechRepublic

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.