Icinga 2 Config Language (DSL): Advanced Apply Rules

by | Jan 14, 2021

As many users of Icinga don’t know what the DSL has to offer, I’m going to show you how to use custom variables and apply for rules to make your life easier when writing configuration for your Icinga environment.

Idea

In this example we will use custom variables on a host to configure a dynamic set of services to monitor multiple web services behind a reverse proxy.

Host definition

On the host we define a custom dictionary called http_vhosts and assign our virtual hosts to it. In our example a virtual host must have the attribute vhost set and can provide the attributes ssl and uri.

object Host "reverse-proxy-1" {
  import "generic-host"
  address = "10.0.10.5"

  vars.http_vhosts = {
    "icingaweb2" = {
      vhost = "icinga.domain.com"
      ssl = true
    }
    "grafana" = {
      vhost = "grafana.domain.com"
      ssl = true
    }
    "api_v1" = { 
      vhost = "api.domain.com" 
      ssl = true
      uri = "/v1"
    }
    "api_v2" = {
      vhost = "api.domain.com" 
      ssl = true
      uri = "/v2"
    }
    "status" = {
      vhost = "status.domain.com"
    }
  }
}

 

Service apply for definitions

The important part here is the for keyword behind the usual apply. This tells Icinga to iterate over every virtual host in host.vars.http_vhosts and execute the apply rule for each of them. As a result, you will have access to the variables key and config inside the scope of the apply rule.

apply Service "http_vhost_" for (key => config in host.vars.http_vhosts) {
  import "generic-service"
  check_command = "http"

  vars.http_vhost = config.vhost

  if (config.ssl) {
    vars.http_ssl = config.ssl
  }

  if (config.uri) {
    vars.http_uri = config.uri 
  }

  assign where host.vars.http_vhosts
}

 

Result

Our configuration has resulted in one service for each of our virtual hosts. The names use the name of the apply rule and the value of our key variable.

Conclusion

Using advanced apply rules with for loops allows you to only define a flexible rule set and configure everything you need on the hosts. This can be extended using functions and more complex dictionary structures to suit every infrastructures needs. If you want to learn more about what the Icinga DSL has to offer, take a look at the Language Reference in our documentation.

You May Also Like…

Code Reviews – How do they work?

Code Reviews – How do they work?

We at Icinga / NETWAYS (yes, that’s the order) held an internal event recently. It’s name was Knowledge Days and I got...

Subscribe to our Newsletter

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