One of the most important aspects of system administration these days is monitoring the traffic on your server. Many hosting providers impose limits on how much you can transfer per month. If you go over these limits, it’s not really a problem from the data center’s point of view, but you will usually end up getting slapped with a fee per GB you transfer in excess of your limits. This can add up quickly if you have a busy site. It’s not only restrictions either. Abnormal bandwidth usage could alert you to a problem on the server, such as an exploit or some other unauthorized wasteful activity.
Vnstat is a neat little tool to help monitor your bandwidth usage. It is found in the EPEL Linux repositories. After doing some Googling, I found there aren’t many good tutorials on installing and configuring vnstat out there.
To start, you will need to enable the EPEL repositories for your version of Linux. To enable the EPEL repositories in CentOS/RHEL 6
wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm rpm -ivh epel-release-latest-6.noarch
Next, you can install vnstat as normal through yum.
yum install vnstat
Once installed, the default configuration will work for most people. The default will run on the eth0 interface. If you need to change the interfaces, you will need to edit two files. It’s self explanatory. Change the occurrences of eth0 to eth1 (or your choice of interface).
vim /etc/sysconfig/vnstat vim /etc/vnstat.conf
Next, we need to create the database where the data will be collected and stored. The command may generate an error. If it does, that’s fine. It is because the new file is created with ownership of the logged in user (we are using root). We will fix this by running a chown on the file before starting the service. Again, if you’re using eth1 or another interface, make sure you replace it in the command below.
vnstat -u -i eth0 chown vnstat:vnstat /var/lib/vnstat/eth0
That’s it. You’re ready to start the service and start collecting data.
service vnstat start chkconfig vnstat on
It may take a couple of hours for data to be ready to view. You can view bandwidth usage based on hour, day, week, or month. If you’re inpatient, like I often am, you can restart the vnstat service after running for a few minutes. This will force it to load the stats so far and give you some to look at.
vnstat -h vnstat -d vnstat -w vnstat -m