Search

vnstat: a console-based network traffic monitor

February 8th, 2009 edited by Vicho

Article submitted by András Horváth. Guess what? We still need you to submit good articles about software you like!

There are many command-line utilities in the Unix / Linux world that collect network traffic statistics for a chosen interface.

vnstat is different because statistics are collected permanently. It keeps the data in files so traffic monitoring is resumed at reboot. vnstat can be useful in situations when you need to know the exact amount of traffic that goes through the interface for a period longer than the computer is on. For example when you have a mobile 3G internet connection or an xDSL connection that has traffic limit per month, then you can easily check how much traffic you generated.

vnstat can be run without root privileges because it collects data from the kernel’s proc filesystem. It doesn’t have a daemon service, it is simply run by cron periodically. In Debian, the installation creates a cron job where a vnstat -u command is run every 5 minutes:

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

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

Initially, you have to create the database with this command:

vnstat -u -i wlan1

After creating the database, the statistics can be viewed by running vnstat without any further parameters. Thanks to the cron job, the statistic is updated every 5 minutes. It even remembers the interface name to list:

$ vnstat

Database updated: Tue Jan 27 15:40:01 2009

        wlan1

           received:       2.18 GB (81.3%)
        transmitted:     512.85 MB (18.7%)
              total:       2.68 GB

                        rx     |     tx     |  total
        -----------------------+------------+-----------
        yesterday     95.14 MB |   12.19 MB |  107.32 MB
            today     11.33 MB |    4.47 MB |   15.80 MB
        -----------------------+------------+-----------
        estimated        16 MB |       6 MB |      22 MB

Some command line options allow you to view statistics for specific periods of time. For example:

  • --days
    $ vnstat --days
    
     wlan1  /  daily
    
        day         rx      |     tx      |  total
    ------------------------+-------------+----------------------------------------
       18.01.       231 kB  |     115 kB  |     346 kB
       19.01.    611.72 MB  |   58.16 MB  |  669.88 MB   %%%%%%%%%%%%%%%%%::
       20.01.    400.53 MB  |  200.70 MB  |  601.23 MB   %%%%%%%%%%%%%%:::::
       21.01.      5.87 MB  |    1.56 MB  |    7.43 MB
       22.01.    417.97 MB  |  108.94 MB  |  526.91 MB   %%%%%%%%%%%%%%::::
       23.01.    365.24 MB  |   41.78 MB  |  407.02 MB   %%%%%%%%%%%%::
       24.01.    150.00 MB  |   13.42 MB  |  163.43 MB   %%%%%%
       25.01.    170.17 MB  |   71.52 MB  |  241.70 MB   %%%%%%:::
       26.01.     95.14 MB  |   12.19 MB  |  107.32 MB   %%%%
       27.01.     11.36 MB  |    4.51 MB  |   15.87 MB
    ------------------------+-------------+----------------------------------------
     estimated       16 MB  |       6 MB  |      22 MB
    
  • --weeks
    $ vnstat --weeks
    
            wlan1  /  weekly
    
                                rx      |       tx      |    total
            ----------------------------+---------------+--------------
              last 7 days      1.19 GB  |    253.93 MB  |      1.44 GB
                last week      2.07 GB  |    496.08 MB  |      2.56 GB
             current week    106.52 MB  |     16.71 MB  |    123.23 MB
            ----------------------------+---------------+--------------
                estimated       456 MB  |        68 MB  |       524 MB
    
  • --months
    $ vnstat --months
    
     wlan1  /  monthly
    
       month         rx      |      tx      |   total
    -------------------------+--------------+--------------------------------------
      Jan '09       2.18 GB  |   512.90 MB  |     2.68 GB   %%%%%%%%%%%%%:::
    -------------------------+--------------+--------------------------------------
     estimated      2.53 GB  |      596 MB  |     3.12 GB
    
  • --top10
    $ vnstat --top10
    
     wlan1  /  top 10
    
       #       day         rx      |     tx      |  total
    -------------------------------+-------------+---------------------------------
       1    19.01.09    611.72 MB  |   58.16 MB  |  669.88 MB   %%%%%%%%%%%%%%%%::
       2    20.01.09    400.53 MB  |  200.70 MB  |  601.23 MB   %%%%%%%%%%%:::::
       3    22.01.09    417.97 MB  |  108.94 MB  |  526.91 MB   %%%%%%%%%%%:::
       4    23.01.09    365.24 MB  |   41.78 MB  |  407.02 MB   %%%%%%%%%:
       5    25.01.09    170.17 MB  |   71.52 MB  |  241.70 MB   %%%%::
       6    24.01.09    150.00 MB  |   13.42 MB  |  163.43 MB   %%%%
       7    26.01.09     95.14 MB  |   12.19 MB  |  107.32 MB   %%
       8    21.01.09      5.87 MB  |    1.56 MB  |    7.43 MB
       9    18.01.09       231 kB  |     115 kB  |     346 kB
    -------------------------------+-------------+---------------------------------
    
  • --live gives real-time statistics about the packets currently going through the interface:
    $ vnstat --live -i wlan1
    Monitoring wlan1...    (press CTRL-C to stop)
    
       rx:     113.40 kB/s    99 p/s            tx:      28.42 kB/s    96 p/s
    

Pros and cons

  • Pros:
    • Permanent network traffic statistic that is not forgotten.
    • Very lightweight solution, no daemon service is running in the background (automatically run from cron periodically).
    • Easy to install and use. In vnstat, everything can be done from the command line and it clearly shows how many megabytes have you send/receive in a given day or month.
  • Cons:
    • Internet or remote traffic cannot be separated from local network traffic if it all goes through the interface being monitored.
    • It works for a single host, but it doesn’t scale very well. If you need to monitor a lot of hosts and need detailed graphs, there are better alternatives, such as munin or ganglia.

Availability

There are official packages available in both Debian and Ubuntu (Universe) for a long time now.

Posted in Debian, Ubuntu |

3 Responses

  1. xio Says:

    Right, and it also has a nice web interface: http://www.sqweek.com/sqweek/index.php?p=1

  2. fenix Says:

    Hi, I’ve realized also a GTK fronted based on vnstat.
    The software is designed to monitor the traffic from my mobile phone with the plan “naviga 3″. This plan is available with the Italian operator “3″.
    The program is downloadable from here:
    http://portus.altervista.org/?p=4

  3. Kane Says:

    Seems like a nice lightweight solution. I’ve tried a few and they eat resources on my poor old p3 server.