# Calculating command threshold with a host variable

**URL:** <https://community.icinga.com/t/calculating-command-threshold-with-a-host-variable/5134>\
**Category:** Icinga 2\
**Tags:** director\
**Created:** [July 31, 2020, 8:27am UTC](https://community.icinga.com/t/calculating-command-threshold-with-a-host-variable/5134 "2020-07-31T08:27:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![log1c](https://community.icinga.com/user_avatar/community.icinga.com/log1c/32/2071_2.png) [@log1c](https://community.icinga.com/u/log1c)\
**Post date:** [July 31, 2020, 8:27am UTC](https://community.icinga.com/t/calculating-command-threshold-with-a-host-variable/5134/1 "2020-07-31T08:27:54Z")

</div>

Hi all 🙂

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:

```auto
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:

```auto
                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:

```auto
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()

```auto
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”.

```auto
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:

```auto
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:

```auto
'/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 🙂

---

<div class="post-metadata">

**Author:** ![log1c](https://community.icinga.com/user_avatar/community.icinga.com/log1c/32/2071_2.png) [@log1c](https://community.icinga.com/u/log1c)\
**Post date:** [August 10, 2020, 1:50pm UTC](https://community.icinga.com/t/calculating-command-threshold-with-a-host-variable/5134/2 "2020-08-10T13:50:09Z")

</div>

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 :)?

---

<div class="post-metadata">

**Author:** ![fluxX](https://community.icinga.com/user_avatar/community.icinga.com/fluxx/32/133_2.png) [@fluxX](https://community.icinga.com/u/fluxX)\
**Post date:** [August 17, 2020, 6:31am UTC](https://community.icinga.com/t/calculating-command-threshold-with-a-host-variable/5134/3 "2020-08-17T06:31:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![log1c](https://community.icinga.com/user_avatar/community.icinga.com/log1c/32/2071_2.png) [@log1c](https://community.icinga.com/u/log1c)\
**Post date:** [August 17, 2020, 7:08am UTC](https://community.icinga.com/t/calculating-command-threshold-with-a-host-variable/5134/4 "2020-08-17T07:08:40Z")

</div>

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):

> <https://github.com/Icinga/icingaweb2-module-director/issues/2174>
>
> Expected Behavior
> I would like to be able to specify a calculation inside a variable field in the Director.
> E.g.
> template Service "linux-by\_ssh-cpu" {
> ...
