Ansible Inventory - Finding Docker Nodes via CheckMK REST API

Dear CheckMK Forum Users,

we have been using Ansible to manage our 100+ Linux Servers with the Ansible inventory being auto-generated via the CheckMK REST API and a Python script.

As we have used CheckMK some years already, we implemented some Ansible Inventory groups, which we otherwise couldn’t find via CheckMK API, in the form of Host-Tags, AUX-Tags, etc.

We currently use python to call the host_config/collections/all endpoint and scan for the necessary information.

After looking through some things again, we found that (at least in the Monitoring WebUI) you can find a list of every server with docker installed when searching Docker Nodes in the Monitoring search bar. This would be the perfect replacement for our dockerhosts group, which is still manually managed via per-host-settings.

I was also unable to find automatically discovered host-labels like cmk/docker_object:node, which otherwise would also be an easier way to manage our dockerhosts Ansible group.

Sadly, I was not able to find the corresponding API endpoint which would contain any of these information.

Did I miss something or is this information not available via REST API?

Best regards,
pixelpoint

no the RestAPI is not mainly targeted to do filtering. you can use any view however and use the standard outpot_format parameters - or you could use livestatus

Perhaps you have not found the correct API endpoint to fetch?

It took my some time to understand how to use the query and columns parameters. Here I’m fetching all services which are not OK:

check_mk/api/1.0/domain-types/service/collections/all?query={"op":+"!=",+"left":+"state",+"right":+"OK"}&columns=host_tags&columns=hard_state

You can query the host(s table) with this URL (matches “cyed” in the hostname and additionally fetches “tags” and “labels”):

check_mk/api/1.0/domain-types/host/collections/all?query={"op":+"~~",+"left":+"name",+"right":+"cyed"}&columns=tags&columns=labels

More information can be found in the API documentation (Help → REST API documentation)
under “Querying-Status-Data” and “Table definitions”.
A useful python example can be found under “Queries through the REST API”.

Complete example when you want to find all Docker nodes:

check_mk/api/1.0/domain-types/host/collections/all?query={"op":+"=",+"left":+"labels",+"right":+"'cmk/docker_object' 'node'"}&columns=tags&columns=labels

You can access almost everything from livestatus with the REST API. Sometimes it needs some time to understand how to build the query and how the columns are named.

1 Like

Thank you very much, this was exactly what I needed.
I never got around to really understand the REST API and all of its features, especially how to query specific information.

Thanks to you and these examples, I have updated my python script already.

I’m definitely going to look through my whole ansible-inventory-generation script, because now that I know of and am able to use the query system, I feel like there might be a lot of other things I have not been doing in the most optimal way possible :slight_smile:

Best regards,
pixelpoint

2 Likes

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.