VnStat PHP: A Web Based Interface for Monitoring Network Bandwidth Usage

[‘

n

VnStat PHP a graphical interface application for most famous console mode network logger utility called “vnstat“. This VnStat PHP is a graphical frontend to VnStat, to view and monitor network traffic bandwidth usage report in nicely graphical format. It displays IN and OUT network traffic statistics in hourly, days, months, or full summary.

n

n

This article shows you how to install VnStat and VnStat PHPxa0 in Linux systems.

n

VnStat PHP Prerequisites

n

You need to install the following software packages on your system.

n

    n

  • VnStat: A command-line network bandwidth monitoring tool, must be installed, configured, and should collect network bandwidth statistics.
  • n

  • Apache: A Web Server to serve web pages.
  • n

  • PHP: A server-side scripting language for executing php scripts on the server.
  • n

  • php-gd extension: A GD extension for serving graphic images.
  • n

n

Step 1: Installing and Configuring VnStat Command Line Tool

n

VnStat is a command-line network bandwidth monitoring utility that counts bandwidth (transmit and received) on network devices and keeps the data in its own database.

n

Vnstat is a third-party tool and can be installed via enabling epel repository under Red Hat-based systems. Once you’ve enabled, you can install it using the yum command as shown below.

n

# yum install vnstat              [On CentOS/RHEL]rn$ sudo apt-get install vnstat     [On Debian/Ubuntu]rn

n

To install the most latest version of VnStat, follow this article – How to Install vnStat to Monitor Network Traffic in Linux.

n

As I said Vnstat maintains its own database to keep all network information. To create a new database for network interface called “eth0“, issue the following command. Make sure to replace the interface name as per your requirements.

n

# vnstat -i eth0rnrnError: Unable to read database "/var/lib/vnstat/eth0".rnInfo: -> A new database has been created.

n

If you get the above error, don’t worry about such error, because you are executing the command first time. So, it creates a new database for eth0.

n

Now run the following command to update all enabled databases or only specific interface with -i parameter as shown. It will generate traffic statistics of IN and OUT of an eth0 interface.

n

# vnstat -u -i eth0

n

Next, add a crontab that runs every 5min and update the eth0 database to generate traffic statistics.

n

*/5 * * * * /usr/bin/vnstat -u >/dev/null 2>&1

n

Step 2: Installing Apache, Php, and Php-gd Extension

n

Install the following software packages with the help of a package manager tool called “yum” for Red Hat-basedxa0systems and “apt-get” for Debian based systems.

n

On RHEL/CentOS and Fedora

n

# yum install httpd php php-gd

n

Turn on Apache at system start-up and start the service.

n

# chkconfig httpd onrn# service httpd start

n

Run the following “iptables” command to open Apache port “80” on the firewall and then restart the service.

n

# iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPTrn# service iptables restart

n

On Debian/Ubuntu and Linux Mint

n

$ sudo apt-get install apache2 php php-gd

n

$ sudo /etc/init.d/apache2 start

n

Open port 80 for Apache.

n

$ sudo ufw allow 80

n

Step 3: Downloading VnStat PHP Frontend

n

Download the latest VnStat PHP source tarball file using “wget command” as shown below or visit THIS PAGE to grab the latest version.

n

# cd /tmprn# wget http://www.sqweek.com/sqweek/files/vnstat_php_frontend-1.5.1.tar.gz

n

Extract the source tarball file, using “tar command” as shown given.

n

# tar xvf vnstat_php_frontend-1.5.1.tar.gz

n

Step 4: Installing VnStat PHP Frontend

n

Once extracted, you will see a directory called “vnstat_php_frontend-1.5.1“. Copy the contents of this directory to the webserver root location as directory vnstat as shown below.

n

On RHEL/CentOS and Fedora

n

# cp -fr vnstat_php_frontend-1.5.1/ /var/www/html/vnstat

n

If SELinux enabled on your system, run the “restorecon” command to restore files default SELinux security contexts.

n

# restorecon -Rv /var/www/html/vnstat/

n

On Debian/Ubuntu and Linux Mint

n

# cp -fr vnstat_php_frontend-1.5.1/ /var/www/vnstat

n

Step 5: Configuring VnStat PHP Frontend

n

Configure it to match your setup. To do open the following file with VI editor and change the parameters as shown below.

n

On RHEL/CentOS and Fedora

n

# vi /var/www/html/vnstat/config.php

n

On Debian/Ubuntu and Linux Mint

n

# vi /var/www/vnstat/config.php

n

Set your default, Language.

n

// edit these to reflect your particular situationrn$locale = 'en_US.UTF-8';rn$language = 'en';

n

Define your network interfaces to be monitored.

n

// list of network interfaces monitored by vnStatrn$iface_list = array('eth0', 'eth1');

n

You can set custom names for your network interfaces.

n

// optional names for interfacesrn// if there's no name set for an interface then the interface identifier.rn// will be displayed insteadrn$iface_title['eth0'] = 'Internal';rn$iface_title['eth1'] = 'External';

n

Save and close the file.

n

Step 6: Access VnStat PHP and View Graphs

n

Open your favorite browser and navigate to any of the following links. Now you will see a fancy network graph that shows you a summary of network bandwidth usage in hours, days, and months.

n

http://localhost/vnstat/rnhttp://your-ip-address/vnstat/

n

Sample Output

n

Install Vnstat PHP in Linux
VnStat PHP Network Summary

n

Reference Link

n

VnStat PHP Homepage

n

‘]