vnStat – A lightweight (Command Line) Network Traffic Monitoring Tool

vnStat is a console based (lightweight) network traffic monitor utility for Linux & BSD, that collect & store network interface statistics data (bytes transmitted & bytes received) information provided by the kernel (based on /proc & /sys filesystems) for the selected interfaces in a database for later analysis.

Kernel is the source for vnstat. So, it wont add any additional load to your system when you are running vnstat to capture the network traffic. Database file has been created based on the interface name at /var/lib/vnstat/ and configuration file located /etc/vnstat.conf.

It will automatically discover the eth0 interface and create database file & necessary directory too but not for other interfaces.

[ac-button size=”large” color=”green” style=”flat” icon=”fa-link” url=”https://www.2daygeek.com/category/monitoring-tools/” target=”_blank”]Check available monitoring tools for Linux[/ac-button]

Make sure, you have to identify your network interface before creating database file with help of IP Command. Optionally we can generate png image output with help of gd library, make sure gd library installed on your system. If no, run the below command to install it.

[Install GD library on CentOS/Fedora]
$ sudo yum install php-gd

[Install GD library on Fedora 22 and later]
$ sudo dnf install php-gd

[Install GD library on Debian/Linux Mint/Ubuntu for php5]
$ sudo apt-get install php5-gd

[Install GD library on Debian/Linux Mint/Ubuntu for php7]
$ sudo apt-get install php-gd

[Install GD library on openSUSE]
$ sudo zypper in php-gd

[Install GD library on Mageia]
$ sudo urpmi php-gd

[Install GD library on Arch based system]
$ sudo pacman -S php-gd

Traffic statistics data

  • Hourly : It stores last 24 hours data on a hourly basics.
  • Daily : It stores last 30 days data on a daily basics.
  • Monthly : It stores last 12 months data on a monthly basics.

How to install vnStat

vnStat utility is available on distribution default repository, so just install like that with help of package manager based on distribution.

Note : CentOS/RHEL users need to install EPEL repositories in order to get vnstat package.

[Install vnStat on CentOS/Fedora]
$ sudo yum install vnstat

[Install vnStat on Fedora] 22 and later]
$ sudo dnf install vnstat

[Install vnStat on Debian/Linux Mint/Ubuntu]
$ sudo apt-get install vnstat

[Install vnStat on openSUSE]
$ sudo zypper in vnstat

[Install vnStat on Mageia]
$ sudo urpmi vnstat

[Install vnStat on Arch based distro]
$ sudo pacman -S vnstat

Start the vnstat service by running below command.

[For systemd system]
$ sudo systemctl statrt vnstat

[For SysVinit system]
$ sudo service vnstat start

Check your network interface name

Just fire the below IP Command to find your network interface name to create database.

$ ip addr
1: lo:  mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: wlp4s0:  mtu 1500 qdisc mq state UNKNOWN qlen 1000
    link/ether 00:50:56:8a:4c:53 brd ff:ff:ff:ff:ff:ff
    inet 10.20.0.29/24 brd 10.20.0.255 scope global wlp4s0
    inet6 fe80::250:56ff:fe8a:4c53/64 scope link
       valid_lft forever preferred_lft forever

I could see that, my network interface name is wlp4s0. It won’t create database by default and we have to create manually.

Create a new database for network interface

vnStat installation got success, just fire the following command on terminal to check whether database created by default or not.

[ac-box color=”yellow” icon=”fa-lightbulb-o”]
By default vnStat create database, when you have eth0 as a default interface. If no, you have to create manually based on the interface name.
[/ac-box]

I have fired vnstat command on terminal and got below error, it means database notyet created. So, follow the below procedure to create a database manually.

$ vnstat
Error: Unable to open database directory "/var/lib/vnstat": No such file or directory
The vnStat daemon should have created this directory when started.
Check that it is is configured and running. See also "man vnstatd".

Make sure that the below directory exist. If no, create it.

$ sudo mkdir /var/lib/vnstat

$ ls -lh /var/lib/vnstat
ls: cannot access '/var/lib/vnstat': No such file or directory

Directory created successfully but not database, that’s why we are getting below message. As i told earler, we have to create database manually, if we don’t have eth0 as default interface.

$ vnstat
No database found, nothing to do. Use --help for help.

A new database can be created with the following command:
    vnstat --create -i eth0

Replace 'eth0' with the interface that should be monitored.

The following interfaces are currently available:
    lo enp5s0 (10 Mbit) wlp4s0

Make sure, you need to replace your interface name wlp4s0 insteadof us, while running below command.

$ sudo vnstat --create -i wlp4s0
Creating database for interface "wlp4s0"...
Info: -> A new database has been created.

Restart the vnStat daemon if it is currently running in order to start monitoring "wlp4s0".

Restart the vnstat service to take effect.

[For systemd system]
$ sudo systemctl restatrt vnstat

[For SysVinit system]
$ sudo service vnstat restart

Everything is fine but you need allow vnstat sometime to collect the data. If you fire the command instantly, probably you will get below message.

$ vnstat
 wlp4s0: Not enough data available yet.

vnStat Usage

Beginning of the article, i have mentioned, we can easily get the network interface reports hourly, daily, weekly, monthly, Real Time update & possible to export as a png image.

To Check overall network interface Statistics, issue vnstat command without any option.

$ vnstat

Database updated: Wed Nov 16 04:02:08 2016

   wlp4s0 since 10/19/16

          rx:  16.37 GiB      tx:  50.80 GiB      total:  67.17 GiB

   monthly
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
       Oct '16      8.70 GiB |   26.52 GiB |   35.22 GiB |  110.31 kbit/s
       Nov '16      7.67 GiB |   24.28 GiB |   31.95 GiB |  203.94 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated     15.12 GiB |   47.89 GiB |   63.01 GiB |

   daily
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     yesterday    534.87 MiB |    1.27 GiB |    1.79 GiB |  173.69 kbit/s
         today     59.42 MiB |  208.36 MiB |  267.78 MiB |  151.00 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated       351 MiB |    1.21 GiB |    1.55 GiB |

To check hourly Statistics.

$ vnstat -h
 wlp4s0                                                                     04:37
  ^                                                         t
  |                                                t        t
  |                                                t        t
  |                                                t  t     t
  |                                             t  t  t     t
  |                                             t  t  t     t  t
  |                              t              t  t  t     t  t
  |                              t              t rt  t     t  t
  |                           t  t    r        rt rt rt     t  t
  |                     t  t rt rt    r      t rt rt rt     t rt  t
 -+--------------------------------------------------------------------------->
  |  05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 00 01 02 03 04

 h  rx (KiB)   tx (KiB)      h  rx (KiB)   tx (KiB)      h  rx (KiB)   tx (KiB)
05      11277       7255    13      26593      69026    21      68523     174513
06       5718       9454    14      38378     102727    22      11999      24136
07      19890      23350    15       9866      21148    23      22196     242514
08       8922      20916    16      62191       9380    00      26336     135677
09       8128      16131    17      12456      13115    01      15488      36997
10       8486      16887    18      11402      25789    02       9459      20252
11      17849      27287    19      54814     150075    03       9402      20170
12      17684      42951    20      81299     224294    04       2910       4501

To check daily Statistics.

$ vnstat -d

 wlp4s0  /  daily

         day         rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
      10/19/16     54.51 MiB |    3.95 MiB |   58.46 MiB |    5.54 kbit/s
      10/20/16      1.26 GiB |    4.27 GiB |    5.53 GiB |  536.77 kbit/s
      10/21/16    327.04 MiB |    1.39 GiB |    1.71 GiB |  165.73 kbit/s
      10/22/16    287.60 MiB |  553.42 MiB |  841.02 MiB |   79.74 kbit/s
      10/23/16    432.92 MiB |    0.98 GiB |    1.40 GiB |  136.14 kbit/s
      10/24/16    735.98 MiB |    1.42 GiB |    2.14 GiB |  207.43 kbit/s
      10/25/16      1.29 GiB |    4.71 GiB |    6.00 GiB |  582.60 kbit/s
      10/26/16      1.27 GiB |    4.28 GiB |    5.55 GiB |  538.76 kbit/s
      10/27/16      1.46 GiB |    5.75 GiB |    7.21 GiB |  699.85 kbit/s
      10/28/16    306.31 MiB |  426.13 MiB |  732.44 MiB |   69.45 kbit/s
      10/29/16    218.92 MiB |  366.70 MiB |  585.61 MiB |   55.52 kbit/s
      10/30/16    523.79 MiB |    1.32 GiB |    1.83 GiB |  178.11 kbit/s
      10/31/16    618.49 MiB |    1.08 GiB |    1.69 GiB |  163.82 kbit/s
      11/01/16      1.28 GiB |    4.67 GiB |    5.95 GiB |  577.70 kbit/s
      11/02/16      1.24 GiB |    4.28 GiB |    5.52 GiB |  536.02 kbit/s
      11/03/16      1.23 GiB |    4.19 GiB |    5.42 GiB |  526.34 kbit/s
      11/04/16    162.58 MiB |  141.10 MiB |  303.67 MiB |   28.79 kbit/s
      11/05/16     85.43 MiB |   56.56 MiB |  142.00 MiB |   13.46 kbit/s
      11/06/16    344.27 MiB |    2.04 GiB |    2.37 GiB |  230.27 kbit/s
      11/07/16    404.64 MiB |  518.53 MiB |  923.16 MiB |   87.53 kbit/s
      11/08/16    606.42 MiB |    1.31 GiB |    1.90 GiB |  184.65 kbit/s
      11/09/16    411.42 MiB |  799.87 MiB |    1.18 GiB |  114.85 kbit/s
      11/10/16    423.83 MiB |    2.05 GiB |    2.46 GiB |  239.09 kbit/s
      11/11/16    132.00 MiB |  148.01 MiB |  280.01 MiB |   26.55 kbit/s
      11/12/16    145.90 MiB |    1.54 GiB |    1.68 GiB |  163.54 kbit/s
      11/13/16    316.19 MiB |  758.73 MiB |    1.05 GiB |  101.92 kbit/s
      11/14/16    382.57 MiB |  377.54 MiB |  760.10 MiB |   72.07 kbit/s
      11/15/16    534.87 MiB |    1.27 GiB |    1.79 GiB |  173.69 kbit/s
      11/16/16     62.10 MiB |  212.50 MiB |  274.60 MiB |  135.29 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated       322 MiB |    1.08 GiB |    1.39 GiB |

To check weekly Statistics.

$ vnstat -w

 wlp4s0  /  weekly

                      rx      |     tx      |    total    |   avg. rate
   ---------------------------+-------------+-------------+---------------
    last 7 days      1.95 GiB |    6.32 GiB |    8.27 GiB |  129.66 kbit/s
      last week      2.38 GiB |    7.07 GiB |    9.46 GiB |  131.16 kbit/s
   current week    979.54 MiB |    1.84 GiB |    2.80 GiB |  123.97 kbit/s
   ---------------------------+-------------+-------------+---------------
      estimated      3.09 GiB |    5.95 GiB |    9.04 GiB |

To check monthly Statistics.

$ vnstat -m

 wlp4s0  /  monthly

       month        rx      |     tx      |    total    |   avg. rate
    ------------------------+-------------+-------------+---------------
      Oct '16      8.70 GiB |   26.52 GiB |   35.22 GiB |  110.31 kbit/s
      Nov '16      7.67 GiB |   24.28 GiB |   31.95 GiB |  203.66 kbit/s
    ------------------------+-------------+-------------+---------------
    estimated     15.10 GiB |   47.82 GiB |   62.92 GiB |

To check real time Statistics.

$ vnstat -l
Monitoring wlp4s0...    (press CTRL-C to stop)

   rx:        8 kbit/s    10 p/s          tx:       16 kbit/s     3 p/s

When you press CTRL-C to stop the real time statistics, you will get the real time traffic statistics.

$ vnstat -l

Monitoring wlp4s0...    (press CTRL-C to stop)

   rx:       24 kbit/s    26 p/s          tx:       40 kbit/s     8 p/s^C


 wlp4s0  /  traffic statistics

                           rx         |       tx
--------------------------------------+------------------
  bytes                      683 KiB  |        1.24 MiB
--------------------------------------+------------------
          max              36 kbit/s  |       92 kbit/s
      average           13.94 kbit/s  |    25.94 kbit/s
          min               4 kbit/s  |        8 kbit/s
--------------------------------------+------------------
  packets                       4949  |            2018
--------------------------------------+------------------
          max                 26 p/s  |          20 p/s
      average                 12 p/s  |           5 p/s
          min                  4 p/s  |           2 p/s
--------------------------------------+------------------
  time                  6.53 minutes

To see top 10 bandwidth usage date.

$ vnstat -t

 wlp4s0  /  top 10

    #      day          rx      |     tx      |    total    |   avg. rate
   -----------------------------+-------------+-------------+---------------
    1   10/27/16       1.46 GiB |    5.75 GiB |    7.21 GiB |  699.85 kbit/s
    2   10/25/16       1.29 GiB |    4.71 GiB |    6.00 GiB |  582.60 kbit/s
    3   11/01/16       1.28 GiB |    4.67 GiB |    5.95 GiB |  577.70 kbit/s
    4   10/26/16       1.27 GiB |    4.28 GiB |    5.55 GiB |  538.76 kbit/s
    5   10/20/16       1.26 GiB |    4.27 GiB |    5.53 GiB |  536.77 kbit/s
    6   11/02/16       1.24 GiB |    4.28 GiB |    5.52 GiB |  536.02 kbit/s
    7   11/03/16       1.23 GiB |    4.19 GiB |    5.42 GiB |  526.34 kbit/s
    8   11/10/16     423.83 MiB |    2.05 GiB |    2.46 GiB |  239.09 kbit/s
    9   11/06/16     344.27 MiB |    2.04 GiB |    2.37 GiB |  230.27 kbit/s
   10   10/24/16     735.98 MiB |    1.42 GiB |    2.14 GiB |  207.43 kbit/s
   -----------------------------+-------------+-------------+---------------

Generate png output

vnStati is a tool to generate .png images from vnStat, which is not installed by default. Just run the below command on your terminal to install it.

[Install vnstati on CentOS/Fedora]
$ sudo yum install vnstati

[Install vnstati on Fedora 22 and later]
$ sudo dnf install vnstati

[Install vnstati on Debian/Linux Mint/Ubuntu]
$ sudo apt-get install vnstati

[Install vnstati on openSUSE]
$ sudo zypper in vnstati

[Install vnstati on Mageia]
$ sudo urpmi vnstati

[Install vnstati on Arch based system]
$ sudo pacman -S vnstati

Now, you can generate the png image. Here i’m going to generate png image based on default vnstat statistics.

$ vnstati -i wlp4s0 -o /home/daygeek/sample.png

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

Your email address will not be published. Required fields are marked *