Wednesday, January 28, 2009

vnstat Console Network Traffic Monitor

Many thanks to Nick Anderson for this blog entry!


Matt was kind enough to ask me to spice things up a bit while he is gone on vacation.(well kind to me, perhaps not so kind to his readers).

If you have ever wanted to monitor network bandwidth I'm sure your familiar with mrtg. I find it useful to be able to do things from the console whenever possible. vnstat fills the void for simple bandwidth monitoring. Since vnstat pulls its information from the /proc filesystem it can be used without root permissions.

To install vnstat on debian just do

[code=bash]
sudo aptitude install vnstat
[/code]

Now you need to create the databases for it to log to. For each interface you want to monitor do the following.

[code=bash]
sudo vnstat -u -i iface
[/code]

After you have created the databases for each interface you want to monitor check to ensure the cron entry was setup correctly. You should have a file in /etc/cron.d called vnstat and its contents should be similar to the following.

[code=bash]
# /etc/cron.d/vnstat: crontab entries for the vnstat package

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

Now all you have to do is wait. After some time you can get interesting output like this.


[code=bash]
vnstat --days -i wlan0

wlan0 / daily

day rx | tx | total
------------------------+-------------+----------------------------------------
05.01. 9.69 MB | 1.84 MB | 11.53 MB
06.01. 410.54 MB | 23.72 MB | 434.26 MB %
07.01. 409.89 MB | 25.83 MB | 435.71 MB %
08.01. 68.63 MB | 12.91 MB | 81.54 MB
09.01. 9.75 MB | 3.73 MB | 13.49 MB
10.01. 292.16 MB | 39.78 MB | 331.94 MB %
11.01. 178.23 MB | 1.57 GB | 1.74 GB %:::::
12.01. 350.97 MB | 1.60 GB | 1.94 GB %:::::
13.01. 23.65 MB | 5.89 MB | 29.54 MB
14.01. 410.56 MB | 1.61 GB | 2.01 GB %::::::
15.01. 440.17 MB | 1.62 GB | 2.05 GB %::::::
16.01. 241.75 MB | 26.32 MB | 268.07 MB
17.01. 713.33 MB | 79.22 MB | 792.56 MB %%
18.01. 5.40 GB | 1.64 GB | 7.05 GB %%%%%%%%%%%%%%%%%%%::::::
19.01. 168.10 MB | 1.45 GB | 1.61 GB %::::
20.01. 19.27 MB | 9.64 MB | 28.91 MB
------------------------+-------------+----------------------------------------
estimated 21 MB | 10 MB | 31 MB

vnstat --months -i wlan0

wlan0 / monthly

month rx | tx | total
-------------------------+--------------+--------------------------------------
Jan '09 9.06 GB | 9.72 GB | 18.78 GB %%%%%%%%%%%:::::::::::
-------------------------+--------------+--------------------------------------
estimated 14.13 GB | 15.16 GB | 29.30 GB
[/code]

Hopefully its something you can add to your command line toolbox.