Friday, April 27, 2012

FreeBSD Install vnStat: A Console-Based Network Traffic Monitor

How do I install vnStat under FreeBSD 8 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

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
Sample outputs:
scheme:   [ftp]
user: []
password: []
host: [ftp.freebsd.org]
port: [0]
document: [/pub/FreeBSD/ports/amd64/packages-8.0-release/Latest/vnstat.tbz]
---> ftp.freebsd.org:21
looking up ftp.freebsd.org
connecting to ftp.freebsd.org:21
<<< 220 ftp.beastie.tdk.net FTP server (Version 6.00LS) ready.
>>> USER anonymous
<<< 331 Guest login ok, send your email address as password.
>>> PASS root@b1.simplyguide.org
<<< 230 Guest login ok, access restrictions apply.
>>> PWD
<<< 257 "/" is current directory.
>>> CWD pub/FreeBSD/ports/amd64/packages-8.0-release/Latest
<<< 250 CWD command successful.
>>> MODE S
<<< 200 MODE S accepted.
>>> TYPE I
<<< 200 Type set to I.
setting passive mode
>>> PASV
<<< 227 Entering Passive Mode (87,51,34,132,212,201)
opening data connection
initiating transfer
>>> RETR vnstat.tbz
<<< 150 Opening BINARY mode data connection for 'vnstat.tbz' (96232 bytes).
Fetching ftp://ftp.freebsd.org/pub/FreeBSD/ports/amd64/packages-8.0-release/Latest/vnstat.tbz...Waiting for final status
<<< 226 Transfer complete.
x +CONTENTS
x +COMMENT
x +DESC
x +DISPLAY
x +MTREE_DIRS
x bin/vnstat
x sbin/vnstatd
....
...
###########################################################################
vnstat has been installed.
This port neeeds a cron entry. Please copy the contents of:
/usr/local/share/doc/vnstat/vnstat-cron to your crontab. A Sample
configuration file has be installed in /usr/local/etc/
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

No comments:

Post a Comment