Service state in notification command argument

Hi,

I’m using the notification object with commands to send SNMP traps to another system. This is working fine while using custom vars in my service.
I need to use our vars as the receiving system is using other states than the standard icinga service states - e.g.:
Icinga service state = “0” => Target system state = 2
icinga “1” => Target 1
Icinga “2” => Target 4

As you can imagine now, a critical event in Icinga would trigger a clear on the other side.

My question is now: How can I change the service.state within a command’s argument using DSL?
Currently I used things like:

if (service.state == 2){
  return 4
} else if (service.state == 1){
  return 1
} else {
  return 2
}

Not working - always a “2” is sent out.

Than I tried

var servicestate = macro("$service.state$")
if (servicestate == 2) {  //or "CRITICAL"
  return 4
}
...

Not working - always an empty string.
Maybe it’s quite simply, but currently I’m stucked…

System info:

  • Version used: icinga2 - The Icinga 2 network monitoring daemon (version: r2.14.2-1)
  • OS: Debian 11
  • Enabled features: api checker icingadb mainlog notification
  • Icinga Web 2 version: 2.12.1

Many thanks in advance

Do you use the director?

Do you use a lamda?

https://icinga.com/docs/icinga-2/latest/doc/17-language-reference/#lambda-expressions

Why not map the logic in the notification program? It’s probably easier.

Some, like the one for Teams, are DSL directly in the notification command without a separate script / program. But besides this caveat, a very valid approach.

Hi - I’m using the director - forgot the “small” info :wink:

this was just another test… therefore another code inside

Edit: sorry, need more coffee.

maybe some of this code helps

var output_status = ""
var total_count = 0
var ok_count = 0
var critical_count = 0
var service_host = macro("$host.name$")
var service_names = get_services(service_host).map(s => s.name)
var service_pattern = macro("$116_all_of_them_service_pattern$")

//output_status += service_host + " "
//output_status += service_pattern + " "

for (service_name in service_names) {
//output_status += service_name + " "
  if (match(service_pattern, service_name)) {
    if (get_service(service_host, service_name).state == 2 && get_service(service_host, service_name).state_type == 1) {
      critical_count += 1
    } else {
      ok_count += 1
    }
    total_count += 1
  }
}
...

Hello again,

so I finally got it running. May ideas were all ok so far - only the bash script being called had an issue :triumph:
So the final solution is looking like this:

var service_host = macro("$host.name$")
var service_name = "Reachability"
if ( get_service(service_host, service_name).state == 1){
    return 1
} else if (get_service(service_host, service_name).state == 2){
   return 4
} else {
    return 2
}

The only think which drove me crazy was sending a 0 (zero) as command argument value. It looks as Icinga is ommiting such value some how (or I didn’t find it in the doc).
By changing now
value type to Icinga DSL and
value to return 0
I could solve this also.

Many thanks to you all!

OR

1 Like