Create our own plugins for Check_MK and WATO

For a long time I used Nagios Core without Check_MK or any other GUI for configuration. I used pynag for massive changes, but nothing else.

When I needed to check for something specific, I just wrote what I needed on bash, put as many arguments/parameters/variables I wanted and added it to the commands.cfg file.

But with Check_MK and WATO, that’s a little different. We could add whatever we want as script but configure the arguments it’s not so easy (it’s not hard either). Mathias Kettner explains it very well on the documentation, but I wanted to have my own experience on my blog.

I won’t write about how to do a script. I will just give an example about what I did.

Necessary files

We will create 3 files: the plugin itself – the check function – the manual page

/usr/lib/check_mk_agent/plugins/lxc_mem
/usr/share/check_mk/checks/lxc_mem
/usr/share/doc/check_mk/checks/lxc_mem.mem

We also will modify this one:

/usr/share/check_mk/web/plugins/wato/check_parameters.py

Files content

The plugin itself

This is a simple bash script that checks the memory of LXC containers from a Proxmox Physical server.

#!/bin/bash

echo '<<<lxc_mem>>>'

CGROUP_PATH=/sys/fs/cgroup/memory/lxc

for container in `find $CGROUP_PATH/* -maxdepth 0 -type d`
do

TOTAL_MEM=`cat $container/memory.limit_in_bytes`
USED_MEM=`cat $container/memory.usage_in_bytes`
PERCENT=$((100*$USED_MEM/$TOTAL_MEM))

TOTAL_MEM=$(echo "($TOTAL_MEM / 1024 / 1024)" | bc)
USED_MEM=$(echo "($USED_MEM / 1024 / 1024)" | bc)

container=`echo $container| awk -F '/' '{print $7}'`
container=`pct list | grep $container | awk '{print $3}'`

echo "$container $USED_MEM $TOTAL_MEM $PERCENT"
done

This would check every running container on the server and show the memory usage, total memory and the percentage.

The check function

This is a simple bash script that checks the memory of LXC containers from a Proxmox Physical server.

# set default value of variable (user can override in main.mk)
lxc_mem_default_values = (90, 95)

# the inventory function
def inventory_lxc_mem(info):
   # loop over all output lines of the agent
   for line in info:
      container = line[0]   # container name is in the first column
      # use default variable as parameter. Note the quotes!
      yield container, "lxc_mem_default_values"

# the check function
def check_lxc_mem(item, params, info):
   # unpack check parameters
   warn, crit = params
   for line in info:
      if line[0] == item:
         used_mem = int(line[1])
         total_mem = int(line[2])
         percent_mem = int(line[3])
         perfdata = [ ( "used", percent_mem, warn, crit ) ]
         if percent_mem > crit:
            return 2, "CRITICAL - %d MB Used of %d MB - %d%% " % (used_mem, total_mem, percent_mem), perfdata
         elif percent_mem > warn:
            return 1, "WARNING - %d MB Used of %d MB - %d%% " % (used_mem, total_mem, percent_mem), perfdata
         else:
            return 0, "OK - %d MB Used of %d MB - %d%% " % (used_mem, total_mem, percent_mem), perfdata

# declare the check to Check_MK
check_info["lxc_mem.mem"] = {
    'check_function':            check_lxc_mem,
    'inventory_function':        inventory_lxc_mem,
    'service_description':       'LXC Memory usage',
    'has_perfdata':              True,
    'group':                     'lxc_mem_params',
}

Here is where we define the default values for the check, and the complete logic of the service check.

The manual page

The manual is optional. It’s what we will see on WATO to understand what the check does and how to configure it.

title: Container_Memory
agents: linux
catalog: os/kernel
license: GPL
distribution: check_mk
description:
 This check measures and checks the use of Memory on LXC containers from
 inside the Proxmox Server.

examples:
 # Set default levels to 90 and 95 percente of use
 lxc_mem_default_values = (90, 95).

checks = [
 # Use levels 80% (warning) and 90% (critical)
 # on all hosts with the tag "proxmox"
 (["proxmox"], ALL_HOSTS, "lxc_mem.mem", None, (80, 90)),
 ]
perfdata:
 Two values: Memory usage.

inventory:
 Automatically.

[parameters]
warning (int): Warning memory limit.
critical (int): Critical memory limit.

[configuration]
lxc_mem_default_values (int, int): A pair of integers: The default
 levels. These levels are used for inventorized checks. This variable
 is preset to {(90, 95)}

The check_parameters.py

Here we have to add the variables that we want to can change on WATO.

register_check_parameters(
    subgroup_os,
    "lxc_mem_params",
    _("LXC Memory Usage"),
        Tuple(
                help = _("LXC Memory Usage"),
                elements = [
                  Integer(title = _("Warning if above"), unit = _("%")),
                  Integer(title = _("Critical if above"), unit = _("%"))
                ]
        ),
    TextAscii(
        title = _("Service Description"),
        allow_empty = False
    ),
    match_type = "dict",
)

 

 

Reference Links:

Print Friendly, PDF & Email

Pablo Javier Furnari

Linux System Administrator at La Plata Linux
I'm a Linux Sysadmin with 8 years of experience. I work with several clients as a consulter here in Argentina and oversea (I have clients in the United States, Mexico, Pakistan and Germany).

I know my strengths and weaknesses. I'm a quick learner, I know how to work with small and big teams. I'm hard worker, proactive and I achieve everything I propose.

Leave a Reply

Your email address will not be published. Required fields are marked *


CAPTCHA Image
Reload Image