icinga2 icon indicating copy to clipboard operation
icinga2 copied to clipboard

Set_if documentation wrong?!

Open pbirokas opened this issue 4 years ago • 0 comments

Describe the bug

I try to implement the set_if functrion in one of my CheckCommands, but failed to succussed. Orientation was the documentation found here Use Functions in Command Arguments set_if

I also describe my tests on the Icinga Community.

To Reproduce

example host:

object Host "s002.domain.net" {
    import "generic-agent"

    display_name = "s002"
    address = "xxx.xxx.240.12"
    zone = "xxx"
    vars.centos = {
        centos7 = {
            cluster = true
        }
    }
    vars.redhatel = true
    vars.redhatel7 = true
    vars.server = true
    vars.sys = {
        icinga2 = {
            client = true
            zone = {
                xxx = true
            }
        }
        infiniband = {
            mlx5_port1_100 = true
        }
        openssh = true
        platform_mpi = true
        prometheus = true
        racadm = true
        resolver = true
    }
    vars.vendor = {
        dell = {
            poweredge = {
                c6525 = true
            }
        }
    }
}

Example CheckCommand:

object CheckCommand "check_ib" {
  command   = [ "/usr/bin/check_ib" ]
  arguments   = {
    "--dev"  = {
      set_if = {{
        var host_vars = host.vars
        log(host_vars)
        var infiniband = host_vars.sys.infiniband
        log(infiniband)
        return infiniband.keys()
      }}
    }
  }
}

I was not able to get the hosts vars into the check command. The variable host_vars was always empty logged.

Expected behavior

Documented examples should work.

Workaround / Possible Solution

Calling macro() when importing the vars:

object CheckCommand "check_ib" {
  command   = [ "/usr/bin/check_ib" ]
  arguments   = {
    "--dev"  = {
      set_if = {{
        var host_vars = macro("$host.vars$")
        log(host_vars)
        var infiniband = host_vars.sys.infiniband
        log(infiniband)
        return infiniband.keys()
      }}
    }
  }
}

Your Environment

Director version (System - About):1.9.0 Icinga Web 2 version and modules (System - About): 2.9.5 Icinga 2 version (icinga2 --version): 2.13.2-1 Operating System and version: RockyLinux 8 Webserver, PHP versions: Apache 2.4.37-41 PHP 7.4.19

pbirokas avatar Mar 28 '22 16:03 pbirokas

When trying to access the host variables using host.vars, it seems that these variables are not being recognized or are not available in the context where you are trying to use them.

The alternative approach you proposed using the macro("$host.vars$") function may work as a temporary solution to the problem, as it retrieves the host variables in a different way. However, it would be important to further investigate why the original way of accessing the host variables is not working as expected.

Make sure that the structure of the host objects is correctly defined and that the variables you are trying to access are within the vars structure of the host.

marc-delgado-ferreres avatar Feb 07 '24 10:02 marc-delgado-ferreres