The creators of Cfengine 3 claim that their configuration management tool is able to perform a basic monitoring of important states, such as the CPU load. And indeed, with the help of Cfengine 3 you possibly get rid of some monitoring checks and therefore relive your monitoring system. This is doable because cf-monitord runs in […]

Author:

The creators of Cfengine 3 claim that their configuration management tool is able to perform a basic monitoring of important states, such as the CPU load. And indeed, with the help of Cfengine 3 you possibly get rid of some monitoring checks and therefore relive your monitoring system. This is doable because cf-monitord runs in the background and collects system information which can be used by cf-agent.

The following code snippet was inspired by Aleksey from Vertical Sysadmin. Please note that I use Cfengine 3.3.5 on Linux Mint.

Writing the Cfengine 3 code

body common control {
        version         => "1.0";
        inputs          => { "cfengine_stdlib.cf" };
        bundlesequence  => { "monitor_cpu_load" };
}

bundle agent monitor_cpu_load {
  vars: # <--- promise type
        "max_load" int => "2";  # <--- promiser

  classes:
        "tooHighCPULoad" # <--- class
          expression => isgreaterthan("$(mon.value_cpu)", "$(max_load)");

  reports:
        tooHighCPULoad::  # <--- "if condition is met" or "if class is set"
          "The current CPU load of $(mon.value_cpu) exceeds the max. allowed load of $(max_load)!";
}

(I saved the file as “example10.cf”.)

Ok, let’s check the syntax now:

/var/cfengine/bin/cf-promises -f /etc/cfengine3/example10.cf

No errors found? Good, then let’s run the Cfengine 3 script:

/var/cfengine/bin/cf-promises -f /etc/cfengine3/example10.cf

I selected a load of 2 which would be ok for my dual core VM. In my case, Cfengine 3 caused a load of 3 when executing this code snippet – that is why my syslog contained the pre-defined warning:

tail -f /var/log/syslog
Aug 15 23:54:22 mintbox cf3[9283]: R: The current CPU load of 3 exceeds the max. allowed load of 2!

Analyzing the Cfengine 3 code snippet
Assuming that you are already familiar with the “body common control” part, we are starting with the main section:

bundle agent monitor_cpu_load {
  vars: # <--- promise type
        "max_load" int => "2";  # <--- promiser

Here we define a promise of the type “vars” with the promiser “max_load”. This variable contains the threshold for the CPU load.

In this part…

  classes:
        "tooHighCPULoad" # <--- class
          expression => isgreaterthan("$(mon.value_cpu)", "$(max_load)");

…the promise of the type “classes” defines the class “tooHighCPULoad” with the help of an expression. If the current CPU load (“mon.value_cpu) is greater than the defined load (“max_load”), the expression becomes true and the promise can be considered made for the duration of the current Cfengine 3 script run.
Expressions help us to define the borders of a condition which can be used for setting classes. The classes themselves then can trigger actions like restarting a service or reporting something to the system log.
Some example values for the promiser “expression” can be regarray, iprange, userexists, isnewerthen, classify, fileexists, groupexists, hashmatch, hostinnetgroup, hostrange, isdir, isexecutable, islessthan, islink, isplain, isvariable, latherthan, ldaparray, regcmp, regextract, regline, reglist, remoteclassesmatching, returnszero, splayclass or usemodule.

Now let’s have a look at the last lines of the code snippet:

  reports:
        tooHighCPULoad::  # <--- "if condition is met" or "if class is set"
          "The current CPU load of $(mon.value_cpu) exceeds the max. allowed load of $(max_load)!";
}

The promise of the type “reports” contains a condition (“tooHighCPULoad::”). If this class is set, the defined text string will be reported (e.g. to /var/log/syslog).

Summary
With this Cfengine 3 code example we can prove that this configuration management solution indeed is capable of monitoring some basic stuff. With a few tricks you can even enhance your Cfengine 3 scripts to monitor open ports (e.g. by calling nmap or netstat) and HTTP headers.

If you want to you can download today’s Cfengine 3 code snippet here.

Comments on this entry (1 comment)

Did you like this post? You can share your opinion with us! Simply click here.

Add Your Comment

You must be logged to comment.