FreeBSD Install vnStat: A Console-Based Network Traffic Monitor

Posted on in Categories , , , last updated March 31, 2017

How do I install vnStat under FreeBSD 8/9/10/11 operating system to keep track of my network traffic?

From the man page:

vnStat is a console-based network traffic monitor. It keeps a log of hourly, daily and monthly network traffic for the selected interface(s). However, it isn’t a packet sniffer. The traffic information is analyzed from the proc(5) and sys filesystems depending on availability. That way vnStat can be used even without root permissions on most systems.

WARNING! These examples only works on FreeBSD operating systems. Linux specific installation instructions are here. Note that virtual and aliased interfaces cannot be monitored because the kernel doesn’t provide traffic information for that type of interfaces.

Step #1: Update Ports Tree

You must update port tree by typing the following portsnap command:
# portsnap fetch update
OR
# pkg update

Step #2: Install vnStat

Type the following command to find out exact port location:
# cd /usr/ports
# whereis vnstat

Sample outputs:

/usr/ports/net/vnstat

Install the vnstat, run:
# cd /usr/ports/net/vnsta
# make install clean
# rehash

Alternatively, you can install the binary package using the pkg_add command, enter:
# pkg_add -r -v vnstat
OR use pkg command:
# pkg install vnstat
Sample outputs:

Updating FreeBSD repository catalogue...
FreeBSD repository is up-to-date.
All repositories are up-to-date.
The following 11 package(s) will be affected (of 0 checked):
 
New packages to be INSTALLED:
	vnstat: 1.15
	libgd: 2.2.4,1
	fontconfig: 2.12.1,1
	expat: 2.2.0_1
	freetype2: 2.6.3
	png: 1.6.27
	webp: 0.5.2
	jpeg-turbo: 1.5.1
	giflib: 5.1.4
	tiff: 4.0.7_1
	jbigkit: 2.1_1
 
Number of packages to be installed: 11
 
The process will require 14 MiB more space.
3 MiB to be downloaded.
 
Proceed with this action? [y/N]: y
[backup] Fetching vnstat-1.15.txz: 100%   92 KiB  94.6kB/s    00:01    
[backup] Fetching libgd-2.2.4,1.txz: 100%  187 KiB 191.6kB/s    00:01    
[backup] Fetching fontconfig-2.12.1,1.txz: 100%  349 KiB  71.4kB/s    00:05    
[backup] Fetching expat-2.2.0_1.txz: 100%  102 KiB  52.3kB/s    00:02    
[backup] Fetching freetype2-2.6.3.txz: 100%  488 KiB  83.4kB/s    00:06    
[backup] Fetching png-1.6.27.txz: 100%  280 KiB 143.4kB/s    00:02    
[backup] Fetching webp-0.5.2.txz: 100%  364 KiB  93.1kB/s    00:04    
[backup] Fetching jpeg-turbo-1.5.1.txz: 100%  298 KiB  61.1kB/s    00:05    
[backup] Fetching giflib-5.1.4.txz: 100%   74 KiB  75.4kB/s    00:01    
[backup] Fetching tiff-4.0.7_1.txz: 100%  794 KiB  45.2kB/s    00:18    
[backup] Fetching jbigkit-2.1_1.txz: 100%   67 KiB  68.8kB/s    00:01    
Checking integrity... done (0 conflicting)
[backup] [1/11] Installing jpeg-turbo-1.5.1...
[backup] [1/11] Extracting jpeg-turbo-1.5.1: 100%
[backup] [2/11] Installing jbigkit-2.1_1...
[backup] [2/11] Extracting jbigkit-2.1_1: 100%
[backup] [3/11] Installing expat-2.2.0_1...
[backup] [3/11] Extracting expat-2.2.0_1: 100%
[backup] [4/11] Installing freetype2-2.6.3...
[backup] [4/11] Extracting freetype2-2.6.3: 100%
[backup] [5/11] Installing png-1.6.27...
[backup] [5/11] Extracting png-1.6.27: 100%
[backup] [6/11] Installing giflib-5.1.4...
[backup] [6/11] Extracting giflib-5.1.4: 100%
[backup] [7/11] Installing tiff-4.0.7_1...
[backup] [7/11] Extracting tiff-4.0.7_1: 100%
[backup] [8/11] Installing fontconfig-2.12.1,1...
[backup] [8/11] Extracting fontconfig-2.12.1,1: 100%
Running fc-cache to build fontconfig cache...
/usr/local/share/fonts: skipping, no such directory
/usr/local/lib/X11/fonts: skipping, no such directory
/var/db/fontconfig: cleaning cache directory
fc-cache: succeeded
[backup] [9/11] Installing webp-0.5.2...
[backup] [9/11] Extracting webp-0.5.2: 100%
[backup] [10/11] Installing libgd-2.2.4,1...
[backup] [10/11] Extracting libgd-2.2.4,1: 100%
[backup] [11/11] Installing vnstat-1.15...
===> Creating groups.
Creating group 'vnstat' with gid '284'.
===> Creating users
Creating user 'vnstat' with uid '284'.
[backup] [11/11] Extracting vnstat-1.15: 100%
Message from vnstat-1.15:
###########################################################################
vnstat has been installed.
 
A sample configuration file has been installed in /usr/local/etc/
Please add your default network interface in the 'Interface' line there
before starting vnstat service.
You can add any number of additional interfaces either in
vnstat_additional_ifaces variable in /etc/rc.conf, or with this command:
su -m vnstat -c "/usr/local/bin/vnstat --create --iface <your interface>"
and restart vnstat service:
service vnstat restart
 
For more information about vnStat use "man vnstat" or visit:
http://humdi.net/vnstat/
###########################################################################

Step #3: Configure vnStat

Now, vnstat has been installed. However, you need to configure /usr/local/etc/vnstat.conf file, enter:
# cp /usr/local/etc/vnstat.conf.sample /usr/local/etc/vnstat.conf
# vi /usr/local/etc/vnstat.conf

Set default interface

Interface "em0"

Set maximum bandwidth (Mbit) for all interfaces, use 0 to disable feature. In this example, set to 100Mbit:

MaxBandwidth 100

Save and close the file.

Step #3.1: Set vnStat cron job

You need to collect network stats using a cron job, enter:
# crontab -e
Append the following cronjob enter:

# run vnstat update every 5 minutes if installed
*/5 *   * * *   if [ -x /usr/local/bin/vnstat ] && [ `ls -l /var/db/vnstat/ | wc -l` -ge 1 ]; then /usr/local/bin/vnstat -u; fi

Save and close the file. Finally, create directory, enter:
# mkdir /var/db/vnstat

Step #3.2: Create Interface Specific Stats

Type the following command:

vnstat -u -i interfaceName

In this example, set em0 and em1, enter:
# vnstat -u -i em0
# vnstat -u -i em1

Sample outputs:

Error: Unable to read database "/var/db/vnstat/em1".
Info: -> A new database has been created.

How Do I View Network Traffic?

Simply type the vnstat command, run:
# vnstat
Sample outputs:

                      rx      /      tx      /     total    /   estimated
 em1:
       Nov '09      2.97 GiB  /   56.38 GiB  /   59.35 GiB
       Dec '09     35.29 MiB  /  604.47 MiB  /  639.76 MiB  /   20.71 GiB
     yesterday     39.43 MiB  /  418.21 MiB  /  457.64 MiB
         today     35.29 MiB  /  604.47 MiB  /  639.76 MiB  /     683 MiB

 em0:
       Nov '09    205.19 MiB  /  134.89 MiB  /  340.07 MiB
       Dec '09      1.53 MiB  /     938 KiB  /    2.45 MiB  /       0 KiB
     yesterday      1.56 MiB  /     769 KiB  /    2.31 MiB
         today      1.53 MiB  /     938 KiB  /    2.45 MiB  /      --    

See Daily Traffic, Enter:

vnstat -d
Sample outputs:

 em1  /  daily

         day         rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
      11/20/09     44.69 MiB |  654.44 MiB |  699.13 MiB |   66.29 kbit/s
      11/21/09     30.52 MiB |  322.34 MiB |  352.86 MiB |   33.46 kbit/s
      11/22/09     31.04 MiB |  313.65 MiB |  344.70 MiB |   32.68 kbit/s
      11/23/09    208.52 MiB |    7.04 GiB |    7.24 GiB |  703.35 kbit/s
      11/24/09     57.55 MiB |    1.00 GiB |    1.06 GiB |  102.95 kbit/s
      11/25/09    710.73 MiB |  705.73 MiB |    1.38 GiB |  134.30 kbit/s
      11/26/09     65.78 MiB |    1.52 GiB |    1.59 GiB |  154.01 kbit/s
      11/27/09     45.24 MiB |  641.35 MiB |  686.59 MiB |   65.10 kbit/s
      11/28/09     95.72 MiB |  280.59 MiB |  376.32 MiB |   35.68 kbit/s
      11/29/09     80.17 MiB |    1.77 GiB |    1.85 GiB |  179.37 kbit/s
      11/30/09    271.64 MiB |   10.62 GiB |   10.89 GiB |    1.06 Mbit/s
      12/01/09     44.13 MiB |  368.22 MiB |  412.35 MiB |   39.10 kbit/s
      12/02/09     45.46 MiB |  699.37 MiB |  744.83 MiB |   70.62 kbit/s
      12/03/09     54.18 MiB |    1.01 GiB |    1.06 GiB |  103.05 kbit/s
      12/04/09     44.90 MiB |  668.54 MiB |  713.43 MiB |   67.64 kbit/s
      12/05/09     28.79 MiB |  325.02 MiB |  353.81 MiB |   33.55 kbit/s
      12/06/09     55.02 MiB |    1.35 GiB |    1.41 GiB |  136.60 kbit/s
      12/07/09    276.27 MiB |   10.73 GiB |   11.00 GiB |    1.07 Mbit/s
      12/08/09     38.15 MiB |  343.64 MiB |  381.79 MiB |   36.20 kbit/s
      12/09/09     43.77 MiB |  625.01 MiB |  668.78 MiB |   63.41 kbit/s
      12/10/09     36.95 MiB |  348.90 MiB |  385.84 MiB |   36.58 kbit/s
      12/11/09     54.36 MiB |  320.11 MiB |  374.47 MiB |   35.51 kbit/s
      12/12/09    138.14 MiB |    1.41 GiB |    1.55 GiB |  150.33 kbit/s
      12/13/09     32.60 MiB |  410.92 MiB |  443.52 MiB |   42.05 kbit/s
      12/14/09    280.86 MiB |   10.51 GiB |   10.79 GiB |    1.05 Mbit/s
      12/15/09     49.07 MiB |  717.77 MiB |  766.83 MiB |   72.71 kbit/s
      12/16/09     39.99 MiB |  356.22 MiB |  396.22 MiB |   37.57 kbit/s
      12/17/09     54.12 MiB |  743.57 MiB |  797.69 MiB |   75.63 kbit/s
      12/18/09     39.43 MiB |  418.21 MiB |  457.64 MiB |   43.39 kbit/s
      12/19/09     35.33 MiB |  604.59 MiB |  639.92 MiB |   64.72 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated        37 MiB |     644 MiB |     681 MiB |

See Traffic For Months, Enter:

# vnstat -m
Sample outputs:

 em1  /  monthly

       month        rx      |     tx      |    total    |   avg. rate
    ------------------------+-------------+-------------+---------------
      Aug '09      2.24 GiB |   42.50 GiB |   44.74 GiB |  140.13 kbit/s
      Sep '09      1.68 GiB |   37.86 GiB |   39.54 GiB |  127.96 kbit/s
      Oct '09      2.12 GiB |   48.09 GiB |   50.21 GiB |  157.27 kbit/s
      Nov '09      2.97 GiB |   56.38 GiB |   59.35 GiB |  192.06 kbit/s
      Dec '09     35.33 MiB |  604.59 MiB |  639.92 MiB |   64.72 kbit/s
    ------------------------+-------------+-------------+---------------
    estimated      1.13 GiB |   19.50 GiB |   20.63 GiB |

Other Options:

To display traffic for the last 24 hours, enter:
# vnstat -h
Display all time top10 traffic days, run:
# vnstat -t
To display traffic for 7 days, current and previous week, run:
# vnstat -w
To display current transfer rate for the selected interface in real time until interrupted. Statistics will be shown after interruption if runtime was more than 10 seconds, run:
# vnstat -l
Sample outputs:

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

   rx:       28 kbit/s    25 p/s          tx:      108 kbit/s    22 p/s


 em1   /  traffic statistics

                           rx         |       tx
--------------------------------------+------------------------
  bytes                      729 KiB  |        3.95 MiB
--------------------------------------+------------------------
          max             236 kbit/s  |     1.84 Mbit/s
      average           72.00 kbit/s  |   399.80 kbit/s
          min               8 kbit/s  |        8 kbit/s
--------------------------------------+------------------------
  packets                       4254  |            4424
--------------------------------------+------------------------
          max                130 p/s  |         185 p/s
      average                 52 p/s  |          54 p/s
          min                  8 p/s  |           8 p/s
--------------------------------------+------------------------
  time                  1.35 minutes

See Also:

  1. How to install vnstat under Linux operating systems.

2 comment

Leave a Comment