Pass service custom var to another services checks

Hi all,
I need to select a custom vars from specific services from a service group :slight_smile:

means… I have services assigned to a group “A12” I need to select the custom var called “CV001” from all these services and pass it as argument to a command/service check.

is that possible ?

icinga2 version: r2.8.1-1
Platform version: 16.04.4 LTS (Xenial Xerus)

How exactly would these multiple custom attribute values be compiled into one parameter value?

could it be passed as --CVAR “Value1” --CVAR “Value2” --CVAR “ValueN”

Actually I need the Value to be “SVC.Name=CV001(Value)”… and passed to --CVAR “my.service.name=5”

I do this, because i have a service check for more than 100 services per service from a http server page.

I don’t need to hit that http very frequently, so I want to pass some of params of those services(inside the page), to the main service check.

So basically your resulting value for this custom attribute could be an array. Your CheckCommand uses just that, and sets repeat_key = true (look into the disk command for examples).

Well, you probably need to understand the magic behind functions, runtime lambda functions, custom attributes and object accessors then.

Resources

Custom Attributes

https://www.icinga.com/docs/icinga2/latest/doc/03-monitoring-basics/#custom-attribute-values

Functions

https://www.icinga.com/docs/icinga2/latest/doc/17-language-reference/#abbreviated-lambda-syntax

Object Accessors

https://www.icinga.com/docs/icinga2/latest/doc/18-library-reference/#get_objects
https://www.icinga.com/docs/icinga2/latest/doc/08-advanced-topics/#access-object-attributes-at-runtime

More

https://www.icinga.com/docs/icinga2/latest/doc/03-monitoring-basics/#functions-as-custom-attributes
https://www.icinga.com/docs/icinga2/latest/doc/17-language-reference/#operators
https://www.icinga.com/docs/icinga2/latest/doc/18-library-reference/#dictionarycontains
https://www.icinga.com/docs/icinga2/latest/doc/18-library-reference/#arrayadd

Example

Below is an idea from my mind, nothing tested.

apply Service "a12-group-check" {
  check_command = "group-check"

  //runtime evaluate lambda function
  vars.group_check_params = {{ 
    var result = []

    for(s in get_objects(Service)) {
      //skip all services which do not belong to group "A12"
      if ("A12" !in s.groups) {
        continue;
      }

      //found a service, look if it provides our value
      if(s.vars.contains("CV001")) {
         result.add(s.vars.CV001)
      }

      return result

    }

  }}

  assign where ...
}
object CheckCommand "group-check" {

  arguments = {
     "--CVAR" = "$group_check_params$"
    .....
   }
}
  

## Notes

This solution is expensive if you have many services (obviously the loop). If you are not familiar with all the above, start simple with the debug console and learn the DSL and its expression. Tackle how lambda functions work in general, and start developing your code bit by bit.
3 Likes

Hi dnsmichi!
Thanks a lot for your help…

I could do the below

myGroup=macro("$SVCGroup$")
var result = []
for(s in get_objects(Service)) { 
    if ( myGroup in s.groups ) {
# continue;  } ## commented
        if(s.vars.contains("Sessions")) {
            var tmpresult=  s.name + "=" + s.vars.Sessions;
            result.add(tmpresult);
            }
    }
}
return result

the if ("A12" !in s.groups) wasn’t matching for me :frowning: so I commented it out and reversed the logic to match and include the rest of logic inside it… is it harmful that way?

I add it directly to the argument … in the command definition…
does that mean that every check it will do resolve this function every time ? if yes, could it be added to field and/or cached somewhere ? I have around 70 main service with sub-services of 100 each.

I’m using the director latest version as of today.

That snippet only works inside static configuration files, that won’t be possible inside the Director itself.

If the reversed logic works for you, very nice. I did not test my example, it was just an idea how I would code it.