Is there a way to get list of hosts name(only)/host ids using rest api? And get them as json ?I am using this endpoint: /domain-types/host_config/collections/all but it return to much data in nested json arrays that I don’t need.Is there a query or a better way to simply get all the hosts ?
There are no necessary conditions for this api-call
You can just:
https://<check_mk_server>//check_mk/api/1.0/domain-types/host/collections/all
The response you get is a json-response containing all hosts.
See the documentation:
https://<check_mk_server>//check_mk/openapi/#operation/cmk.gui.plugins.openapi.endpoints.host.list_hosts
You are right, but I am getting a huge array of nested json objects with unnecessary fields like domainType and so on .What I wanted was someting like this:
[
{name: “host1”},
{name: “host2”} …
]
a simple json array not all the data, don’t know if it possible through the api.
The REST API follows specific design patterns (OpenAPI 3 spec and HATEOAS) that “add” this seemingly unnecessary information to the returned data.
If you just want a list of currently monitored hosts have a look at the Multisite API, which is basically just adding output_format=json to any view.py URL. You can create any custom view of monitoring data and query this table.
This is most likely the easiest. Create a view that only contain HOSTNAME and call that view using output_format=json and you get much less data. You will still get an “incorrect” json due to how checkmk handles view column but it will be much less data.
Great example jodok ! In my case I am using go to create the script , not bash(so no jq ).But I found a way.From gui all hosts-> export tab-> as json-> the file contains a list of arrays.Easy to traverse in a programming language.Then extract the hosts into a list/array.
Here is a example of exported hosts from the json file:
[
What you have used (“Export as JSON”) is the Multisite API. You just need to use that URL from the menu link and add _username= and _secret= parameters to it. It is not REST though.
I have use the option of creating a view and them getting the json with a CURL. It works as expected.
However, I think it should be indispensable that the API has integrated these basic filters as basic as taking a parameter out of all the hosts