Retreive a list of hosts by theyr status using REST APIs

Hello,
i’m here to ask some help about getting started with retreiving data within rest apis.
Since i’m totally new of checkMK i’m in need of some help cause i’m not able to understand how to recover some data or if it’s possible.
Reading documentation, I’ve learned how to recover all hosts from APIs ( /api/1.0/domain-types/host_config/collections/all?effective_attributes=false&include_links=false ) , Then from this result i can recover all hosts and, for each host, i can check the services ( /api/1.0/objects/host/{hostname}/collections/services ) and then the status of every single service of that host.

This kind of process, by the way, is very long, expecially if there are thousands of elements to check everytime.

What i’d need, is a way to recover all hosts that have any service status not equal to 0 (OK). So i can retrieve only elements that are in a state of warn/crit/uk . This would help me to discover more easly and faster the elements that are having problems.

Is there any kind of API that i’m missing that can do that?

I thank you in advance for any advice and help provided.

Regards

Hi Daniele,

welcome to the forums.

This is the query I use to get all services that have a state != 0, from there you could group the hosts by script.

http://10.167.160.15/jochum/check_mk/api/1.0/domain-types/service/collections/all?columns=state&columns=acknowledged&columns=host_name&columns=display_name&columns=description&columns=plugin_output&columns=comments&columns=last_state_change&query={"op"%3A+"!%3D"%2C+"left"%3A+"state"%2C+"right"%3A+"0"}

The important part is the query at last:

query={"op":"!=", "left": "state", "right": "0"}

Happy Monitoring,
René

2 Likes

Hello.
Thank you so much for your answer. This really saved my day.
Also because, this API doesn’t appear in the swagger and i can’t understand why?! It’s absolutely the result i was looking for!

Thanks a lot!

Have a nice day!

1 Like

@jochum

I tested the previous link, and it worked, but I noticed that it returns values for the services.

However, when I try the same approach for all hosts, I can’t see the values for the selected columns.

URL used:
http://192.168.100.10/principal/check_mk/api/1.0/domain-types/host_config/collections/all?columns=state&columns=host_name&

Could you guide me on whether the status query for hosts works differently? Should I consider any adjustments to my URL?

You find all the columns in the API documentation on your host:

http://192.168.100.10/principal/check_mk/api/doc/#section/Table-definitions/Hosts-Table

There is an API to query them as well but I forgot it.

Else GitHub - jochumdev/lql-api: LQL API Server for check_mk is very usefull for playing around with livestatus.

@jochum you’re a crack.
Thank you so much, this has been very helpful!
Cheers

1 Like

You know the “Export” menu in the views? You can create an API call from every host/service view with filters set:

This makes designing the query a lot easier.

Columns must be defined with (multiple) “&columns=” parameters.

The available columns are documented in the api/doc/#section/Table-definitions and the manual creation of API queries is explained under api/doc/#section/Querying-Status-Data

1 Like