vnStat is a very simple utility that can track the amount of bandwidth used by your web server. It works by monitoring the received and transmitted bytes counters of your network interface and logging the data in a database. In this post I look at how to install vnStat and a web-based front end on a CentOS Linux web server.
Installing vnStat
- Log in as root: To install vnStat you must log in as root.
-
Add RPM repository: vnStat is not available on the default CentOS repositories. So you will have to add the 3rd part repository maintained by Dag Wieers. You do that using the latest rpmforge release. For example:
32bit CentOS 5:
rpm -Uhv http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
64 bit CentOS 6:
rpm -Uhv http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
-
Install using yum: The next step is to simply use yum to install the vnStat package:
yum install vnstat
-
Setup database: Before vnStat can begin logging bandwidth usage you need to tell it to setup the database for your network interface. Replace eth0 with the correct interface:
vnstat -u -i eth0
If your using an openvz vps your network interface is usually venet0 or venet1. Doing an
ifconfig
will help you identify it.
-
Setup correct permissions: vnStat updates its statistics automatically every 5 minutes via a cron job. By default the RPM install sets the permissions for this cron job incorrectly. So edit the /etc/cron.d/vnstat file and replace “nobody” with “root”:
MAILTO=root
*/5 * * * * root /usr/sbin/vnstat.cron
-
Setup correct interface in config file: By default the vnStat cron job will attempt to collect statistics for the eth0 interface. If your network interface is different from that one you will have to modifiy the /etc/sysconfig/vnstat file:
VNSTAT_OPTIONS="-i eth3"
-
Setup bash alias: Logout of root now and back to your regular user account. If you intend to use vnStat over the command line and your network interface is not eth0 you should setup a bash alias for ease of use in your .bashrc file:
alias vnstat="vnstat -i eth3"
Thats about it for the setup.
Using vnStat
Using vnStat is easy since its a simple program.
-
Statistics by day
vnstat -d
eth3 / daily day rx | tx | total ------------------------+-------------+---------------------------------------- 22.02. 49.88 GiB | 55.70 GiB | 105.58 GiB %%%%%::::: 23.02. 117.31 GiB | 132.10 GiB | 249.42 GiB %%%%%%%%%%%%::::::::::::: ------------------------+-------------+---------------------------------------- estimated 152.19 GiB | 171.37 GiB | 323.56 GiB
-
By hour
vnstat -h
eth3 18:30 ^ t | t t rt rt t | t rt rt rt rt rt t | rt rt rt rt rt rt rt | rt rt rt rt rt rt rt t | t rt rt rt rt rt rt rt rt | t t rt rt rt rt rt rt rt rt rt | rt rt rt rt rt rt rt rt rt rt rt rt rt rt | t rt rt rt rt rt rt rt rt rt rt rt rt rt rt rt rt rt rt | rt rt rt t rt rt rt rt rt rt rt rt rt rt rt rt rt rt rt rt rt rt rt t -+---------------------------------------------------------------------------> | 19 20 21 22 23 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 h rx (KiB) tx (KiB) h rx (KiB) tx (KiB) h rx (KiB) tx (KiB) 19 1,856,484 2,162,308 03 3,901,273 4,899,191 11 11,237,321 12,284,756 20 2,102,528 2,310,322 04 4,834,759 5,745,156 12 9,588,273 10,660,528 21 1,348,942 1,525,638 05 6,264,453 7,334,658 13 7,382,067 8,082,776 22 1,089,267 1,301,581 06 9,527,424 10,775,408 14 4,160,481 4,556,939 23 2,252,512 2,747,903 07 10,183,329 11,395,931 15 3,272,184 3,542,964 00 3,024,305 3,583,757 08 10,959,014 12,136,572 16 2,676,814 3,010,420 01 4,063,322 4,938,705 09 11,380,338 12,376,197 17 3,018,233 3,421,954 02 4,764,074 5,811,465 10 11,522,942 12,541,008 18 1,252,560 1,419,053
-
By month
vnstat -m
-
Live monitoring of the network interface
vnstat -l
Monitoring eth3... (press CTRL-C to stop) rx: 617.39 KiB/s 756 p/s tx: 582.82 KiB/s 814 p/s
vnStat PHP based web frontend
You may find it inconvenient to have to login to your server every time you want to see your server’s bandwidth statistics. To make it easier to see bandwidth stats one kind soul made a PHP frontend that uses vnStat to obtain the necessary data and display it with pretty graphs.
vnStat php frontend requires that you have apache, php and the php-gd image processing library installed. Installing the latter is as easy as running the following command as root:
yum install php-gd
Installing vnStat PHP is pretty straight forward and can be done as a regular user:
-
Download it, untar it and place it in a web accessible place like /var/www/html:
wget http://www.sqweek.com/sqweek/files/vnstat_php_frontend-1.4.1.tar.gztar -xzf vnstat_php_frontend-1.4.1.tar.gz
mkdir /var/www/html/vnstat
mv vnstat_php_frontend/* /var/www/html/vnstat
-
Edit the config.php file: Just set the correct binary name:
$vnstat_bin = 'vnstat';
Then just view the page in your browser by visiting yourdomain.com/vnstat ! You’ll get pretty pictures like below
vnStat is a must have utility for monitoring your system’s bandwidth usage. Its simple to install and use and yet can provide valuable insight.
Thanks – a really useful post, I gave up with trying to figure this out from the vnstat homepage.
I installed from the epel repository and I didn’t have to replace nobody with root in /etc/cron.d/vnstat but I did have to uncomment the line below to enable the monitoring.
Rather than setting a bash alias, if you want to change the default interface system wide, simply edit /etc/vnstat.conf and change:
Interface “eth0”
to
Interface “eth3”
I should also mention, the EPEL package installs the /etc/cron.d/vnstat file with the vnstat command commented out. So you’ll need to edit /etc/cron.d/vnstat and uncomment the command to get the 5 minute updates.
Oh, and the EPEL package also creates and uses a new user ‘vnstat’, so no need to modify the user in the cron file.
Awesome post, thank you for making it a little easier to use Linux!
Thanks for this post man, it really helped me out! Keep feeding us with topics like that.
BR
Useful post. In my case I had to restart the cron service after the configurations.
“service crond restart”
Thanks for this, it really helped me. For anyone interested, please note that the latest version of the php frontend is now 1.5.1
Glad I could help 🙂
how to configure vnstat to log 2 or more network card, i.e eth0, eth1, eth1:1. thanks
Setup an additional cron job to run vnstat -u -i {interface}
Hi.
On CentOS 5.5 i did not need to run it as root. However make sure that the databases in /var/libvnstat are owned by nobody.nobody after creating them with the -i -u parameters.
Also for setting up monitoring for multiple databases i did not have to make multiple cron jobs or configs. Just creating the databases is enough.
Thanks for the tutorial.
How can I configure it to use with nginx instead of apache
hi,
i’m already install vnstat frontend on my linux server with nginx. then i access from my browser, i got error like this:
Warning: popen() has been disabled for security reasons in /home/itera/itera.ac.id/mon/vnstat/vnstat.php on line 111
Warning: feof() expects parameter 1 to be resource, null given in /home/itera/itera.ac.id/mon/vnstat/vnstat.php on line 113
Warning: fgets() expects parameter 1 to be resource, null given in /home/itera/itera.ac.id/mon/vnstat/vnstat.php on line 114
–skip–
can you help me?
Sorry about the very late reply. You need to look at the disabled functions in your php.ini file. popen has been disabled there for security reasons.