Open koppel opened 4 years ago
👍
Hello @koppel and thank you for reporting!
cluster_nodes = [] all_nodes = get_objects(Host)
Both of these are global variables. Please try again with "var ".
Best, AK
I have variously tested with and without using var
, and the result was the same. But I did just try changing the variable's names and adding var
back in. Again, the result was the same:
object Host "Cluster: aws_test01_cluster - lambda test" {
enable_notifications = false
import "aws_test01_cluster"
check_command = "dummy"
vars += {
dummy_state = get_dummy_test("state", "aws_test01_cluster", "App: Healthcheck URL")
dummy_text = {{
var selected_nodes = []
var every_node = get_objects(Host)
var host_group = "aws_test01_cluster"
var output = "Nodes in " + host_group + ":\n"
for (node in every_node) {
if (host_group in node.groups) {
log(
LogInformation,
"get_hostgroup_members",
host_group + ": found node: " + node.name
)
selected_nodes.add(node)
}
}
for (node in selected_nodes) {
output += node.name + "\n"
}
output += "\n\nAll nodes:\n"
for (node in every_node) {
output += node.name + ": " + node.groups.join(",") + "\n"
}
return output
}}
}
}
and in Icingaweb2:
Plugin Output
Nodes in aws_test01_cluster:
All nodes:
kafka-01: kafka, all_linux_hosts, production_hosts
cassandra-01: cassandra, all_linux_hosts, production_hosts
zookeeper-01: zookeeper, all_linux_hosts, production_hosts
...truncated
Which zones are your hosts in?
References
Summary
I have found that while
get_objects
(andget_object
) work perfectly for many use cases, they fail to return any hosts that were added via the Icinga2 API when used inside an attribute's custom function. See the Prime example in the Not Working section below.Details:
Our Environment
Expected Behavior
get_object
andget_objects
should always return all hosts, either defined in the DSL or added via the API, even inside an attribute's custom function.Current Behavior
Working
get_object
for hosts added dynamically via the API, from the Icinga console.and in the log:
get_objects
for hosts added dynamically via the API, from the Icinga console.and in the log:
get_objects
for hosts added dynamically via the API, from a normal function.The function:
Call from the Icinga Console:
and in the log:
Not Working
Using the documentation provided by @dnsmichi, from within an attribute's custom function.
See: Get host objects in hostgroup - failing for hosts added via the API
For hosts added dynamically via the API,
get_objects
does not return any hosts.Function (defined in lambda for brevity):
get_hostgroup_members
, which works from the console. But, the output in Icingaweb2 shows that no hosts were returned:Prime example: Attempt to add logging, and ensure
get_objects
returns at least some of the hosts.The function, moving the logic from the external function into the lambda:
log
statements do not seem to work. So, I am relying on the output of the check itself.In Icingaweb2 (truncated for brevity):
Extra test:
get_object
cannot return the same host as the first test above, in the Working section.So, this worked above from the console, but if you put that same logic in the dummy_text lambda function, it fails:
and in Icingaweb2:
Steps to Reproduce
get_object
function via the Icinga Console.Update -
get_service
also does not return data for hosts added via the API when used inside an attribute's function.In an attempt to work around the above bug, I tried hard-coding the hostnames of the cluster members in the cluster check, but
get_service
does not return data when used inside an attribute's custom function for hosts added via the API.Fails - used inside an attribute's custom function for hosts added via the API.
and seen in Icingaweb2's output:
Working - The same code run from the Icinga2 Console
and in the log:
Working: The identical code from above, but replacing the servers with with some that are statically defined in the DSL
and in Icingaweb2:
Conclusion
Functions that work properly when used from the Icinga2 Console, or inside a regular function for all hosts do not work for hosts that were added dynamically via the API, when used inside an attribute's custom function.
✅ All functions work for statically-defined hosts in all circumstances that were tested. ✅ All functions work for dynamically-added hosts (via the API) when used from the Icinga2 Console. ✅ All functions work for dynamically-added hosts (via the API) when used in a standard function. ❌ None of the tested functions work for dynamically-added hosts (via the API) when used in an attribute's custom function. The impact of this is that cluster checks do not work for hosts that were added dynamically via the API.