Monitor Bandwidth Usage with vnStat

The command vnstat shows you bandwidth usage for interfaces on your system based on what's available and provided by the network piece of the kernel/kernel modules. You can configure it to log this information as well. This is helpful when using hosted services like DigitalOcean, who as of yet has not implemented graphs of this data in the dashboard, and you are charged for bandwidth over a certain amount.

Install vnStat

The vnStat rpm packages are on the EPEL Repo, so you'll need that setup on your server. Then you should be able to just install with yum.

yum -y install vnstat

Test Installation

Unless you're using a custom kernel, the dependencies should have been resolved with yum and the RPMs, but just to be certain, it can't hurt to run the tests.

vnstat --testkernel

The output of this should look something like..

[root@cht01 ~]# vnstat --testkernel
This test will take about 60 seconds.
[==============================] done.

Detected boot time variation during test: 0
Maximum boot time variation set in config: 15

The current kernel doesn't seem to suffer from boot time variation problems.
Everything is ok.

Create vnStat Database

vnStat will create its own database files located under /var/lib/vnstat (by default in /etc/vnstat.conf). We will want to create one for eth0 - which is the default if none are specified, but we'll specify it here as an example. If you try to run vnstat and the database doesnt exist, you'll get an error like this.

[root@cht01 ~]# vnstat -i eth1
Error: Unable to read database "/var/lib/vnstat/eth1".

 To create the database, we'll want to add the -u flag.

[root@cht01 ~]# vnstat -i eth1 -u
Error: Unable to read database "/var/lib/vnstat/eth1".
Info: -> A new database has been created.

Gather Bandwidth Data

After the initial interface database creation, we'll need to wait and let it get enough data to compute. Otherwise, if you go to check the stats you'll get a "Not enough data available yet." type of message. This is normal, just give it a bit.

Check Bandwidth Data

After enough time has passed, you can check the bandwidth. We'll specify eth1 in the example. If you don't specify any arguments, any interfaces that have databases will be shown.

vnstat -i eth1

You should see some output like this... It will tell you the last time the database file was updated, when the database file was created and started gathering data, rx, tx, and total bandwidth for that duration. Then below it will break it up monthly and daily.

[root@cht01 ~]# vnstat -i eth1
Database updated: Wed Feb 4 15:19:01 2015

eth1 since 12/08/14
rx: 7.48 GiB tx: 53.69 GiB total: 61.17 GiB

rx | tx | total | avg. rate
Jan '15 4.43 GiB | 31.52 GiB | 35.95 GiB | 112.59 kbit/s
Feb '15 494.33 MiB | 3.93 GiB | 4.41 GiB | 117.66 kbit/s
estimated 3.71 GiB | 30.21 GiB | 33.92 GiB |

rx | tx | total | avg. rate
yesterday 127.48 MiB | 1.22 GiB | 1.34 GiB | 130.38 kbit/s
today 94.43 MiB | 632.46 MiB | 726.90 MiB | 107.99 kbit/s
estimated 147 MiB | 990 MiB | 1.11 GiB |

Run from Cron or as Daemon

We will want to regularly update this database file. To do this, we have two options, periodically run a cronjob to update the database by calling the included bash script /usr/sbin/vnstat.cron, or by specifying -d to run it in the background as a daemon. You might want to use supervisord or upstart to keep it running if you go that way - there is an included init.d script but I was having issues with it by default that I have not yet dug into.

vnStat Cron Entry

I have a crontab entry to run it every minute that looks like this.

* * * * * /usr/sbin/vnstat.cron

This script looks like this, it basically just sources VNSTAT_OPTIONS from /etc/sysconfig/vnstat ($VNSTAT_CONF). The options mentioned are the default.

# this script (/usr/sbin/vnstat.cron) reads /etc/sysconfig/vnstat
# to start /usr/bin/vnstat.
# example for /etc/sysconfig/vnstat:
# VNSTAT_OPTIONS="-u -i eth0"
# see also: vnstat(1)


if [ ! -f ]; then
exit 0


/usr/bin/vnstat $VNSTAT_OPTIONS

Show Available Interfaces

If you do a vnstat --iflist, you can see the available interfaces.

[root@cht01 ~]# vnstat --iflist
Available interfaces: lo eth0 eth1

vnStat Configuration Files

Aside from the mentioned /etc/sysconfig/vnstat, there is also /etc/vnstat.conf which is well documented and pretty self explanatory in the comments.