Help understand Lambda Closures / Language Syntax

Hi,

I found great function wrote by somebody earlier that monitor overdue services. Trying to however change it into monitoring overdue services per service group (taken as parameter).

As you can say may need some work (but works from console) for example:

  • Need to figure out how I can merge 2 filter arguments (so overdue + in groups - now just doing this stupid if/if thing)
  • Same with passive (want to ignore these)
globals.getLateServicesPerEnv = function(env) {
  var res = []
  var env_filter = env + "-services"
  for (s in get_objects(Service).filter(s => s.last_check < get_time() - 2 * s.check_interval)) {
    if (env_filter in s.groups) {
        if (s.check_command == "passive") {
            true
        } else {
          res.add(s.__name)
        }
    }
  }
  return res
}

Calling this like that in my check


apply Service "ic2_overdue_checks-" for (key => config in host.vars.overdue_checks) {
....
  vars.overdue_services = {{ return getLateServicesPerEnv(key) }}

....

So in my host have simple vars.overdue_services[“SOMETHING”'] = { } and I want to see only overdue checks for SOMETHING-services group that are not passive checks.

But this is no go - key value is not defined. I think it’s because this that I need to implement:
https://icinga.com/docs/icinga-2/latest/doc/17-language-reference/#lambda-expressions-with-closures

But not so good in IC2 syntax - are anyone able to assist what need to be added?

And if you already stop laughing then feel free also to simplify my function!

Thanks
D

Hi Dariusz!

You seem to want

vars.overdue_services = () use(key) => getLateServicesPerEnv(key)

Best,
A/K