Welcome to TQ CMS knowledge base. Here you will find useful information for all sorts of things.

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 thetotal 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): 

# 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)
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:


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.

You can download the template here: Template Total Bandwidth Month

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 hitsAPC hitsMemcached hits and fillratio ...).

There is 57 guides in the database.

More news

25 February, 2014


I have implemented bootstrap into the project and changed all code in the administration to use this new framework and I really love the result that I see after a couple of hours of coding. To make things look so good has never been so easy as with
20 February, 2014


I have added some of my guides for different things when it comes to the different areas in the computer, I will continue to add guides and especially when it comes to things that I am using every now and then in my own work.
First phase completed
17 February, 2014

First phase completed

Then I had time to finish the dirty work with the page and now it only remains little touches here and there and I need to upload information about what I'm doing and my progress on the project as well.
Time for a change
16 February, 2014

Time for a change

Today I've decided to update my own personal website with the latest version of my project called TQ CMS and with a completely new design that is responsive. My goal with this project is to make it as easy as possible for the end user while


The next step

The next step

I have now resumed my programming after a couple of months of vacation which was very much needed. I got a call one day from a friend that needed a system which can handle peoples reports on various problems like kitchen problems, water problems etc and I decided to make a system for it. Little did I know that it would totally revolutionise my own project and that it would teach me much needed jquery skills to further enhance the CMS. I also