Using Zabbix to monitor lxc / lxd containers
published on
Note: This article is based on the work found at https://github.com/kvaps/zabbix-linux-container-template that has been done by others. This article simply updates that work to for Zabbix 7. However, credit should go to the original author.
Introduction
Zabbix doesn't collect memory information from lxc containers properly and so the following 'intergration' or 'workaround' has been put together to fix this.
Implement the fix
First, login to the container you wish to monitor and create a Zabbix container configuration file in the agent folder (presuming you are using Zabbix agent and not Zabbix agent v2) by running sudo nano /etc/zabbix/zabbix_agentd.d/zabbix_container.conf.
Add the following into this file:
UserParameter=ct.memory.size[*],free -b | awk 'NR==2 {total=$ 2; used=($ 3+$ 5); pused=(($ 3+$ 5)*100/$ 2); free=$ 4; pfree=($ 4*100/$ 2); shared=$ 5; buffers=$ 6; cached=$ 6; available=$ 7; pavailable=($ 7*100/$ 2); if("$1" == "") {printf("%.0f", total )} else {printf("%.0f", $1 "" )} }'UserParameter=ct.swap.size[*],free -b | awk 'NR==3 {total=$ 2; used=$ 3; free=$ 4; pfree=($ 4*100/$ 2); pused=($ 3*100/$ 2); if("$1" == "") {printf("%.0f", free )} else {printf("%.0f", $1 "" )} }'UserParameter=ct.cpu.load[*],cut -d" " -f1-3 /proc/loadavg | awk -F'[, ]+' '{avg1=$(NF-2); avg5=$(NF-1); avg15=$(NF)}{print $2/'$(nproc)'}'UserParameter=ct.uptime,cut -d"." -f1 /proc/uptime
Now restart the Zabbix agent with sudo systemctl restart zabbix-agent.
Update the Zabbix GUI
In the Zabbix GUI select Data Collection then Templates. Click on Linux by Zabbix agent (for passive checks) or Linux by Zabbix agent active (for active checks) and click on Clone.
Once you've given it a new name and clicked on Add you need to find the Items section of the new template.
- Find any reference to
vm.memory.sizeand change them toct.memory.size. - Find any reference to
system.cpu.loadand change them toct.cpu.load. - Find any reference to
system.swap.sizeand change them toct.swap.size. You also need to remove the comma inside the square brackets too.
Once you've done all of that select Data Collection from the menu on the far left then choose Hosts. Click on the name of a host which is a container. Under the templates section unlink and clear the Linux by Zabbix agent template and then attach your new template you created earlier.
Finally click on Update and enjoy your container reporting the correct values back to Zabbix!
Conclusion
This is just one of the many integrations which you can explore and work with that can be found at https://www.zabbix.com/integrations.
