Tuesday, October 6, 2015

Monthly Bandwidth Usage monitoring with Zabbix


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

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

[root@centos ~]# yum 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@centos ~]# 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@centos ~]# vnstat –m

eth0  /  monthly
       month        rx      |     tx      |    total    |   avg. rate
    ------------------------+-------------+-------------+---------------
      Nov '14    201.01 GiB |   82.29 GiB |  283.30 GiB |  916.87 kbit/s
      Dec '14    282.30 GiB |  219.39 GiB |  501.69 GiB |    1.57 Mbit/s
      Jan '15    637.36 GiB |  521.07 GiB |    1.13 TiB |    3.63 Mbit/s
      Feb '15    608.20 GiB |  608.40 GiB |    1.19 TiB |    4.22 Mbit/s
      Mar '15    559.96 GiB |  419.99 GiB |  979.95 GiB |    3.07 Mbit/s
      Apr '15    420.36 GiB |  355.42 GiB |  775.78 GiB |    2.51 Mbit/s
      May '15    155.58 GiB |  110.97 GiB |  266.55 GiB |  834.81 kbit/s
      Jun '15    184.76 GiB |  122.08 GiB |  306.84 GiB |  993.05 kbit/s
      Jul '15    288.87 GiB |  143.55 GiB |  432.42 GiB |    1.35 Mbit/s
      Aug '15    268.43 GiB |  266.29 GiB |  534.71 GiB |    1.67 Mbit/s
      Sep '15     93.30 GiB |  235.18 GiB |  328.48 GiB |    1.06 Mbit/s
      Oct '15     15.69 GiB |    6.72 GiB |   22.40 GiB |  419.62 kbit/s
    ------------------------+-------------+-------------+---------------
    estimated     93.79 GiB |   40.18 GiB |  133.97 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/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







Here you can see the definition of the new item with the name of  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 ... 

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hello,

    Thanks for the useful post. You send data in MB from zabbix agent and use custom multiplier. why 0.0009*?

    ReplyDelete