"macro()" not callable for "env" dictionary or how to get custom variable value based on other cust. variab. value

Hello,
I’m looking for your wisdom … I’d like to process a value from a custom variable based on value from other custom/standard variable. I found a solution with intermediate step, but is there also other solution?

Scenario:
I have 3 custom variables:

vars = {
  var_0 = "Clear"
  var_1 = "Warning"
  var_2 = "Critical"
}

and based on the actual service “state_id” (0 = OK, 1 = WARNING and 2 = CRITICAL) I’d like to use proper string from above defined custom variables (aka event severity mapping)

Within my NotificationCommand object definition I managed it like - get state_id, concatenate with “var_” string, get value of cust. variable matching the concatenated string. This works:

object NotificationCommand "notification-with-env" {
  command = [ "/etc/icinga2/scripts/notification.sh" ]

  vars = {
    state_severity = {{ return macro("$var_" + macro("$state_id$") + "$") }}
  }

  env = {
    STATE_ID = "$state_id$"
    STATE_SEVERITY = "$state_severity$"
  }
}

Questions:

  1. Is it possible to run “macro()” within the env dictionary (or other predefined function like “get_host()”)?
    e.g.:
  env = {
    STATE_SEVERITY = macro("$state_id$")       >> I know, assigning "$state_id$" just works, but question is about the macro() function 
  }

I get no value/no error …

  1. Is it possible to access the variables/runtime macros somehow directly within the env dictionary? I mean e.g. n-th entry from an array or to concatenate two cust. variables:
env {
  NTH_ENTRY = "$my_array["$state_id$"]$"      >> this troughs error obviously 

  CONCAT_STRINGS = "$var_" + "$state_id$" + "$"     >> no errors, it just returns "state_id" (ignoring "$var_" ...?)
}

Thanks for ideas.

Hello @gathiu!

I’m afraid the solution you’re already using already hits the technical limits.

Best,
AK

Hi @Al2Klimov,
thanks for the response. That’s also a positive info - at least I know I don’t need to spend more time searching for simpler solution :slight_smile:

I take it also as “functions cannot be executed within the env dictionary definition” - am I right? Any special reason for that?

  1. Not in your case.
  2. A looong story…