Header RSS Feed
» Nagios/Monitoring Plugins

Monitoring Plugin: check_lxc

Last Update: December 05, 2018

This is a plugin to monitor Linux Containers (LXC). It needs to run on the LXC host and allows to check CPU, Memory, Swap usage of a container. The plugin also allows to check for an automatic boot of a container.

Important note on cpu check: It is important to understand what the cpu check does. It will not tell you the usage of the CPU itself. Instead the number of jiffies (time spent on CPU) of a container is compared to the total number of jiffies of the host during a given period (default 5s, can be modified with -s parameter). This means the plugin's cpu check will tell you how much a container can be accounted for of the current cpu usage. This is not bullet proof, but it helps to find the most busy containers.

Jump to...
Version History
Requirements for memory check
Definition of parameters
Command definition
Nagios and Icinga 1 service definition examples
Icinga 2 service definition examples


Download check_lxc check_lxc.sh
Download plugin and save it in your Nagios/Icinga plugin folder (e.g. /usr/lib/nagios/plugins)
Contribute on https://github.com/Napsty/check_lxc
590 downloads so far...


Version History
# 20130830 Finished first check (mem)
# 20130902 Added cgroup kernel boot parameter check (cgroup_active)
# 20130902 Fixed previous cgroup check (see issue #1)
# 20130902 Activated lxc_exists verification (finally turned to lxc_running)
# 20130902 Added new check type (auto)
# 20130912 Reorganizing code, put output calculation into function
# 20130912 Added new check type (swap)
# 20130913 Bugfix in swap check warning calculation
# 20160316 Make plugin work with LXC 1.x, too
# 20160316 In LXC 1.x, lxc-cgroup command needs sudo
# 20160318 Additional checks if swap value can be read
# 20160318 Perfdata of mem check: Only show 'max' when thresholds set
# 20160318 Adapt lxc_running function to work on 1.x, too
# 20160318 Add warn and crit values into mem check perfdata
# 20160318 Remove sudo commands within plugin, whole plugin requires sudo
# 20170710 Added cpu check type
# 20181203 Merged PR #4, #5 from BarbUk. Update GPL address. Increase version.
# 20181203 Fix issue #9 (added lxc-cgroup sanity check)
# 20181204 Merged PR #6 from BarbUk (shellcheck)


- Plugin must be executed with sudo
- LXC commands
- cgroups enabled
- Other bash relevant commands as expr (plugin checks for its existance)


Requirements for memory check
To be able to run the memory check (-t mem), the cgroup subsys "memory" must be enabled. You can verify this manually by running:

cat /proc/cgroups | grep memory

If the first value is not enabled (0), then add the following options as your kernel boot parameter: "cgroup_enable=memory" and "swapaccount=1". In Debian this can be done by modifying /etc/default/grub2 followed by a update of the grub2 and reboot:

# cat /etc/default/grub | grep CMDLINE_LINUX_DEFAULT
GRUB_CMDLINE_LINUX_DEFAULT="quiet cgroup_enable=memory swapaccount=1"


Then verify if you can get the memory statistics of a container:

lxc-cgroup -n lxctest01 memory.stat


Definition of parameters

Parameter Description
-n * name of container (or ALL for some types)
-t * name of container (or ALL for some types)
-u unit of output values (k|m|g)
-w warning threshold (for memory makes only sense if limit is set in lxc config)
-c critical threshold (for memory makes only sense if limit is set in lxc config)
-s sleep in seconds between cpu checks (default: 5)
--help Help!


Definition of check types

Type Description
mem Check the memory usage of the given container (thresholds in percent). Thresholds make only sense if you have limited the container's memory resources in the first place.
swap Check the swap usage (thresholds in MB)
cpu Check cpu usage (percentage) of a container (thresholds in percent)
auto Check autostart of a container or all containers (-n ALL)


NRPE Command definition for simple checks without thresholds in your nrpe.cfg

command[check_lxc]=sudo /usr/lib/nagios/plugins/check_lxc.sh -n $ARG1$ -t $ARG2$

NRPE command definition for check with thresholds

command[check_lxc_thresholds]=sudo /usr/lib/nagios/plugins/check_lxc.sh -n $ARG1$ -t $ARG2$ -w $ARG3$ -c $ARG4$


Nagios and Icinga 1 service check example using NRPE

# Check cpu usage of lxc mylxc01
define service{
  use generic-service
  host_name lxchost1
  service_description LXC CPU mylxc01
  check_command check_nrpe!check_lxc!mylxc01!cpu

In this example, the cpu usage check happens on host lxchost1, executed by NRPE. No thresholds were given. This means that there will be no alerts but the check is rather used for graphing and information.


Icinga 2 service check example using NRPE

# Check memory usage of lxc mylxc01
object Service "LXC Memory Usage mylxc01" {
  import "generic-service"
  host_name = "lxchost1"
  check_command = "nrpe"
  vars.nrpe_command = "check_lxc_thresholds"
  vars.nrpe_arguments = [ "mylxc01", "mem", "80", "95" ]

In this example, the memory usage check happens on lxchost1, executed by NRPE. Warning threshold is set to 80%, critical threshold is set to 90%. As soon as the container mylxc01 uses more than 80% of its memory capacity (when cgroup limits were defined), the alerts will be triggered.



Go to Homepage home
Linux Howtos how to's
Monitoring Plugins monitoring plugins
Links links

Valid HTML 4.01 Transitional
Valid CSS!
[Valid RSS]

6905 Days
until Death of Computers