Dynamically format check_cluster Service Attributes

I have a use case where I’d like to use check_cluster to evaluate all service checks within a given host.

The idea is to first set the $host.name$ macro to a variable.

Then use the get_services function, or even get_objects, to pull all services for that host into an array.

Next, I’d like to loop through the array of services and reformat it to be compatible for check_cluster - so in the format of “$host.name$!service1”, “$host.name$!service2” and so on.

Finally, return the array as a comma-separated variable.

I’ve looked over the documentation and provided examples countless times, and have re-wrote the service to try a different approach almost as much. So, any insight would be very helpful.

Here is my code as it sits right now. Also, I’m unsure how to test that my functions are working outside of the console. Attempts to use the log function do not seem to push messages anywhere regardless of what facility I set it it use. When the validation passes, the variables just show up as “Object as type ‘Function’”.

apply Service "overall" {
        check_command = "check_cluster"
        max_check_attempts = 1

        vars.check_cluster_service = true
        vars.check_cluster_warning = 1
        vars.check_cluster_critical = 2

        vars.check_cluster_objects = {{
                # Set node
                var nodeid = macro("$host.name$")

                # Get Services for the Host (hoping the node will substitute as $host.name$ macro)
#                var host_services = {{
#                        get_objects(Service).filter(s => match("*nodeid*", s.host_name)).map(s => s.name)
#                }}

                var host_services = {{
                        get_services("$host.name$").map(s => s.name)

                        ##var host_services = macro("$service_tags$")

                # Declare new and empty array to house a combination of nodeid and host_services
                var result = []

                # Format services pulled to prefix with '$host.name$!'
                #for (item in host_services) {
                #  result.add("nodeid!item")

                for (item in host_services) {
                        var bind = [ "$host.name$" + "!" + item ]

                # Now we should have and array of ["node!service1" "node!service2" "node!service3"]
                return result.join(",")
assign where host.address

So the final idea here would be to have check_cluster_objects be formatted as the combination mentioned above, where it will then use the check_cluster command I have defined in commands.conf.

Note that I have the $host.name$ macro in some of those loops, I’ve also tried to use the variable set in the first line of the function.