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
}
}}
Correct, very good 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)
}
}