Skip to content

Keep track of your bandwidth usage using vnstat on Ubuntu

2009 July 12
by Hardik Dalwadi

I have already covered My User Experience with Reliance Netconnect – Broadband+ service and Ubuntu 9.04. Now as i said in previous article it’s very hard to track bandwidth usage with Reliance Netconnect – Broadband+ service using on-line facility  Reliance Mycare. I don’t have any single word to praise that service. We all know the Reliance Netconnect – Broadband+ service is not offering any unlimited bandwidth usage package. So INTERNET  savvy should keep track their bandwidth usage or else you would be jump in 5 digit monthly bill (Imagine @ 2 INR / MB). Which nobody can afford. I have 10 GB Day + 10 GB Night plan.

So, here is my way to keep track of bandwidth usage using vnstat in Ubuntu.

Q. What is vnstat?
A. vnStat is a console-based network traffic monitor for Linux and BSD that keeps a log of network traffic for the selected
    interface(s).

Simply install the vnstat from Ubuntu repository with you favorite installation method ( synaptic / aptitude / apt-get / …). I am apt-get fan.

person@CANONICALDESK:~$ sudo apt-get install vnstat

Now, as i said i want to keep track of bandwidth usage of my Reliance Netconnect Broadband+ service. Which is always running to ‘ppp0’ interface. So first create the database of ‘ppp0’ for vnstat.

person@CANONICALDESK:~$ sudo vnstat -u -i ppp0

Error:

Unable to read database "/var/lib/vnstat/ppp0".

-> A new database has been created.

person@CANONICALDESK:~$

You can also discover more option using “vnstat -h”. Now every five minutes vnstat will run through cron to track the detail usage of bandwidth (TX + RX @ ppp0). I am not sure when i am going to shutdown / hibernate / sleep my machine. Still i want as much accurate result as i can. Because of that i have decreased the default 5 minutes schedule time to 1 minute for vnstat. Here is my modified vnstat cron script.

person@CANONICALDESK:~$ cat /etc/cron.d/vnstat

# /etc/cron.d/vnstat: crontab entries for the vnstat package

* *	* * *	root	if [ -x /usr/bin/vnstat ] && [ `ls /var/lib/vnstat/ | wc -l` -ge 1 ]; then /usr/bin/vnstat -u; fi

person@CANONICALDESK:~$

Time to take reports. We can also check / compare bandwidth usage with Reliance Mycare on-line report, which never generated for me :(

Monthly Rport:

person@CANONICALDESK:~$ vnstat -i ppp0 -m

 ppp0  /  monthly

   month         rx      |      tx      |   total
-------------------------+--------------+--------------------------------------
  Jul '09       1.00 GB  |    63.47 MB  |     1.06 GB   %%%%%%%%%%%%%%%%%%%%%:
-------------------------+--------------+--------------------------------------
 estimated      2.69 GB  |      169 MB  |     2.85 GB
person@CANONICALDESK:~$ 

Weekly Report:

person@CANONICALDESK:~$ vnstat -i ppp0 -w

	ppp0  /  weekly

	                    rx      |       tx      |    total
	----------------------------+---------------+--------------
	  last 7 days    418.01 MB  |     39.00 MB  |    457.01 MB
	    last week    606.37 MB  |     24.46 MB  |    630.83 MB
	 current week    418.01 MB  |     39.00 MB  |    457.01 MB
	----------------------------+---------------+--------------
	    estimated       447 MB  |        41 MB  |       488 MB
person@CANONICALDESK:~$ 

Daily Report:

person@CANONICALDESK:~$ vnstat -i ppp0 -d

 ppp0  /  daily

    day         rx      |     tx      |  total
------------------------+-------------+----------------------------------------
   03.07.    251.93 MB  |   11.99 MB  |  263.92 MB   %%%%%%%%%%%%%%%%:
   04.07.    354.43 MB  |   12.48 MB  |  366.91 MB   %%%%%%%%%%%%%%%%%%%%%%%%:
   09.07.    140.25 MB  |   19.11 MB  |  159.36 MB   %%%%%%%%%:
   10.07.     60.12 MB  |   10.25 MB  |   70.37 MB   %%%:
   11.07.    148.81 MB  |    6.27 MB  |  155.08 MB   %%%%%%%%%%
   12.07.     68.83 MB  |    3.39 MB  |   72.22 MB   %%%%
------------------------+-------------+----------------------------------------
 estimated      124 MB  |       5 MB  |     129 MB
person@CANONICALDESK:~$ 

Hourly Report:

person@CANONICALDESK:~$ vnstat -i ppp0 -h
 ppp0                                                                     13:09 
  ^                                                                    r        
  |                                                                    r        
  |                                                                    r        
  |                                                                    r        
  |                                                                    r        
  |                                                                    r        
  |                                                                    r        
  |                                                                    r        
  |                                                                    r        
  |                                                                    r        
 -+---------------------------------------------------------------------------> 
  |  14 15 16 17 18 19 20 21 22 23 00 01 02 03 04 05 06 07 08 09 10 11 12 13    
                                                                                
 h   rx (kB)    tx (kB)      h   rx (kB)    tx (kB)      h   rx (kB)    tx (kB) 
14          0          0    22          0          0    06          0          0
15          0          0    23          0          0    07          0          0
16          0          0    00          0          0    08          0          0
17          0          0    01          0          0    09          0          0
18          0          0    02          0          0    10          0          0
19          0          0    03          0          0    11          0          0
20          0          0    04          0          0    12      70595       3506
21          0          0    05          0          0    13        311        108
person@CANONICALDESK:~$ 

I think above reports are sufficient to make argument against unfair bill right ;). There is also web base support for this application. Please do “Google” for more information.

Update:

If you want to stick with latest version of vnstat. Then please follow this

6 Responses leave one →
  1. September 1, 2009

    Hi Hardik
    I’ve just installed vnstat in ubuntu, and it seems to work fine; what is not clear to me yet is: say that vnstat runs in cron (default 5 minutes), and after 4 minutes the connection is lost (or disconnected), will the traffic in that 4 minutes be lost (i.e., not registered by vnstat)?
    thanks in advance
    Lorenzo

  2. October 9, 2009

    Hi Lore,

    I had same question. But then i have changed my /etc/cron.d/vnstat
    script to run at every one minute.

    person@CANONICALDESK:~$ cat /etc/cron.d/vnstat
    # /etc/cron.d/vnstat: crontab entries for the vnstat package

    * * * * * root if [ -x /usr/bin/vnstat ] && [ `ls /var/lib/vnstat/ | wc
    -l` -ge 1 ]; then /usr/bin/vnstat -u; fi
    person@CANONICALDESK:~$

    Here is my log from /var/log/syslog:

    Sep 4 13:40:01 CANONICALDESK /USR/SBIN/CRON[18331]: (root) CMD (if [ -x
    /usr/bin/vnstat ] && [ `ls /var/lib/vnstat/ | wc -l` -ge 1 ]; then
    /usr/bin/vnstat -u; fi)
    Sep 4 13:41:01 CANONICALDESK /USR/SBIN/CRON[18541]: (root) CMD (if [ -x
    /usr/bin/vnstat ] && [ `ls /var/lib/vnstat/ | wc -l` -ge 1 ]; then
    /usr/bin/vnstat -u; fi)
    Sep 4 13:42:01 CANONICALDESK /USR/SBIN/CRON[18584]: (root) CMD (if [ -x
    /usr/bin/vnstat ] && [ `ls /var/lib/vnstat/ | wc -l` -ge 1 ]; then
    /usr/bin/vnstat -u; fi)

    Any, Yes you will lost your traffic. because it will not update your
    database in /var/lib/vnstat directory

    person@CANONICALDESK:~$ ls -l /var/lib/vnstat/
    total 12
    -rw-r–r– 1 root root 2272 2009-09-04 13:44 eth0
    -rw-r–r– 1 root root 2272 2009-09-04 13:44 ppp0
    -rw-r–r– 1 root root 2272 2009-09-03 11:35 wlan0
    person@CANONICALDESK:~$

    Note: Next version of vnstat may take care of such things (That is not
    available in Ubuntu Jaunty (9.04) repository). It will give deamon mode
    support with vnstat.conf [1] configuration file. It have token facility
    (UpdateInterval = How often in seconds the interface data is updated).
    Check more on official site [2].

    [1] http://humdi.net/vnstat/man/vnstat.conf.html
    [2] http://humdi.net/vnstat/

  3. October 11, 2009

    Hardik, thanks for publishing the link to my post as an update :)

    vnstat really rocks!

  4. bluelight permalink
    December 3, 2009

    Thx for this post. This tool is really easy to set up and use.

  5. Mainak Kundu permalink
    April 29, 2012

    Thanks for the post..really useful.

  6. Nisarg Shah permalink
    June 13, 2014

    Thanks man! Badly needed to set up ppp0 interface for vnstat.

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS

Ubuntu