Calculating command threshold with a host variable

Hi all :slight_smile:

I’m trying to figure out a way to use a host variable in a check command and calculate a threshold from it. If that is even possible.

The host objects have a variable host.vars.vm_cpu_cores which the get via an import&sync. This variable simply holds the number of cpu cores as an integer value.

Now I would like to “dynamically” change the thresholds for the load check based on what the hosts cores.

by_ssh is used as check command:

object CheckCommand "by_ssh_check_load" {
        import "by_ssh"

        vars.by_ssh_command = [ PluginDir + "/check_load" ]
        vars.by_ssh_arguments = {
                "-w" = {
                        value = "$load_wload1$,$load_wload5$,$load_wload15$"
                }
                "-c" = {
                        value = "$load_cload1$,$load_cload5$,$load_cload15$"
                }
        }
...
}

Now I have tried various approaches to accomplish what I want:

                vars.load_cload1 = number("$host.vars.vm_cpu_cores$")
                vars.load_cload15 = number("$host.vars.vm_cpu_cores$")
                vars.load_cload5 = number("$host.vars.vm_cpu_cores$")
                vars.load_wload1 = number("$host.vars.vm_cpu_cores$")
                vars.load_wload15 = number("$host.vars.vm_cpu_cores$")
                vars.load_wload5 = number("$host.vars.vm_cpu_cores$")

                vars.load_cload1 *= 2
                vars.load_cload5 += 2
                vars.load_wload1 += 1
                vars.load_wload15 -= 1

Result:

critical/config: Error: Error while evaluating expression: Can't convert '$host.vars.vm_cpu_cores$' to a floating point number.
    Location: in /etc/icinga2/zones.d/global-satellites/commands-byssh.conf: 136:22-136:55
    /etc/icinga2/zones.d/global-satellites/commands-byssh.conf(134):         }
    /etc/icinga2/zones.d/global-satellites/commands-byssh.conf(135):
    /etc/icinga2/zones.d/global-satellites/commands-byssh.conf(136):   vars.load_cload1 = number("$host.vars.vm_cpu_cores$")
                                                                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    /etc/icinga2/zones.d/global-satellites/commands-byssh.conf(137):          vars.load_cload15 = number("$host.vars.vm_cpu_cores$")
    /etc/icinga2/zones.d/global-satellites/commands-byssh.conf(138):          vars.load_cload5 = number("$host.vars.vm_cpu_cores$")

If I encase the whole vars. part with { ... } and remove the number()

object CheckCommand "by_ssh_check_load" {
        import "by_ssh"

        vars.by_ssh_command = [ PluginDir + "/check_load" ]
        vars.by_ssh_arguments = {
                "-w" = {
                        value = "$load_wload1$,$load_wload5$,$load_wload15$"
                }
                "-c" = {
                        value = "$load_cload1$,$load_cload5$,$load_cload15$"
                }
        }
{
                vars.load_cload1 =  "$host.vars.vm_cpu_cores$"
                vars.load_cload15 = "$host.vars.vm_cpu_cores$"
                vars.load_cload5 = "$host.vars.vm_cpu_cores$"
                vars.load_wload1 = "$host.vars.vm_cpu_cores$"
                vars.load_wload15 = "$host.vars.vm_cpu_cores$"
                vars.load_wload5 = "$host.vars.vm_cpu_cores$"

                vars.load_cload1 *= 2
                vars.load_cload5 += 2
                vars.load_wload1 += 1
                vars.load_wload15 -= 1
}
...

the config validation is complaining about the whole package saying “Value computed is not used”.

critical/config: Error: Value computed is not used.
Location: in /etc/icinga2/zones.d/global-satellites/commands-byssh.conf: 135:3-147:3
/etc/icinga2/zones.d/global-satellites/commands-byssh.conf(133):                 }
/etc/icinga2/zones.d/global-satellites/commands-byssh.conf(134):  }
/etc/icinga2/zones.d/global-satellites/commands-byssh.conf(135):   {
                                                                   ^^
/etc/icinga2/zones.d/global-satellites/commands-byssh.conf(136):   vars.load_cload1 =  "$host.vars.vm_cpu_cores$"
                                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/etc/icinga2/zones.d/global-satellites/commands-byssh.conf(137):          vars.load_cload15 = "$host.vars.vm_cpu_cores$"
                                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/etc/icinga2/zones.d/global-satellites/commands-byssh.conf(138):          vars.load_cload5 = "$host.vars.vm_cpu_cores$"
                                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/etc/icinga2/zones.d/global-satellites/commands-byssh.conf(139):          vars.load_wload1 = "$host.vars.vm_cpu_cores$"
                                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/etc/icinga2/zones.d/global-satellites/commands-byssh.conf(140):          vars.load_wload15 = "$host.vars.vm_cpu_cores$"
                                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/etc/icinga2/zones.d/global-satellites/commands-byssh.conf(141):          vars.load_wload5 = "$host.vars.vm_cpu_cores$"
                                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/etc/icinga2/zones.d/global-satellites/commands-byssh.conf(142):

/etc/icinga2/zones.d/global-satellites/commands-byssh.conf(143):   vars.load_cload1 *= 2
                                                                 ^^^^^^^^^^^^^^^^^^^^^^^
/etc/icinga2/zones.d/global-satellites/commands-byssh.conf(144):   vars.load_cload5 += 2
                                                                 ^^^^^^^^^^^^^^^^^^^^^^^
/etc/icinga2/zones.d/global-satellites/commands-byssh.conf(145):   vars.load_wload1 += 1
                                                                 ^^^^^^^^^^^^^^^^^^^^^^^
/etc/icinga2/zones.d/global-satellites/commands-byssh.conf(146):   vars.load_wload15 -= 1
                                                                 ^^^^^^^^^^^^^^^^^^^^^^^^
/etc/icinga2/zones.d/global-satellites/commands-byssh.conf(147):   }
                                                                 ^^^

I have also tried doing this in the Director:

template Service "linux-by_ssh-cpu" {
    import "generic-service-template"

    check_command = "by_ssh_check_load"
    vars.load_cload1 = "$host.vars.vm_cpu_cores$ *2"
    vars.load_cload15 = "$host.vars.vm_cpu_cores$"
    vars.load_cload5 = "$host.vars.vm_cpu_cores$ +2"
    vars.load_wload1 = "$host.vars.vm_cpu_cores$ +1"
    vars.load_wload15 = "$host.vars.vm_cpu_cores$ -1"
    vars.load_wload5 = "$host.vars.vm_cpu_cores$"
}

The host variable gets resolved without problems, but no calculation is happening(what I pretty much expected)
Executed command line:

'/usr/lib64/nagios/plugins/check_by_ssh' '-C' ''\''/usr/lib64/nagios/plugins/check_load'\'' '\''-c'\'' '\''2 *2,2 +2,2'\'' '\''-w'\'' '\''2 +1,2,2 -1'\''' 

Long story short:
Is what I’m trying to do even possible?
Does anyone know a better way to achieve something like this without having different templates/apply rules for each number of cores?

Best regards and thanks in advance :slight_smile:

I’ll bump this up again.
Does someone have an idea or wants to tell me that what I’m trying to achieve is not possible :)?

Hi,

i’ve only tested this with an apply service. Host:

object Host NodeName {
    import "generic-host"
    ...
    vars.vm_cpu_cores = 8
}

and the service:

apply Service "load" {
    import "generic-service"

    check_command = "load"

    vars.load_cload1 = host.vars.vm_cpu_cores * 2      // resolves to 16
    vars.load_cload5 = host.vars.vm_cpu_cores + 2      // resolves to 10
    vars.load_wload1 = host.vars.vm_cpu_cores + 1      // resolves to 11
    vars.load_wload15 = host.vars.vm_cpu_cores - 1     // resolves to 7

    assign where ....
}

You are defining here strings, so the calculation cannot work:

vars.load_cload1 = "$host.vars.vm_cpu_cores$ *2"
vars.load_cload15 = "$host.vars.vm_cpu_cores$"
vars.load_cload5 = "$host.vars.vm_cpu_cores$ +2"
vars.load_wload1 = "$host.vars.vm_cpu_cores$ +1"
vars.load_wload15 = "$host.vars.vm_cpu_cores$ -1"
vars.load_wload5 = "$host.vars.vm_cpu_cores$"

see above on my calculation or you can do something like this:

vars.load_cload1 = $host.vars.vm_cpu_cores$ * 2

Greetz

Hi Alex,

thanks for your input!
I should have stated more clearly that we would like to keep the service (template and apply rule) in the Director, so that we can override thrsholds for single hosts/services, for which the Director does “some magic”. Haven’t tried if this is still possible with services defined in config files tbh.

Iirc I had tried using host.vars.vm_cpu_cores with the $ inside the check command, which didn’t work (which I suspected, as it is a host variable)

As the Director does not support calculations inside of variable fields (yet?), I think we will have to stick to multiple service apply rules for the number of cpu cores and assign them accordingly.

edit: Have defined the apply rule with the calculation inside a config file. As expected, the Director does not know the service after deployment and therefore can’t override the variables.
I added a feature request for the Director to allow the calculation of variables (if even possible):

1 Like