Outside of internal networks bandwidth is paid often for by the gigabyte and as such it becomes important to be able to see the bandwidth consumption of machines and plan future quotas accordingly as usage grows. There are plenty of packages available that fill this void like darkstat, ntop etc, but they tend to be overkill in a lot of situations were you don’t need pretty graphs you just want to know how much data is entering and leaving an interface. Enter vnStat a simple console based tool that allows you to monitor traffic flow to and from interfaces on your servers.
On a Debian / Ubuntu based machine simply execute:
apt-get install vnstat
CentOS is a little more difficult here as vnStat is unfortunately not available in the default repositories so you will have to grab it from a 3rd party source.
rpm -Uhv http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm
yum install vnstat
Now that vnStat is installed on your system you need to initialise a database to store information for the interface you would like to monitor. For example the command below would create a database for the eth0 interface:
sudo vnstat -u -i eth0
This will give a message similar to:
Unable to read database “/var/lib/vnstat/eth0”.
-> A new database has been created.
After this you just have to give it some time to start gathering data if you run the command before it has enough data for the chosen interface you will get an error similar to:
eth0: Not enough data available yet.
After vnStat has enough data getting information backout is easy for a high level summary simply execute the command on its on for a report similar to:
Database updated: Tue Jul 5 15:15:01 2011
received: 9.90 GB (7.5%)
transmitted: 122.09 GB (92.5%)
total: 131.99 GB
rx | tx | total
yesterday 4.04 MB | 6.82 MB | 10.86 MB
today 2.64 MB | 4.20 MB | 6.84 MB
estimated 3 MB | 6 MB | 9 MB
For a look at bandwidth usage per day over the last 30 days simply add the -d flag.
vnstat -i eth0 -d
To see a breakdown of the last 7 days, the current week and the previous week use the -w flag.
vnstat -i eth0 -w
Last but not least if you want a monthly breakdown of bandwidth usage use the -m flag.
vnstat -i eth0 -m
Reports delivered straight to you
Logging in and running the command to get your usage is one thing but in the long run you are probably going to want something a bit more automated. The solution, run the vnstat command as a cron job and email the output to yourself with something similar to below:
55 22 * * 6 /usr/bin/vnstat -i eth0 -w | /usr/bin/mail email@example.com -s “host.myserver.com Weekly Bandwidth stats” > /dev/null 2>&1