Zabbix: solve memory and cpu load monitoring issues inside LXC containers

Andrei Kvapil
Nov 29, 2017 · 2 min read

Zabbix have some problems with memory collecting from cgroups limited containers.
If you using Promxox, you know what I mean: The available memory collected worng without calculating buffers and cache memory.
Zabbix have bug report, but it seems that no one don’t want to fix it soon.
So let’s fix it together byself.

First we need to create new user parameter for zabbix, I used ct.memory.size name similarly built-in vm.memory.size parameter, but with my own single line script.

Please be sure that you have newest version of free tool, 3.3.10 and higher, because old one have different output format.

$ free -V
free from procps-ng 3.3.10

Edit /etc/zabbix/zabbix_agentd.conf:

Add user parameter for retrieve memory information:

UserParameter=ct.memory.size[*],free -b | awk '$ 1 == "Mem:" {total=$ 2;  used=($ 3+$ 5); pused=(($ 3+$ 5)*100/$ 2); free=$ 4; pfree=($ 4*100/$  2); shared=$ 5; buffers=$ 6; cache=$ 6; available=($ 6+$ 7);  pavailable=(($ 6+$ 7)*100/$ 2); if("$1" == "") {printf("%.0f", total )}  else {printf("%.0f", $1 "" )} }'

Add another one for retrieve swap information:

UserParameter=ct.swap.size[*],free -b | awk '$ 1 == "Swap:" {total=$ 2;  used=$ 3; free=$ 4; pfree=($ 4*100/$ 2); pused=($ 3*100/$ 2); if("$1" ==  "") {printf("%.0f", free )} else {printf("%.0f", $1 "" )} }'

Add another one for retrieve right CPU load information:

UserParameter=ct.cpu.load[*],uptime | awk -F'[, ]+' '{avg1=$(NF-2); avg5=$(NF-1); avg15=$(NF)}{print $2/'$(nproc)'}'

Or just download and copy my zabbix_container.conf.

It will provide you support for next parameters:


Don’t forget to restart zabbix-agent.service after

Ok, now we can check is our parameter working from zabbix server:

$ zabbix_get -s <container_ip> -k ct.memory.size[available]

Ok it’s working.

Now let’s configure zabbix for use them. In Zabbix Interface:

Go Configuration → Templtes

  • Make full copy “Template OS Linux” to “Template Linux Container”
  • Open “Template Linux Container” → Items
  • Replace all vm.memory.size items to ct.memory.size.
  • Replace all system.swap.size items to ct.swap.size:
    You also need to remove commas in key filed here. Example:
    replace system.swap.size[,free] to ct.swap.size[free]
  • Replace all system.cpu.load[percpu,*] items to ct.cpu.load[percpu,*.

Or just download and import my zabbix template.

Next, go to the Configuration → Hosts

  • Unlink and clear “Template OS Linux” from your hosts
  • Attach “Template Linux Container”

Wait some time and check the graphics:

Image for post
Image for post

Job is done!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store