Icinga 2: How to create global custom variables and use them in apply rules

Written by - 0 comments

Published on - Listed in Icinga Monitoring

The most common use case for writing apply rules is to use a custom variable which was defined in a Host or a Service object.

object Host "myhost" {
  import "generic-host"
  address = ""
  vars.os = "Linux"
  vars.interfaces = [ "ens33", "ens35" ]

A very simple use case would be an array of network interfaces and the apply rule just uses a for loop to apply service checks for each interface:

apply Service "Network IO " for (interface in host.vars.interfaces) {
  import "generic-service"
  check_command = "nrpe"
  vars.nrpe_command = "check_netio"
  vars.nrpe_arguments = [ interface ]
  assign where host.address && host.vars.interfaces && host.vars.os == "Linux"

A few other examples from older articles can be looked at here:

So far so good. But what if one wants to use a custom variable which is globally available, meaning across all hosts and all services?

For this purpose, such variables should be defined using the const keyword. A dedicated file where such global custom variables could be defined exists in the default packaged installation of Icinga 2: /etc/icinga2/constants.conf, but one is not obliged to use this file. The variable definition can also happen right in front of an apply rule (in the same .conf file). The custom variable can then be used inside the apply rule. Watch out for the correct usage how to "insert" the variable into the strings:

# Authentication Cookie:
const login_cookie = "eu8eek7aoTh9Aecheip4"

apply Service "HTTP Login Simulation on app.example.com" {
  import "generic-service"
  check_command = "http"
  vars.http_vhost = "app.example.com"
  vars.http_uri = "/login"
  vars.http_string = "Authenticated"
  vars.http_header = "Cookie: auth=" + login_cookie + ""
  vars.http_ssl = true
  vars.http_sni = true
  assign where match("appserver*", host.name)

Icinga 2 will insert the variable's value into the http_header variable. In the Icingaweb2 user interface this can be verified:

Icingaweb2 global custom variable used in apply rule

Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.