Thursday, June 2, 2016

How to install vnstat Network Monitoring tool in linux?

vnstat is a command line utility that displays and logs network traffic of the interfaces on your systems. This depends on the network statistics provided by the kernel. So, vnstat doesn’t add any additional load to your system for monitoring and logging the network traffic.

 Install vnStat

In Ubuntu
For example, on Ubuntu use apt-get to install it as shown below.
$ apt-get install vnstat

Install vnStat on your system from the repository that is specific to your Linux distributions.

Install rpmforge Repository
CentOS/RHEL 6, 32 Bit (i686):
# rpm -Uvh

CentOS/RHEL 6, 64 Bit (x86_64):
# rpm -Uvh
Install vnStat package using Yum
Use yum command line utility to install vnstat package on your system.

# yum install vnstat

Since vnstat depends on the information provided by kernel, execute the following command to verify whether kernel is providing all the information that vnStat is expecting.

# vnstat --testkernel
This test will take about 60 seconds.
Everything is ok.
Pick a Interface to Monitor using vnStat

vnStat doesn’t monitor any interfaces unless you specifically request it to do so.

To start monitoring eth0, do the following. This needs to be executed only once. As you see below, this creates a database file eth0 under /var/lib/vnstat directory that will contain all the network traffic log messages for this specific interface.

# vnstat -u -i eth0
Error: Unable to read database "/var/lib/vnstat/eth0".
Info: -> A new database has been created.
To view all the available interfaces on your system that vnStat can monitor, do the following.

# vnstat --iflist
Available interfaces: lo eth0 eth1 sit0
Start the vnstatd (vnstat daemon), which will monitor and log these information in the background.

# vnstatd -d
# ps -ef | grep vnst
root     14353     1  0 09:12 ?        00:00:00 vnstatd -d
root     14355   330  0 09:12 pts/1    00:00:00 grep vnst
Note: You can add “vnstatd -d” to your /etc/rc.local file, so that it starts automatically anytime you reboot your system.
vnStat Basic Usage

vnstat without any argument will give you a quick summary with the following info:

The last time when the vnStat datbase located under /var/lib/vnstat/ was updated
From when it started collecting the statistics for a specific interface
The network statistic data (bytes transmitted, bytes received) for the last two months, and last two days.
# vnstat
Database updated: Sat Oct 15 11:54:00 2015

   eth0 since 10/01/15

          rx:  12.89 MiB      tx:  6.94 MiB      total:  19.82 MiB

                     rx      |     tx      |    total    |   avg. rate
       Sep '15     12.90 MiB |    6.90 MiB |   19.81 MiB |    0.14 kbit/s
       Oct '15     12.89 MiB |    6.94 MiB |   19.82 MiB |    0.15 kbit/s
     estimated        29 MiB |      14 MiB |      43 MiB |

                     rx      |     tx      |    total    |   avg. rate
     yesterday      4.30 MiB |    2.42 MiB |    6.72 MiB |    0.64 kbit/s
         today      2.03 MiB |    1.07 MiB |    3.10 MiB |    0.59 kbit/s
     estimated         4 MiB |       2 MiB |       6 MiB |
Note: If you just installed the vnStat, it will give the following message “eth0: Not enough data available yet.”. Wait for some time and try the command again.
vnStat hours, days, months, weeks Network Data

Use “vnstat -h” (or) “vnstat –hours” for network statistic data breakdown by hour. This also displays a text based graph.

Use “vnstat -d” (or) “vnstat –days” for network statistic data breakdown by day.

# vnstat -d
 eth0  /  daily
         day         rx      |     tx      |    total    |   avg. rate
      10/10/15      2.48 MiB |    1.28 MiB |    3.76 MiB |    0.36 kbit/s
      10/11/15      4.07 MiB |    2.17 MiB |    6.24 MiB |    0.59 kbit/s
      10/12/15      4.30 MiB |    2.42 MiB |    6.72 MiB |    0.64 kbit/s
      10/13/15      2.06 MiB |    1.10 MiB |    3.16 MiB |    0.60 kbit/s
     estimated         3 MiB |       1 MiB |       4 MiB |
Use “vnstat -m” (or) “vnstat –months” for network statistic data breakdown by month.

# vnstat --m

 eth0  /  monthly

       month        rx      |     tx      |    total    |   avg. rate
      Sep '15     12.90 MiB |    6.90 MiB |   19.81 MiB |    0.14 kbit/s
      Oct '15     12.92 MiB |    6.96 MiB |   19.89 MiB |    0.15 kbit/s
    estimated        29 MiB |      14 MiB |      43 MiB |
Similar to days and months, use “vnstat -m” (or) “vnstat –months” for network statistic data breakdown by week.

Export the data to Excel or other DB

If you like to export the network monitoring data to an excel or other database, you can dump the data in a text format delimited with semi-colon, which you can import to Excel or other db.

The 1st few lines of the –dumpdb output contains some header information. After the header lines, it has 30 lines that starts with “d;” (d;0;1318316406;1;0;386;698;1). This lines has the following information separated by semi-colon.

d – stands for days
0 – number of the day. 0 indicates today.
1318316406 – data in Unix format
Followed by this, it contains the bytes transmitted and received
$ vnstat --dumpdb
You can also use “vnstat –oneline”, which displays the traffic summary in a single line where the values are delimited with semi-colon.

$ vnstat --oneline
1;eth0;10/11/15;1.45 MiB;801 KiB;2.23 MiB;0.59 kbit/s;Oct '15;3.93 MiB;2.06 MiB;6.00 MiB;0.05 kbit/s;3.93 MiB;2.06 MiB;6.00 MiB
Display Live Network Statistics

Use “vnstat -l” or “vnstat –live” to display the live network statistic information.

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

   rx:        2 kbit/s     5 p/s          tx:        2 kbit/s     4 p/s
After you press Ctrl-C to stop it, vnstat will display a summary for the time period the live monitor was running.

No comments:

Post a Comment