Issue passing vars from host to service

Hi all,

I have a problem passing the notificatiopn.mail.groups variable from the host config to the service.

parts from host.conf:

    vars.by_ssh_file_age["/var/run/file_check/lastRun"] = {
        host = "192.168.1.1"
        login = "root"
        key = "/var/lib/nagios/.ssh/id_rsa"
        command = "/usr/lib/nagios/plugins/check_file_age -f /var/run/file_check/lastRun -w 0 -c 90000 -W 0 -C 0"
        descr = "Check fileAge for lastRun file /var/run/file_check/lastRun"
       mailgroup = [ "AdminsGroup" ]
    }

parts from service.conf:

apply Service for (file => config in host.vars.by_ssh_file_age) {
    import "generic-service"
    check_command = "check_by_sshfile_age"
    vars += config
    display_name = vars.descr
    vars.host = vars.host
    vars.login = vars.login
    vars.key = vars.key
    vars.command = vars.command
    vars.notification["mail"] = {
      groups = host.vars.mailgroup
    }
    assign where host.vars.by_ssh_file_age
}

The config check always gives an error, no matter if i put the mailgroup value in brackets or not.

How can I pass the groups (mailgroup var) from the host.conf to the service.conf?

TIA

cheers
Harald

The iterator is config so in your loop you access it by

config.host
config.login
config.key

But there could be additional problems, I don’t use plain config files very often

thx, i’ll give it a try. keep you posted.

no luck, still running into an issue.

/etc/icinga2/conf.d/services/by_ssh_file_age.conf(14):     vars.notification["mail"] = {
/etc/icinga2/conf.d/services/by_ssh_file_age.conf(15): #      groups = [ "icingaadmins" ]
/etc/icinga2/conf.d/services/by_ssh_file_age.conf(16):       groups = config.host.vars.mailgroup
                                                                      ^^^^^^^^^^^^^^^^
/etc/icinga2/conf.d/services/by_ssh_file_age.conf(17):     }

config.host is a string (“192.168.1.1”), of course it doesn’t have the property vars.

Thanks for your post. However, I am unsure which variable are you actually trying to access. I am assuming you want to use mailgroup from the host’s vars.by_ssh_file_age["/var/run/file_check/lastRun"]. If so, would you try:

apply Service for (file => config in host.vars.by_ssh_file_age) {
    // SNIP
    vars.notification["mail"] = {
      groups = config.mailgroup
    }
}

Btw, the assign where host.vars.by_ssh_file_age is not needed as you are already using an apply for rule for this exact variable.

Perfect, thanks, solved!

I changed the conf in this way:

    vars.notification["mail"] = {
#      groups = [ "icingaadmins" ]
      groups = config.mailgroup
    }

#    assign where host.vars.by_ssh_file_age

Is there any way to dump all varibales that could be used?

I am glad to hear this. Please feel free to mark the thread as being solved.

As you defined your Service via an Apply For Rule over (file => config in host.vars.by_ssh_file_age), the two variables file and config are being available in your Service definition. For each entry in host.vars.by_ssh_file_age, a Service objects gets created, where file is always the dictionary key and config is its value.

Taking your configuration snippet from your host.conf in the initial post, there is one entry in the host.vars.by_ssh_file_age dictionary. Its dictionary key is "/var/run/file_check/lastRun", getting assigned to the file variable while its value - that’s your dictionary of host, login, key, command, descr and mailgroup - getting assigned to the config variable, to be used in the Service definition scope.

However, there is also the possibility to take a real look into what’s available with by using the Script Debugger. While this might be a bit more complex, it allows you an interactive debugging.

Hi Alvar,

thansk for the good explanation.

I marked one post as solution, Is this enough or do I have to set it to solve somewhere else?

cheers
Harald

That’s fine. Thanks :slight_smile:

I am glad I was able to help you out.