Const dictionary

Hi Team.
Can I create constant as dictionary in icinga2?

example

const CONST_01 =  {
	"labci_upstream-01" = ["url_01", "80"]
	"labci_upstream-02" = ["url_02", "80"]

}

call it in service-loop-creation?

Looks like does not work for dictionary type, only for list and string

Hello @pluhin!

What do you mean with “[it] does not work for dictionary type”?

<1> => const d = {}
null
<2> => d
{
}
<3> =>

Please share an actual Icinga config which doesn’t work.

Best,
AK

Hello @Al2Klimov

Config file:


    const MS_UPSTREAMS_lab_ecx =  {
	"labci_upstream-1" = ["lab_10.com", "80"]
	"labci_upstream-2" = ["lab_20.com", "80"]
.....
}

for ( connect => item in MS_UPSTREAMS_lab_ecx ) {
  apply Service "check_connect_" + connect use (connect,item) {
    import "generic-service"
    import "muted-service"
    display_name        = "Connection to VIP " + connect 
    check_command = "tcp"
    notes = "Check connection to " + connect
    vars.tcp_address = item[0]
    vars.tcp_port = item[1]
    vars.service_check_type = "external_connect"
    command_endpoint = host.vars.client_endpoint
    assign where match("*Kubernetes*", host.name) 
    ignore where match("*_Kubernetes_Kafka_*", host.name)
  }
}

During compilation i have:

[2021-04-28 13:43:28 +0000] information/cli: Icinga application loader (version: r2.12.2-1)
[2021-04-28 13:43:28 +0000] information/cli: Loading configuration file(s).
[2021-04-28 13:43:29 +0000] critical/config: Error: Error while evaluating expression: Tried to access undefined script variable MS_UPSTREAMS_lab_ecx
Location: in /etc/icinga2/zones.d/global-templates/services/MS_K8s/upstreams/check_upstream.conf: 1:25-1:44
/etc/icinga2/zones.d/global-templates/services/MS_K8s/upstreams/check_upstream.conf(1): for ( connect => item in MS_UPSTREAMS_lab_ecx ) {
                                                                                                               ^^^^^^^^^^^^^^^^^^^^
/etc/icinga2/zones.d/global-templates/services/MS_K8s/upstreams/check_upstream.conf(2):   apply Service check_connect_ + connect use (connect,item) {
/etc/icinga2/zones.d/global-templates/services/MS_K8s/upstreams/check_upstream.conf(3):     import generic-service
[2021-04-28 13:43:29 +0000] critical/cli: Config validation failed. Re-run with icinga2 daemon -C after fixing the config.

But if I change const to var, all work fine

What do you need it to be constant for?

@Al2Klimov
I would like to put constant to separate file. And time-to-time regenerate it by pipeline

Does globals.MS_UPSTREAMS_lab_ecx = { ... } work for you?

Same

[2021-05-03 10:56:24 +0000] information/cli: Loading configuration file(s).
[2021-05-03 10:56:24 +0000] critical/config: Error: Error while evaluating expression: Tried to access undefined script variable MS_UPSTREAMS_lab_ecx
Location: in /etc/icinga2/zones.d/global-templates/services/MS_K8s/upstreams/check_upstream.conf: 1:25-1:44

for variants
const globals.MS_UPSTREAMS_lab_ecx = {
and
globals.MS_UPSTREAMS_lab_ecx = {

in separate file

I did changes only in constant name, maybe need to change name in call of it in loop?

What’s the separate file’s name?

 icinga2-config git:(E2E_SI_LAB) ls -l zones.d/global-templates/services/MS_K8s/upstreams 
total 16
-rw-rw-r-- 1 pishchyk pishchyk  611 mei  3 12:44 check_upstream.conf
-rw-rw-r-- 1 pishchyk pishchyk 8218 mei  3 12:55 lab-ecx.conf   

check_upstream.conf:

for ( connect => item in MS_UPSTREAMS_lab_ecx ) {
  apply Service "check_connect_" + connect use (connect,item) {
    import "generic-service"
    import "muted-service"
    display_name        = "Connection to VIP " + connect 
    check_command = "tcp"
    notes = "Check connection to " + connect
    vars.tcp_address = item[0]
    vars.tcp_port = item[1]
    vars.service_check_type = "external_connect"
    command_endpoint = host.vars.client_endpoint
    assign where ( match("*Kubernetes*", host.name) || match("MT_tst_Kube_MS*",host.name) )
    ignore where match("*_Kubernetes_Kafka_*", host.name)
  }
}

lab-ecx.conf:

const globals.MS_UPSTREAMS_lab_ecx =  {...}

What if you start the name with a capital L?

What do you mean? Use capital L where?

Here: Lab-ecx.conf

Some progress, now is

[2021-05-03 15:58:26 +0000] information/cli: Icinga application loader (version: r2.12.2-1)
[2021-05-03 15:58:26 +0000] information/cli: Loading configuration file(s).
[2021-05-03 15:58:27 +0000] critical/config: Error: syntax error, unexpected globals (T_GLOBALS), expecting T_IDENTIFIER
Location: in /etc/icinga2/zones.d/global-templates/services/MS_K8s/upstreams/Lab-ecx.conf: 1:6-1:12

Change variable to

const MS_UPSTREAMS_lab_ecx =  {...

in file Lab-ecx.conf

looks like works

@Al2Klimov , thank you, but why we need to use capital in file name? do we have documented this “feature”

Config files seem to be sorted alphabetically and L < c, so the constant is set up before its usage.

1 Like