REST API display only the host names

Hi,

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 ?

Thanks,
Luc

1 Like

Hi @lucfx,

use " Show hosts of specific condition" for that.
/domain-types/host/collections/all

Without query you get all hosts in a json format, only given the column “name”.

Can you please give me a example with specific condition? I am new to ckeckmk api.

There are no necessary conditions for this api-call :slight_smile:
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.

1 Like

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.

I usually filter out the .links nodes first with jq because for me they are useless clutter.

...
| jq 'del(.links) | del(.value[].links)' \
...

I use this script to get a list of host, it’s faster than cmk --list-hosts
rest-api-get-all-hosts.sh (683 Bytes)

1 Like

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:
[

[“host_state”, “host”,“host_icons”],

[“UP”, “host1”,“delivered”,“10”,“0”, “0”,“0”,“0”],

[“UP”, “host2”,“delivered”,“28”,“0”, “0”,“0”,“0”],

[“UP”, “host3”,“delivered”,“15”,“0”, “0”,“0”,“0”]

]

Maybe in the future this can be exposed as an endpoint in the REST api for easy grabbing the hosts.

Thanks,
luc

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.

Yes.It’s not REST api, but it helps a lot for simple scripts. :smiley:

Well… the REST-Api isn’t hard to use, if you have a little scripting expierence as well :smiley:

Thanks!

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