How-To: monitor network bandwidth usage with vnstat

2 minute read

There is many tools out there that help in monitoring network usage, collect statistics and generate graphs so we can view what happened at a given date/time. Anyhow, finding the bandwidth usage over an hour/day/week/month can be really tricky.

vnstat is a suite of daemon and client programs that monitor network bandwidth usage.

A pro of vnStat is that it uses the values from /proc/net/dev to calculate bandwidth usage. Which means that it saves the resources that would be needed if it was sniffing the network and that it can be ran as a unprivileged user.

Installation

To install vnStat on Debian, you can use apt-get:

# apt-get install vnstat

Unfortunately, while the service will be unable after the package had been installed, it will fail with an error similar to:

Starting vnStat daemon: vnstatdZero database found, exiting.
failed!

To fix this, we will need to create an empty database for vnStat by using:

# vnstat -u -i eth0
Error: Unable to read database “/var/lib/vnstat/eth0″.
Info: -> A new database has been created.

Now that the database is created, you can restart the vnStat daemon vnstatd with:

# /etc/init.d/vnstat restart
Starting vnStat daemon: vnstatd

If you need to configure vnStat you can go and edit /etc/vnstat.conf .

Usage

Now, let’s wait a bit for data to be collected and let’s run the command:

# vnstat
Database updated: Mon Mar 11 06:49:58 2013

eth0 since 03/11/13

rx: 508.83 MiB tx: 554.87 MiB total: 1.04 GiB

monthly
rx | tx | total | avg. rate
------------------------+-------------+-------------+---------------
Mar '13 508.83 MiB | 554.87 MiB | 1.04 GiB | 9.81 kbit/s
------------------------+-------------+-------------+---------------
estimated 1.50 GiB | 1.63 GiB | 3.12 GiB |

daily
rx | tx | total | avg. rate
------------------------+-------------+-------------+---------------
today 508.83 MiB | 554.87 MiB | 1.04 GiB | 354.25 kbit/s
------------------------+-------------+-------------+---------------
estimated 1.75 GiB | 1.90 GiB | 3.65 GiB |

If the daemon had been running for more than a month, we would have got the summary for the current and the previous day and month.

To see the summary of the hourly usage use:

$ vnstat -h

The daily usage:

$ vnstat -d

The weekly usage:

$ vnstat -w

and the monthly usage:

$ vnstat -m

Some other useful information that we can get out of vnStat are the top 10 days:

$ vnstat -t

usage over X seconds (5 if no value provided):

$ vnstat -tr 10

and finally live bandwidth usage:

$ vnstat -l

vnStat is a useful little tool to get a good overview of how much bandwidth you use on a hourly/daily/weekly/monthly basis and requires almost no resources to do so.