Vnstat Bandwidth/Network Monitoring Tool For Linux – Configure Vnstat With Web Interface

By | September 14, 2009

Monitoring bandwidth of the network, gateway and servers are one of important day to day activity of System Administrator. Bandwidth monitoring can help us to monitor the activities on network. We can easily monitor the users bandwidth usage and control their activities. Monitoring help us keep aware of the each day activities on our network, thus help to prevent harmful activities inside the network eg: torrent download.

Their are many techniques to control and monitor the bandwidth usage. Here i like to explain a simple monitoring tool called “Vnstat”. Vnstat help us to monitor the bandwidth usage on each network interfaces. Operating and monitoring using vnstat is simple. We can also view the usage using web interface.

Vnstat tool can able to calculate the total bandwidth usage on each interface.

Configure Vnstat On Linux

1. Download Vnstat tar.gz.

# cd /donwloadlocation
# wget http://humdi.net/vnstat/vnstat-1.9.tar.gz

**Vnstat is also available via yum installation using rpmforge repo

2. Extract downloaded vnstat package.

# tar -xvzf vnstat-1.9.tar.gz
# cd vnstat-1.9/

3. Compiling the binaries.

# make all

*Required extra packages are usually named libgd2 and libgd2-dev (or libgd2-noxpm and libgd2-noxpm-dev in Debian/Ubuntu, xpm version can also be used)

4. Installing vnstat.

Login as root and run the following command,

# make install

5. Test Kernel compatibility.

# vnstat –testkernel

If the kernel is not compatible we need to upgrade the kernel.

6. Check the interface availability.

# vnstat –iflist

eg: Available interfaces: lo eth0 sit0

7. Create new databases for the monitoring interface.

# vnstat -u -i eth0

Repeat that for every other interface you wish to use.

eg: # vnstat -u -i eth0

Error: Unable to read database “/var/lib/vnstat/eth0″.

Info: -> A new database has been created.

**Set write permission for interface database files inside “/var/lib/vnstat/”

8. Run Vnstat via cron.

Edit the crontab and add the following line,

# crontab -e

Add the line into crontab

*/5 * * * * /usr/bin/vnstat -u

The script will update vnstat database in every five minutes. We can also define the same vnstat cron on /etc/cron.d/ folder. Only one cron definition is required.

# vi /etc/cron.d/vnstat

Add the same line

*/5 * * * * /usr/bin/vnstat -u

9. Reload crond

# /etc/init.d/crond reload

10. View the Bandwidth usage.

Run the command.

# vnstat

Configure Web Interface for Vnstat

1. Download the vnstat php frontend

# cd /tmp
# wget http://www.sqweek.com/sqweek/files/vnstat_php_frontend-1.4.tar.gz

2. Extract the package.

# tar -xvzf vnstat_php_frontend-1.4.tar.gz

3. Move the package to html doc root.

# mv vnstat_php_frontend-1.4 /var/www/html/vnstat

* Apache must configure to handle php request.

4. Edit config.php

Specify the network interface,

$iface_list = array(‘eth0′, ‘eth1′);

Specify interface function on the following line,

$iface_title[‘eth0′] = ‘Internal';
$iface_title[‘eth1′] = ‘Internet';

Comment the line,

//$iface_title[‘sixxs’] = ‘SixXS IPv6′;

Specify the vnstat command. Use which command to locate the path.

# which vnstat

$vnstat_bin = ‘/usr/bin/vnstat';

Now open browser and browser the url eg: http://bdgw2.talk2melbin.com. Secure the root directory using .htaccess password protection.

Cheers!
Melbin Mathew
www.talk2melbin.com

Melbin Mathew

I am Melbin Mathew, Systems Engineer from Kerala, India. I live with my parents, daughter and my wonderful wife. I graduated in 2004 with a bachelor’s degree in Electronics and Hardware from Mahatma Gandhi University, Kottayam and completed certification in MCITP, RHCE, CCNA, VCP ...Read More
Category: Linux

About Melbin Mathew

I am Melbin Mathew, Systems Engineer from Kerala, India. I live with my parents, daughter and my wonderful wife. I graduated in 2004 with a bachelor’s degree in Electronics and Hardware from Mahatma Gandhi University, Kottayam and completed certification in MCITP, RHCE, CCNA, VCP ...Read More

  • http://humdi.net/vnstat/ Teemu Toivola

    You might want to check the INSTALL documentation that comes with the source package again. :)

    LibGD2 is only required if the image output is compiled and that’s not done by default. It’s done with ‘make all’ instead of only ‘make’. The cgi ( examples/vnstat.cgi ) can be used if image output is available and it doesn’t require the webserver to support php.

    The daemon is also usually a better option instead of the cron entry and it provides more configurability.

    • http://www.talk2melbin.com Melbin Mathew

      Thank you for pointing me the mistake. I have updated the post with “make all” command. I would like to check the daemon working, Hope soon i am able to update the post with vnstat daemon configuration.