Graphical Bandwidth Monitor for Ubuntu
After having shifted to Ubuntu from Windows for quite a long time, I was looking for a no nonsense Bandwidth Monitor. It was during this time that I came across vnStat.
vnStat in the makers word :
vnStat is a network traffic monitor for Linux that keeps a log of daily network traffic for the selected interface(s). vnStat isn’t a packet sniffer. The traffic information is analyzed from the /proc filesystem. That way vnStat can be used even without root permissions. However, at least a 2.2 series kernel is required.
This program is open source/GPL’ed and can be installed either as root or as a single user. Better instructions are included in the README.
So I installed vnStat using
:~$sudo apt-get install vnstat
The first step in using vnStat is to initialize it and configure it to read your Internet bandwidth usage.
Since I use a wireless network and my wireless card is detected as wlan0 , I ran the following command
:~$sudo vnstat -u -i wlan0
to initialize the vnStat database. The database is stored in a file in the /var/lib/vnstat/ directory in a small binary file.
During installation vnStat sets up a cron job that refreshes the bandwidth consumption data on the wlan0(eth0 by default) port every five minutes. After letting the program run for a few minutes, type
vnstat
with no arguments. You will end up see something like this:
:~$vnstat
rx / tx / total / estimated
eth0 [disabled]:
27.03. 213.19 MB / 1.75 GB / 1.96 GB
today 186.04 MB / 2.29 GB / 2.47 GB / 8.93 GB
wlan0:
yesterday 1.77 GB / 1.91 GB / 3.68 GB
today 735.70 MB / 1.04 GB / 1.76 GB / 6.35 GB
Note : Since i am having two interfaces aka eth0 and wlan0 hence the outcome of vnstat is like that.
In case you have only one network inetrface you would have seen something like this
:~$vnstat Database updated: Sun Dec 2 22:20:01 2007 inet (eth0) received: 735.00 GB (48.9%) transmitted: 769.38 GB (51.1%) total: 1.47 TB rx | tx | total -----------------------+------------+----------- yesterday 23.35 MB | 20.25 MB | 43.60 MB today 27.47 MB | 21.37 MB | 48.84 MB -----------------------+------------+----------- estimated 29 MB | 22 MB | 51 MB
The above data is from vnStat homepage itself.
You can display your data in hourly, daily, or monthly increments using the -h, -d, and -m arguments.
In case you want to monitor a particular interface use the following command
vnstat -i [interfaceID]
In my case i would like to monitor the wireless adapter. So i used
:~$vnstat -i wlan0
The output was as follows :
:~$ vnstat -i wlan0 Database updated: Fri Apr 3 07:50:01 2009 wlan0 received: 3.49 GB (28.8%) transmitted: 8.63 GB (71.2%) total: 12.12 GB rx | tx | total -----------------------+------------+----------- yesterday 1.77 GB | 1.91 GB | 3.68 GB today 915.66 MB | 1.24 GB | 2.13 GB -----------------------+------------+----------- estimated 2.73 GB | 3.79 GB | 6.52 GB
Now since I was not satisfied with the appearance of vnStat’s command-line interface, I started searching for somethign more graphical and came across Bjorge Dijkstra’s PHP-based Web front end for vnStat, which takes the data collected by the command-line vnStat and displays it in tables and graphically in your browser. The front end requires a Web server configured with PHP.
As per vnStat PHP Frontend :
This is a PHP fronted end to vnStat, a network traffic logger. Since vnStat is console mode only I created this script to make a ‘nice’ report of the data collected by vnStat.
This script requires a working PHP setup with GD image libraries. Also vnStat must be properly installed and collecting data.
Since this required a Web Server and PHP to work i went ahead and downloaded XAMPP for Linux. Installation of XAMPP takes care of installing Apache, PHP, GD Image libraries. Just follow the instructions on the home page and you should be fine.
Next I downloaded the tarball of the vnStat PHP Frontend and extracted it to the htdocs location that was created by XAMPP aka
/opt/lampp/htdocs
using the command
:~$ sudo mkdir /opt/lampp/htdocs/vnstat :~$ sudo tar -zxvf /home/[Your_user_name]/[Your_Download_Location]/vnstat_php_frontend-1.2.1.tar.gz -C /opt/lampp/htdocs/vnstat/
Now we need to make some configurations.
:~$ cd /opt/lampp/htdocs/vnstat :~$ sudo gedit config.php
In the file find this line
$iface_list = array('eth0', 'wlan0', 'sixxs');
Make sure the array reflects all your adapters that you use. For me I had to change the second one from ‘eth1′ to ‘wlan0′. Next find these line
$iface_title['eth0'] = 'Internal'; $iface_title['wlan0'] = 'Internet'; $iface_title['sixxs'] = 'SixXS IPv6';
Make sure the parameters for the $iface_title match those in the array.
Next find the following line
$data_dir = './dumps';
Change the location to a folder on your home directory with write permissions. Save the file.
Now set up a cron job which will run the following command every 5 minutes continuously.
vnstat --dumpdb -i eth0 > /path/to/data_dir/vnstat_dump_$iface
where
/path/to/data_dir/ = the folder on your home directory with write permission
$iface = the network adapter that you want to monitor and should match with the array list in the config.php file just you had edited just above.
The cron job is set so that you always have the latest data with you w/o loosing it and also that you can use on your own and do what ever you like.
Now open up your browser and go to
http://localhost/vnstat/
and voila you are all ready with your Web based Graphical Bandwidth Monitor.


I cannot find an email address to send you some questions. Hence, the questions are here. I have tried to install vnstat_php_frontend-1.4.1. I was trying your line:
sudo tar -zxvf /home/[Your_Download_Location]/vnstat_php_frontend-1.2.1.tar.gz -C /opt/lampp/htdocs/vnstat/ [having changed 1.2.1 to 1.4.1 and pasting the vnstat_php_frontend into /lampp/htdocs/vnstat and running the tar from there. All seemed to go OK. But I could not find the config.php as you specified until I opened Gksudo Nautilus where it shows up in
/opt/lampp/htdocs/vnstat/vnstat_php_frontend-1.4.1
I changed the eth1 to wlan0 in 2 instances. Then I tried to find:
$data_dir = ‘./dumps’;
and mine is slightly differnt as it reads:
$data_dir = ‘./dumps’;
(not the leading quote mark is different) but after finding it, I cannot understand how to: “Change the location to a folder on your home directory with write permissions”. Would that be $data_dir = /home/’./dumps’; ?
Sorry for these beginner questions. I’m not a developer or computer literate. Some new neighbors moved in recently. As a kindness, I gave them a password for the wireless network I have. I just want to keep tabs on bandwidth usage. Thanks in advance for your help. M. Preston
Hi Mark,
Glad that you asked questions.
I am assuming that vnstat is up and running on your machine.
I haven’t installed vnstat_php_frontend-1.4.1. So it might be that the author has changed the directory structure.
As for the $data_dir question
First go to your home folder which would be /home/[Your_user_name]/
Now create a folder “.dumps” [the "." indicates that the folder will be hidden] with the following permission :
Owner : Read, Write, Execute
Group: Read, Execute
Others: Read, Execute
So on a command prompt do these two
:~$ cd /home/[Your_user_name]
:~$ mkdir .dumps
That should create a directory with Write permissions.
As a added measure what you can do is :
:~$ chmod 777 .dumps
This will ensure that everybody has got write access.
So for you it would be
$data_dir = '/home/[Your_user_name]/.dumps'
Hope this helps
In case of any further problems do post here. I will get back to you.
I’ve mkdir the /.dumps in my home folder (/home/mark/.dumps) & chmod 777s.
Then running
vnstat –dumpdb -i eth0 > $/path/to/data_dir/vnstat_dump_$iface
but as with wlan0, not eth0 and my .dumps in /home/mark:
vnstat –dumpdb -i wlan0 > $/home/mark/.dumps/vnstat_dump_$iface
but the terminal return is:
mark@Lexington-19-karmic:~$ vnstat –dumpdb -i wlan0 > $/home/mark/.dumps/vnstat_dump_$iface [won't all fit on one line in this Comment box]
bash: $/home/mark/.dumps/vnstat_dump_: No such file or directory
do I need to make a file (another folder?) named: vnstat_dump_ ?
Hi Mark,
Since you are using wlan0 run this command
vnstat –dumpdb -i wlan0 > /home/mark/.dumps/vnstat_dump_wlan0
This is because for you the variable $iface = wlan0.
And also sorry for the misprinted $ before the /path/to/data_dir/vnstat_dump_$iface.
Thanks for pointing that out, will rectify that shortly
Hope now you would be able to get an output.
hello
i like too, TOO much your how to..
i just installed vnstat on my ubuntu 8.04 and i’m just doing some tests.
seems that it work also with aliases (eth0:1 and eth0:2 and so on).
i’m just waiting to see a much larger db, next i will do the php tool trick
anyway, my question is.. without any use of mysql, i can i monitoring (as vnstat do) Ip and port?
I mean, i want collect and draw on a graphic, ALL my Inbound and Outbond from IP:port of mine server (aliases too obviously)..
Any suggest?
p.s. when i’ll finish with php, i’ll let you know.
Hi xer,
Glad that you liked the howto
Well I haven’t tried with aliases, but since you say its working, I will take it for granted
See if you want to show it the instant the php application is being accessed then yes, you wont need a MySQL installation.
But in case you want to retain all previous data then a DB of some sort is required. You can write the data in a flat file and then read back from it later.
In case you need further clarification feel free to ask back.
well… never mind, vnstat does not handle ip alias (ethx:x)
and i don’t wan use cacti or something else with a mysql db
What actually are you trying to do?
My turn to pick you brain on this one! im running ubuntu 9.10 and installed vnstat w/o a hitch. my problem is when i load the vnStat page i get
//for many lines ill do the following
Deprecated: Function split() is deprecated in /opt/lampp/htdocs/vnstat/vnstat.php on line 130
//then at the end ill give me this error
Warning: Cannot modify header information – headers already sent by (output started at /opt/lampp/htdocs/vnstat/vnstat.php:130) in /opt/lampp/htdocs/vnstat/index.php on line 146
it does collect the information correctly but will not show the graphical representation. so i know its pulling up the correct dump file or so i think. any ideas? thanks!
Hi QuPop,
Make sure you are using the latest version of vnstat front end. It appears that the version you are using is for php4 and you might be running php5.
A guess I suppose…
Thanks for sharing this helpful info!
Glad that you liked it…
Just checking out your article on my new Pure phone, and I wanted to check if it would let me reply or if it was going to me go to a desktop to do that. Ill check back later to see if it worked.
Thank you ! This was something I missed from Windows ( I had some freeware to monitor my bandwidth) but this one blows it out of the water. Thanks for the tutorial once again =)
Glad that you found it and liked it…
Thanks a ton, this is exactly what I was looking for!
Glad that you found the information useful
Hi,
It works fine. But I couldn’t get the graph. I only get datas. How can i make it?
THanks in advance…..
Padhu
Hi Padhu,
I am exactly not sure as to why the images are not appearing in your case.
Can you make sure the permissions on your web server are correct ?
Also as per vnStat PHP frontend
I’ve got the gui up but it is not in English how do I make it English?
Hi,
I thought that the script displayed the data in English only