VnStat PHP: A Web Based Interface for Monitoring Network Bandwidth Usage
Abstract: it creates a new database for eth0. Now run the following command to update all enabled databases or only specific interface with -i parameter as show
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.
Related Article: 16 Useful Bandwidth Monitoring Tools to Analyze Network Usage in LinuxThis article shows you how to install VnStat and VnStat PHP in Linux systems.
VnStat PHP PrerequisitesYou need to install the following software packages on your system.
- VnStat: A command-line network bandwidth monitoring tool, must be installed, configured, and should collect network bandwidth statistics.
- Apache: A Web Server to serve web pages.
- PHP: A server-side scripting language for executing php scripts on the server.
- php-gd extension: A GD extension for serving graphic images.
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.
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.
# yum install vnstat [On CentOS/RHEL] $ sudo apt-get install vnstat [On Debian/Ubuntu]
To install the most latest version of VnStat, follow this article – How to Install vnStat to Monitor Network Traffic in Linux.
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.
# vnstat -i eth0 Error: Unable to read database "/var/lib/vnstat/eth0". Info: -> A new database has been created.
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.
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.
# vnstat -u -i eth0
Next, add a crontab that runs every 5min and update the eth0 database to generate traffic statistics.
*/5 * * * * /usr/bin/vnstat -u >/dev/null 2>&1Step 2: Installing Apache, Php, and Php-gd Extension
Install the following software packages with the help of a package manager tool called 「yum」 for Red Hat-based systems and 「apt-get」 for Debian based systems.
On RHEL/CentOS and Fedora# yum install httpd php php-gd
Turn on Apache at system start-up and start the service.
# chkconfig httpd on # service httpd start
Run the following 「iptables」 command to open Apache port 「80」 on the firewall and then restart the service.
# iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT # service iptables restartOn Debian/Ubuntu and Linux Mint
$ sudo apt-get install apache2 php php-gd
$ sudo /etc/init.d/apache2 start
Open port 80 for Apache.
$ sudo ufw allow 80Step 3: Downloading VnStat PHP Frontend
Download the latest VnStat PHP source tarball file using 「wget command」 as shown below or visit THIS PAGE to grab the latest version.
# cd /tmp # wget http://www.sqweek.com/sqweek/files/vnstat_php_frontend-1.5.1.tar.gz
Extract the source tarball file, using 「tar command」 as shown given.
# tar xvf vnstat_php_frontend-1.5.1.tar.gzStep 4: Installing VnStat PHP Frontend
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.
On RHEL/CentOS and Fedora# cp -fr vnstat_php_frontend-1.5.1/ /var/www/html/vnstat
If SELinux enabled on your system, run the 「restorecon」 command to restore files default SELinux security contexts.
# restorecon -Rv /var/www/html/vnstat/On Debian/Ubuntu and Linux Mint
# cp -fr vnstat_php_frontend-1.5.1/ /var/www/vnstatStep 5: Configuring VnStat PHP Frontend
Configure it to match your setup. To do open the following file with VI editor and change the parameters as shown below.
On RHEL/CentOS and Fedora# vi /var/www/html/vnstat/config.phpOn Debian/Ubuntu and Linux Mint
# vi /var/www/vnstat/config.php
Set your default, Language.
// edit these to reflect your particular situation $locale = 'en_US.UTF-8'; $language = 'en';
Define your network interfaces to be monitored.
// list of network interfaces monitored by vnStat $iface_list = array('eth0', 'eth1');
You can set custom names for your network interfaces.
// optional names for interfaces // if there's no name set for an interface then the interface identifier. // will be displayed instead $iface_title['eth0'] = 'Internal'; $iface_title['eth1'] = 'External';
Save and close the file.
Step 6: Access VnStat PHP and View GraphsOpen 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.
http://localhost/vnstat/ http://your-ip-address/vnstat/Sample Output VnStat PHP Network Summary Reference Link
VnStat PHP Homepage