Icinga 2: Using functions in custom attributes

by | Feb 11, 2015

Starting with version 2.2 Icinga supports arrays and dictionaries in custom attributes. In combination with apply this is incredibly powerful, e.g. to define HTTP vhosts for a host and to set up individual services for each of these vhosts.
Version 2.3 – which we’re planning to release on the 10th of March – will introduce support for using functions in custom attributes:

object CheckCommand "random-text" {
  import "plugin-check-command"
  command = [ PluginDir + "/check_dummy", "0", "$text$" ]
  vars.text = {{ Math.random() * 100 }}
}

The two curly braces are used to define a function. Icinga runs this function every time it needs the value for the custom attribute “text”. In this example this results in a new random value each time this check command is executed.
However, using functions we’re not limited to calculating simple values. Users can use if/else to accomplish more complex things:

vars.text = {{
  if (host.address == "127.0.0.1") {
    log("This is a check for localhost.")
  }
  return "Test"
}}

We can also access arbitrary attribute for other hosts and services:
 

vars.text = {{
  "The state for 'other-host' is: " + get_host("other-host").state
}}

You May Also Like…

Releasing Icinga 2.13.5

Releasing Icinga 2.13.5

Today we are releasing Icinga 2.13.5. It addresses issues with 2.13.x-only changes such as logging startup messages to...

Icinga Camp Berlin 2022

Icinga Camp Berlin 2022

Our first Icinga Camp after 2019 now took place almost 3 years later and was an amazing event that took us right back...

Subscribe to our Newsletter

A monthly digest of the latest Icinga news, releases, articles and community topics.