Get_host_group function

Iam using the really great cluster-example in the advanced topics section

Ive got several Hosts and I now like to use a hostgroup instead this array:
vars.cluster_nodes = [ “dbhost1”, “dbhost2”, “dbhost3” ]

get_host_group doesnt work:

vars.cluster_nodes = get_host_group(“mydbgroup”);

Iam getting:
Exception occurred while checking ‘clustertest’: Error: Invalid type in for expression: HostGroup

What is wrong?

I think the answer is mentioned here:

But I dont get it

Hi,

can you please share the full configuration object as well as the output? Also, please add the icinga2 --version output.

Cheers,
Michael

here it is.
version_info.txt (1.3 KB) config_object.txt (5.6 KB)

And the content of /etc/icinga2/conf.d/clustertest.conf please, I need the full DSL code.

ok here it is.
clustertest.conf (904 Bytes)

Can you explain the expected value type of cluster_nodes and what exactly the returned value of the get_host_group function is? (Hint: Documentation)

Besides, the function is used in the wrong scope, it must be moved into the runtime lambda function. But that’s secondary for understanding the problem now.

object Host "clustertest3" {
  check_command = "dummy"
vars.cluster_nodes = get_host_group("testgruppe");


  vars.dummy_state = {{
    var up_count = 0
    var down_count = 0
    var cluster_nodes = macro("$cluster_nodes$")

    for (node in cluster_nodes) {
      if (get_host(node).state > 0) {
        down_count += 1
      } else {
        up_count += 1
      }
    }

    if (up_count >= down_count) {
      return 0 //same up as down -> UP
    } else {
      return 2 //something is broken
    }
  }}

I found out how to view the details of objects with “icinga console”.

<11> => get_host_group("testgruppe")

I dont see an array of hosts there, I guess this is part of the problem.
And of course the function gets the whole object not only a variable of it.

Correct, very good :+1: The HostGroup object does not have any members attribute, so you’ll need to write your own collector function. Thanks for the power of the DSL, this can be achieved with using Array#filter for instance.

var cluster_nodes = get_objects(Host).filter(h => "testgruppe" in host.groups)

This iterates over all returned host objects and checks whether testgruppe is found inside the host’s groups array. That’s the short version, the longer version without a function callback as comparator looks like this:

var cluster_nodes = []
for (h in get_objects(Host)) {
  if ("testgruppe" in h.groups) {
    cluster_nodes.add(h)
  }
}

Cheers,
Michael

Thank you very much!

I could insert the long version in the right place (see listing.txt) and it seems to work.

But when I replace it with the one-liner I get this error:

Exception occurred while checking 'clustertest9': Error: Error while evaluating expression: Tried to access undefined script variable 'host'listing.txt (720 Bytes)

Try instead:

var cluster_nodes = get_objects(Host).filter(h => “testgruppe” in h.groups)

Antony.

This works! Thanks
Timo