Hi all,
we are currently stuck on debugging a function we created with the Icinga DSL to use the returned array in an apply rule.
The function looks like this and is located here on the config master /etc/icinga2/zones.d/global-satellites/functions.conf
# Get all hostnames with a http service check
globals.get_hosts_with_http_service = function() {
var http_services = get_objects(Service).filter(service => match("HTTPS_*", service.name))
var http_hosts = []
for (var http_service in http_services) {
if (http_service.vars.get("http_vhost")) {
if (http_service.vars.http_vhost) {
var http_service_object = {
"vhost" = http_service.vars.http_vhost
"hostname" = http_service.host_name
"service_owner" = http_service.vars.service_owner
}
http_hosts.add(http_service_object)
}
}
}
# Array with dictionaries
return http_hosts
}
Returned array http_hosts
should look like this (based on test on the console)
var hosts_with_httpservice = [ {
hostname = "abc-repo01"
service_owner = "null"
vhost = "repo-private.xyz.dev"
}, {
hostname = "monitoring-intern.xyz.dev"
service_owner = "null"
vhost = "monitoring-intern.xyz.dev"
}, {
hostname = "abc-pwm01"
service_owner = "someone"
vhost = "pwm.ad.xyz.dev"
}, {
hostname = "abc-repo01"
service_owner = "null"
vhost = "repo-public.xyz.dev"
} ]
Now we are trying to use the return value (array containing a dictionary) to apply services defined in /etc/icinga2/zones.d/global-satellites/services.conf
# Cert Checks for HTTPS Checks
var hosts_with_httpservice = get_hosts_with_http_service()
# debugger
for (http_obj in hosts_with_httpservice) {
apply Service "CERT_" + http_obj.vhost use (http_obj) {
import "http"
vars.http_certificate = "14,7"
vars.http_ssl = true
vars.jira_ticket_priority = "3"
vars.notification_period = "8x5"
vars.service_owner = http_obj.service_owner
assign where host.name == http_obj.hostname
}
}
With this config we don’t get any services applied to the host objects, so we started to debug with icinga2 daemon -C -X
.
Strangely the debugger does not have the variable hosts_with_httpservice
populated when stopping at the break point
[root@abc-ic-ma01 zones.d]# icinga2 daemon -CX
[2021-08-11 11:10:38 +0200] information/cli: Icinga application loader (version: 2.13.0-1)
[2021-08-11 11:10:38 +0200] information/cli: Loading configuration file(s).
Breakpoint encountered.
Location: in /etc/icinga2/zones.d/global-satellites/services.conf: 129:1-129:8
/etc/icinga2/zones.d/global-satellites/services.conf(127):
/etc/icinga2/zones.d/global-satellites/services.conf(128): var hosts_with_httpservice = get_hosts_with_http_service()
/etc/icinga2/zones.d/global-satellites/services.conf(129): debugger
^^^^^^^^
/etc/icinga2/zones.d/global-satellites/services.conf(130): for (http_obj in hosts_with_httpservice) {
/etc/icinga2/zones.d/global-satellites/services.conf(131): apply Service "CERT_" + http_obj.vhost use (http_obj) {
You can inspect expressions (such as variables) by entering them at the prompt.
To leave the debugger and continue the program use "$continue".
For further commands see "$help".
<1> => get_hosts_with_http_service()
[ ]
<2> =>
Interestingly another function we use, which is defined in /etc/icinga2/zones.d/master/functions.conf
we have the same behavior in the debugger, but the services based on mgmt_zones
still get applied
Function:
globals.get_satellite_zones = function() {
var mgmt_zones = []
var all_zones = get_objects(Zone)
for (sat_zone in all_zones) {
if (match("master", sat_zone.all_parents)){
mgmt_zones.add(sat_zone.name)
}
}
return mgmt_zones
}
Debugger
[root@abc-ic-ma01 zones.d]# icinga2 daemon -CX
[2021-08-11 10:51:03 +0200] information/cli: Icinga application loader (version: 2.13.0-1)
[2021-08-11 10:51:03 +0200] information/cli: Loading configuration file(s).
Breakpoint encountered.
Location: in /etc/icinga2/zones.d/master/services.conf: 20:1-20:8
/etc/icinga2/zones.d/master/services.conf(18): assign where regex(".*-o365-tenant", host.name)
/etc/icinga2/zones.d/master/services.conf(19): }
/etc/icinga2/zones.d/master/services.conf(20): debugger
^^^^^^^^
/etc/icinga2/zones.d/master/services.conf(21): apply Service "cluster-zone-" for (mgmt_zone in get_satellite_zones()){
/etc/icinga2/zones.d/master/services.conf(22): import "icinga-cluster-zone-status"
You can inspect expressions (such as variables) by entering them at the prompt.
To leave the debugger and continue the program use "$continue".
For further commands see "$help".
<1> => get_satellite_zones()
[ ]
Service apply:
apply Service "cluster-zone-" for (mgmt_zone in get_satellite_zones()){
import "icinga-cluster-zone-status"
vars.cluster_zone = mgmt_zone
assign where regex("abc-ic-ma0*", host.name)
ignore where regex("^q1.*", mgmt_zone)
}
So now our questions are:
- Why do both functions return nothing inside the debugger?
- Why does the service apply for the first posted function(get_hosts_with_http_service) not work, but the service apply using the second function(get_satellite_zones) works?
Any help/hints are much appreciated
Cheers.