How to monitor monthly bandwidth with Zabbix and vnstat

Monitoring server's used bandwidth is not something that comes integrated with Zabbix by default.

As we have this need at Cloudways, we have developed a pretty simple way to do it. Let's review it.

I am assuming a Debian/Ubuntu system here, if you are on another distro you will have to adapt instructions.

First of all we deploy vnstat to the target server(s).  vnstat is a simple program to monitor network traffic: 

root@193-eh-web1:/home/pere# apt-cache search -n vnstat
vnstat - console-based network traffic monitor
root@193-eh-web1:/home/pere# apt-get install vnstat

Then we need to initialize the vnstat database for the interface we want to monitor bandwidth on. i.e. for eth0 (change this to the interface that you want to monitor!) :

root@193-eh-web1:/home/pere# vnstat -u -i eth0

We are done with vnstat . After a while we should be able to start retrieving stats for the given interface. i.e. to get the monthly used bandwidth (you can get daily, hourly ... check man page): 

root@193-eh-web1:/home/pere# vnstat -m
 eth0  /  monthly
       month        rx      |     tx      |    total    |   avg. rate
    ------------------------+-------------+-------------+---------------
      Oct '12     22.73 GiB |  211.00 GiB |  233.73 GiB |  732.02 kbit/s
      Nov '12     23.50 GiB |  241.88 GiB |  265.38 GiB |  858.86 kbit/s
      Dec '12     26.49 GiB |  324.78 GiB |  351.27 GiB |    1.10 Mbit/s
      Jan '13     26.54 GiB |  325.44 GiB |  351.98 GiB |    1.10 Mbit/s
      Feb '13     20.01 GiB |  172.79 GiB |  192.80 GiB |  668.52 kbit/s
      Mar '13     23.53 GiB |  190.05 GiB |  213.57 GiB |  668.90 kbit/s
      Apr '13     20.10 GiB |  138.77 GiB |  158.87 GiB |  514.16 kbit/s
      May '13     12.53 GiB |  102.85 GiB |  115.39 GiB |  361.39 kbit/s
      Jun '13     13.72 GiB |  126.12 GiB |  139.83 GiB |  452.54 kbit/s
      Jul '13     12.93 GiB |  128.07 GiB |  141.01 GiB |  441.62 kbit/s
      Aug '13     15.52 GiB |  118.83 GiB |  134.35 GiB |  420.77 kbit/s
      Sep '13     20.50 GiB |  113.71 GiB |  134.20 GiB |  496.30 kbit/s
    ------------------------+-------------+-------------+---------------
    estimated     23.42 GiB |  129.93 GiB |  153.35 GiB |

With vnstat in place and correctly working , we move now to creating a Zabbix UserParameter that will retrieve the total bandwidth used in a given month and send it to Zabbix via the Zabbix agent that we have installed in our server. Visit this link for more information on Zabbix UserParameters.

For this we first create a script that will get from vnstat the necessary information (so the total bandwidth used up to now in the current month): 

#!/bin/bash
# Current month total bandwidth in MB
i=$(vnstat --oneline | awk -F\; '{ print $11 }')
bandwidth_number=$(echo $i | awk '{ print $1 }')
bandwidth_unit=$(echo $i | awk '{ print $2 }')
case "$bandwidth_unit" in
KiB)    bandwidth_number_MB=$(echo "$bandwidth_number/1024" | bc)
    ;;
MiB)    bandwidth_number_MB=$bandwidth_number
    ;;
GiB)     bandwidth_number_MB=$(echo "$bandwidth_number*1024" | bc)
    ;;
TiB)    bandwidth_number_MB=$(echo "$bandwidth_number*1024*1024" | bc)
    ;;
esac
echo $bandwidth_number_MB

And now we add the aforementioned  UserParameter to the Zabbix Agent configuration file (in Debian it is located in /etc/zabbix/zabbix_agentd.conf. We add this line under the ####### USER-DEFINED MONITORED PARAMETERS ####### section:

UserParameter=system.monthlybandwidth,/home/cloudways/zabbix/zabbix_total_month_bandwidth.sh

Please modify the script path and name according to your settings.  Note that the name we have used for this new UserParameter is system.monthlybandwidth . Again, you can customize this according to your needs.

We are mostly done. 

We just need now to go to Zabbix Admin and add the new parameter as an item to the desired server or template. You will want typically to deploy this new parameter to a template so it gets automatically distributed to new servers using this template (this is how we use it at Cloudways):

Here you can see the definition of the new item with the name of  Total Bandwidth Month . We have used a multiplier too to convert the final unit to GB.

So now we can check how it looks a graph for one server with this item configured:   

As expected we have a raising line each month from 0 to the total used bandwidth. We will be able to spot sudden rises in bandwidth consumption, max during the period for which we have data ... 

Hope you find this useful and let me know if you have any questions. I will address them as good as I can.  

I will follow up with additional posts on other interesting items not defined by default in Zabbix but that are very interesting to track (like Varnish hits, APC hits, Memcached hits and fillratio ...).