Monitoring bandwidth usage with vnstat on CentOS

For more advanced setups and breakdowns you should really use MRTG, but for those who just want to know their daily bandwidth consumption this 2 minute tip  to setup vnstat will suffice.

You can add the EPEL yum repository to download vnstat and many other tools, but if you only need this one tool or have requirements not to include EPEL you can just download the single RPM from http://dl.fedoraproject.org/pub/epel/6/x86_64/repoview/vnstat.html

For example, in my case the lastest was:

wget http://dl.fedoraproject.org/pub/epel/6/x86_64/vnstat-1.11-1.el6.x86_64.rpm

which can be installed using

rpm -iv vnstat-1.11-1.el6.x86_64.rpm

Now you just need to populate the database for the interfaces you need to monitor. You can look at the current interfaces with ifconfig

In my case I’m using em1  ( More information can be found here Consistent Network Device naming on Fedora / RHEL )

so you can execute: vnstat -u -i em1
This yields the following output:

Error: Unable to read database “/var/lib/vnstat/em1”.
Info: -> A new database has been created.

All that remains at this point is to setup a cronjob which takes care of inserting the values into the crontab:

@daily /usr/bin/vnstat -h
0-55/5 * * * * /usr/bin/vnstat -u

The first is a report sent to root every day (optional!) and the second is a job that will update the vnstat database every 5 minutes