IF Statement in CheckCommands

  • Dear all,

    I am running the great check_nwc_health check inside my Icinga installation. And for most checks the default tresholds are working for me. But I need to configure a different treshold for one check.

    What I would like to do configure an additional argument inside the check command with the IF Statement. But this seems not to be working for an string...

    Here is the code which I am tried to use. The new part is in the second code block again... But it is not working.

    Any hint for me?

    1. if ($check_nwc_health_mode$ == "cpu-load") {
    2. arguments += {
    3. "--warning"=25
    4. "--critical"=35
    5. }
    6. }
  • That's not possible this way, since the configuration attributes are static and you don't have the value of $check_nwc_health_mode$ yet. These are defined in the service object, thus requiring you to evaluate that as runtime function.

    One thing I would try to avoid - move the threshold logic into CheckCommands. They are widely used for any type of arguments and should be defined just one.

    I'd move the threshold logic into the service apply rule.

    1. arguments = {
    2. //anything above
    3. "--warning" = {
    4. value = "$nwc_health_warning$"
    5. }
    6. }

    1. apply Service "nwc-cpu-load" {
    2. ...
    3. vars.nwc_health_warning = 25
    4. }
  • Thank´s for your reply! But in your example the "--warning" treshold would be active for every check_nwc_health mode.

    Currently my apply rule is like this:

    1. apply Service "check_nwc_health" for (check_nwc_health_mode in host.vars.check_nwc_health_mode) {
    2. import "generic-service"
    3. enable_perfdata = false
    4. check_command = "check_nwc_health"
    5. vars += {
    6. "enable_notifications" = "true"
    7. "check_nwc_health_mode" = check_nwc_health_mode
    8. }
    9. assign where typeof(host.vars.check_nwc_health_mode) == typeof([])
    10. }

    Is there a chance to define the --warning only for the mode cpu-load?

  • No it is not. If you don't set the nwc_health_warning custom attribute, Icinga 2 cannot resolve any value for it and as such it won't be added to the command line. It is truly an optional argument, as can be seen in the docs.

    Try it with a simple example.

  • I had the same problem.

    As dnsmichi said you can put this in the service definition, but if you want different thresholds for the same service on different hosts, you can put it in the host definition.

    In the host definition you put something like
    vars.check_nwc_health.warning = "25"

    and in the service defintion

    if (host.vars.check_nwc_health_warning) { vars.nwc_health_warning = host.vars.check_nwc_health_warning }