Icinga2 acces host custom variabel from service

Hi to the community,

Am facing a problem.
I want to access a host variable from the service définition.


object Host "host1.domain" {
        import "generic-host"
        display_name                            = "host1"
        address                                     = "xx.xx.xx.xx"

       vars.host_systemd_units           = [
                                                                      "chronyd" ]

       vars.my_var_check_service_sshd = 1


apply Service for (config in host.vars.host_systemd_units) {
        import "generic-service"
        check_command = "check_service"
        name = "Check Service : "+config
        vars.service = config
        command_endpoint = host.vars.client_endpoint
        if ( "$host.vars.my_var_check_service_"+config+"$" == 1 ) {
               vars.new_var = "My_value1"
        } else {
               vars.new_var = "My_value2"
        assign where host.vars.host_systemd_units  && host.vars.client_endpoint

Unfortunately, as a result, the “service.vars.new_var” is “My Value2”. It should be “My Value1”.

I think Icinga is comparing the string “host.vars.my_var_check_service_sshd” to ‘1’.

Can some one help me make this work?

Thank you in advance.

Best regards,

I think this concatenation will not be evaluated there.
I have no idea if that works but you could try:
if ( macro("$host.vars.my_var_check_service_"+config+"$") == 1 ) {

Hi @moreamazingnick

Thanks for the reply.
I already tried that and it don’t work.

[2023-09-18 15:06:11 +0200] critical/config: Error: Invalid field access (for value of type 'Service'): 'macro'
Location: in /etc/icinga2/zones.d/global-templates/services.conf: 2574:7-2574:63
/etc/icinga2/zones.d/global-templates/services.conf(2574):  if ( macro("$host.vars.my_var_check_service_”+config+“$") == 1 ) {
/etc/icinga2/zones.d/global-templates/services.conf(2575):                 vars.new_var = "My_value1"
/etc/icinga2/zones.d/global-templates/services.conf(2576):         } else {
[2023-09-18 15:06:11 +0200] critical/config: 876 errors

these are the wrong double quotes

but you can do something like:
if (config =="sshd" && host.vars.my_var_check_service_sshd == 1 ) {
and so on.
it is not generic but it will work

Thank you again.

It don’t work either:

[2023-09-18 15:29:13 +0200] critical/config: Error: Invalid field access (for value of type 'Service'): 'macro'
Location: in /etc/icinga2/zones.d/global-templates/services.conf: 2574:26-2574:76
/etc/icinga2/zones.d/global-templates/services.conf(2574):  if ( config =="sshd" && macro("$host.vars.my_var_check_service_sshd$") == 1 ) {
/etc/icinga2/zones.d/global-templates/services.conf(2575):                 vars.new_var = "My_value1"
/etc/icinga2/zones.d/global-templates/services.conf(2576):         } else {
[2023-09-18 15:29:13 +0200] critical/config: 170 errors

sorry i edited my post :slight_smile: to:
if (config =="sshd" && host.vars.my_var_check_service_sshd == 1 ) {

Oh that works.
But it’s not what am tempting to do :upside_down_face:

In have 1130 Services.
And the hosts can have up to 20services monitored.

ok so you need 20 if blocks :slight_smile:

that would be ridiculous :laughing:

Does anyone know what this error mean?

not without context…

I have this error ( Error: Invalid field access (for value of type 'Service'): 'macro' ), when i use the macro fonction. as @moreamazingnick suggested:

Reread my original post again, not the quote, I told you, I edited the post

Am sorry but i really don’t think it’s a quote problem.
As you can see i tride the macro function differently but i hade the same error.

that doesn’t work
you can access the var by


without the macro part
something like that:

if (config ==“sshd” && host.vars.my_var_check_service_sshd == 1 ) {

Yes i know that.
But it’s not what am attempting to do.
I need to accès this variable dynamically.

Hi, try to use something like this instead:

if (host.vars["my_var_check_service_"+config] == 1) {
1 Like

@yhabteab thank you so much.
that works perfectly.