Unless you are in the position of having an unlimited bandwidth package on your dedicated server or VPS, chances are that at some point you are going to need to know its specific bandwidth usage.
Monitoring Your Bandwidth with vnStat
Fortunately, there are a number of tools to help you monitor this with varying levels of detail. In this article we are going to look at vnStat, a command line tool for monitoring your network traffic. vnStat relies on the kernel’s network traffic statistics for its data so that it isn’t actively monitoring the server’s interfaces itself, meaning that it has a relatively low impact on system resources.
vnStat Installation for Red Hat and CentOS
For Red Hat and CentOS systems the installation process first involves enabling the Fedora Project’s EPEL repositories on your system, more information about which can be found at https://fedoraproject.org/wiki/EPEL. Once the repository is enabled, vnstat can then be installed with the following command:
sudo yum install vnstat
vnStat Installation for Debian and Ubuntu
For Debian and Ubuntu systems vnstat can be installed with the following commands:
sudo apt-get update
sudo apt-get install vnstat
Once installed the next step is to configure vnstat to monitor the network interfaces that you need statistics for. This can be achieved with the following command:
vnstat -u -i eth0
In this instance the -u flag tells vnstat to update its database, and the -i flag informs it which interface to monitor, in this case we’ve used eth0. To add other interfaces you can simply repeat the process replacing eth0 with the other network interface you wish to monitor. It’s also possible to set a friendly name for the interface using the — nick flag, for example:
vnstat -u -i eth0 --nick Public
If you decide you no longer wish to monitor an interface, it can be removed from the database with the — delete flag:
vnstat -i eth0 --delete
Once you initially add an interface to the database, it will take some time while vnstat populates it with the information required to produce any results. Once it’s done running, the command will produce a result displaying the total bandwidth used so far, the bandwidth used so far this month and the previous month and the bandwidth used so far today and used in the previous day. If you are monitoring multiple interfaces, this will be broken down by interface.
The output can also be broken down to display bandwidth used in different time periods with the following flags:
-h shows hourly totals
-d shows daily totals
-w shows weekly totals
-m shows monthly totals
A configuration file is also stored in /etc/vnstat.conf which contains additional settings that can be used to configure vnStat’s output. One of the more useful options is the MonthRotate variable. This by default is set to 1, meaning that a new month of monitoring starts on the first day of a particular month. By changing this number you can set the day of the month that the month totals are calculated from, allowing the displayed totals in the monthly output to match your billing period.
It’s unlikely you’ll need to change any other lines in the vnstat.conf file, but if you are interested in the settings there is a man page available for the config file as well as a page for vnstat itself.
vnStat’s Image Output Trick
One final trick that vnstat has is the ability to generate is an image of its output containing a mix of graphs and text, done using the vnstati command. The following example generates a picture of the bandwidth summary for eth0:
vnstati -s -i eth0 -o ~/eth0stats.png
The -s can be replaced by the previous flags used by vnStat to generate hourly, daily, weekly or monthly statistics instead of the general summary. This can easily be combined with a cron job in order to place regularly updated statistics in a directory accessible by your web server in order to make the bandwidth easy to check on.
If you require a detailed breakdown of your bandwidth statistics, such as which applications are using most of your bandwidth, then vnStat is not the tool for you. On the other hand, if all you require is easy access to bandwidth usage totals with minimal impact on your system then vnStat makes a great option and is well worth trying.
Never miss another post. Sign up for the weekly 100TB newsletter and follow us on Facebook and Twitter.
Originally published at blog.100tb.com.