Connecting services and hosts in Icinga2

To minimize overall configuration text it might be convenient to list all host services in the same block where the host is defined. The “apply for ” construct allows to di such thing. For example:

 

object Host "myhost.com" {
   import "generic-host-template"
   address = "myhost.com"
   vars.ipmi_address = "myhost-mgmt.com"
   vars.ipmi_config = "/usr/local/etc/freeipmi/ipmi_myhost.cfg"
   vars.service.https["Myhost web"] = {port=8443}
   vars.service.ssh["Myhost ssh"] = {}
}

apply Service "HTTPS SRVS" for (service_name => config in host.vars.service.https) {
   import "generic-service-template"
   vars += config
   host_name = host.address
   if (vars.port == "") {
      vars.port = 443
   }
   vars.address = host.address
   if (vars.url == "") {
      vars.url = "https://" + host.address + "/"
   }
   check_command = "check_https_ext"

   assign where host.vars.owner == "it"
}

object CheckCommand "it_check_https_ext" {
   import "migration-check-command"
   import "plugin-check-command"
   command = "$USER1$/check_http -S -p $port$ -H $address$ -u $url$"
}