API string to retrive object information

Hi,

I have migrated from Icinga to Checkmk, and now I need to replicate the API calls that were made to Icinga also to Checkmk.

The syntax used now is a call like this: http://host_name_icinga/icingaweb2/v1/objects/services?service=$HOST!$SERVICE and credentials are passed to it.

For testing, at the time, on Icinga I used syntax like this:

curl -k -s -S -i -u user:pwd ‘https://localhost:5665/v1/objects/services?service=host_name!service_name

I would need to know the correct syntax to first perform the tests locally from the Checkmk machine and then perform them externally. The goal is to know the status of a check on a specific host from an external system.

I have searched but unfortunately, due to my lack of knowledge, I haven’t found anything.

On Checkmk, I created a regular user to perform these curls.

Thanks, Davide

OS: Rocky9
Checkmk: Raw Edition 2.3.0p6

Then have a look at this fine article: The Checkmk REST API

Hi, thanks for the valuable suggestion. I used the REST API GUI which was very useful. From the GUI, I can check the service status, but when I copy the command (including the token), it returns an error of invalid credentials. How is this possible? {“title”: “Unauthorized”, “status”: 401, “detail”: “Wrong credentials (Basic header)”}

Hi, I solved the login problem and now I can get the information from the service I’m querying. The problem is that compared to Icinga, fewer details are returned, such as the last time it was OK and the check output. Below are two examples.

Icinga2 output

[root@inl-icinga2:~]#curl -k -s -S -i -u usewr:pwd ‘http://xxx.xxx.xxx.xxx/icingaweb2/v1/objects/services?service=hostname!APP_CAUDIT_CAPTURE_SVC
HTTP/1.1 200 OK
Date: Fri, 19 Jul 2024 11:18:56 GMT
Server: Icinga/2.11.2-1
Content-Type: application/json
Content-Length: 2542
X-Cache: MISS from inl-pxyout1
X-Cache-Lookup: MISS from xxxxxxxxxx
Via: 1.1 xxxxxxx (squid/4.15)
Connection: keep-alive

{“results”:[{“attrs”:{“__name”:“hostname!APP_CAUDIT_CAPTURE_SVC”,“acknowledgement”:0.0,“acknowledgement_expiry”:0.0,“action_url”:“”,“active”:true,“check_attempt”:1.0,“check_command”:“APP_CAUDIT_CAPTURE_SVC”,“check_interval”:120.0,“check_period”:“9-18”,“check_timeout”:null,“command_endpoint”:“hostname”,“display_name”:“APP_CAUDIT_CAPTURE_SVC”,“downtime_depth”:0.0,“enable_active_checks”:true,“enable_event_handler”:true,“enable_flapping”:false,“enable_notifications”:true,“enable_passive_checks”:true,“enable_perfdata”:true,“event_command”:“”,“flapping”:false,“flapping_current”:0.0,“flapping_last_change”:0.0,“flapping_threshold”:0.0,“flapping_threshold_high”:30.0,“flapping_threshold_low”:25.0,“force_next_check”:false,“force_next_notification”:false,“groups”:,“ha_mode”:0.0,“handled”:false,“host_name”:“hostname”,“icon_image”:“”,“icon_image_alt”:“”,“last_check”:1721387871.417195,“last_check_result”:{“active”:true,“check_source”:“hostname”,“command”:[“path_custom_check/APP_CAUDIT_CAPTURE_SVC.pl”],“execution_end”:1721387871.417112,“execution_start”:1721387871.372446,“exit_status”:0.0,“output”:“OK - Ultima esecuzione processo 122 minuti fa.”,“performance_data”:,“schedule_end”:1721387871.417195,“schedule_start”:1721387871.417195,“state”:0.0,“ttl”:0.0,“type”:“CheckResult”,“vars_after”:{“attempt”:1.0,“reachable”:true,“state”:0.0,“state_type”:1.0},“vars_before”:{“attempt”:1.0,“reachable”:true,“state”:0.0,“state_type”:1.0}},“last_hard_state”:0.0,“last_hard_state_change”:1721380671.413024,“last_reachable”:true,“last_state”:0.0,“last_state_change”:1721380671.413024,“last_state_critical”:1721380551.418716,“last_state_ok”:1721387871.418218,“last_state_type”:1.0,“last_state_unknown”:0.0,“last_state_unreachable”:0.0,“last_state_warning”:0.0,“max_check_attempts”:5.0,“name”:“APP_CAUDIT_CAPTURE_SVC”,“next_check”:1721387991.37,“notes”:“”,“notes_url”:“”,“original_attributes”:null,“package”:“director”,“paused”:false,“previous_state_change”:1721380671.413024,“problem”:false,“retry_interval”:60.0,“severity”:8.0,“source_location”:{“first_column”:1.0,“first_line”:1380.0,“last_column”:39.0,“last_line”:1380.0,“path”:“/var/lib/icinga2/api/packages/director/6d66712d-75fe-4967-a187-317888e6f42f/zones.d/inl-icinga2/services.conf”},“state”:0.0,“state_type”:1.0,“templates”:[“APP_CAUDIT_CAPTURE_SVC”,“APP_CAUDIT_CAPTURE_SVC”,“Agent-based Service”],“type”:“Service”,“vars”:null,“version”:0.0,“volatile”:false,“zone”:“inl-icinga2”},“joins”:{},“meta”:{},“name”:“hostname!APP_CAUDIT_CAPTURE_SVC”,“type”:“Service”}]}

checkmk output
[root@inl-checkmk:~]#curl -k -s -S -i -u user:pwd ‘http://xxx.xxx.xxx.xxx./consolidato/check_mk/api/1.0/objects/host/hostname/actions/show_service/invoke?service_description=APP_CAUDIT_CAPTURE_SVC’
HTTP/1.1 200 OK
Date: Fri, 19 Jul 2024 11:19:54 GMT
Server: Apache
Permissions-Policy: accelerometer=(), autoplay=(), camera=(), encrypted-media=(), fullscreen=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), midi=(), payment=(), picture-in-picture=(), usb=()
X-Frame-Options: sameorigin
X-XSS-Protection: 1; mode=block
X-Permitted-Cross-Domain-Policies: none
Referrer-Policy: origin-when-cross-origin
X-Content-Type-Options: nosniff
Content-Length: 508
ETag: “d01d31e6cbded20b522238eabbab8cb3553efc9d”
X-Checkmk-Version: 2.3.0p6
X-Checkmk-Edition: cre
Content-Security-Policy: default-src ‘self’ ‘unsafe-inline’ ‘unsafe-eval’ ssh: rdp:; img-src ‘self’ data: https://*.tile.openstreetmap.org/ ; connect-src ‘self’ https://crash.checkmk.com/ https://license.checkmk.com/api/verify; frame-ancestors ‘self’ ; base-uri ‘self’; form-action ‘self’ javascript: ‘unsafe-inline’; object-src ‘self’; worker-src ‘self’ blob:
Content-Type: application/json

{“links”: [{“domainType”: “link”, “rel”: “self”, “href”: “http://xxx.xxx.xxxx.xxxx/consolidato/check_mk/api/1.0/objects/service/hostname-APP_CAUDIT_CAPTURE_SVC”, “method”: “GET”, “type”: “application/json”}], “domainType”: “service”, “id”: “hostname-APP_CAUDIT_CAPTURE_SVC”, “title”: “Service APP_CAUDIT_CAPTURE_SVC”, “members”: {}, “extensions”: {“description”: “APP_CAUDIT_CAPTURE_SVC”, “host_name”: “hotname”, “state_type”: 1, “state”: 0, “last_check”: 1721387819}}

You can use the API endpoint “Show all monitored services” /domain-types/service/collections/all to retrieve more information, when you add specific columns to the query.

Or the endpoint “Show the monitored services of a host” /objects/host/{host_name}/collections/services where you can also specify which columns from livestatus should be returned.

I tried both queries, but neither has what I need. What I actually need is the “summary” that is shown in the GUI. As it is now, it would be fine because it would return ok/warning or critical, but I wouldn’t know why :slight_smile: I know very little about APIs, so it’s probably my fault.

The “summary” from the GUI is the plugin output and is available in the column “plugin_output”.
The state is in the column “state”.

You’ll find a description of the available columns in the API documentation.

Thank you. Is it possible retrive it from rest api call wihtout use script?

I do not understand the question. An API call is just using an URL with a GET, POST, PUT or DELETE request.

Sure, here’s the translation:

“Unfortunately, I am completely ignorant when it comes to APIs; I am not able to create a URL with that summary. I have read about it, but I just can’t understand it. I hope to find someone in the company who can help me create the correct string. Anyway, you have been a great help, thank you.”

What @r.sander means is a simple curl command like in your example.

curl \
  -G \
  --request GET \
  --header "Authorization: Bearer $USERNAME $PASSWORD" \
  --header "Accept: application/json" \
  --data-urlencode 'host_name=TESTING' \
  --data-urlencode 'query={"op": "and", "expr": [{"op": "=", "left": "host_name", "right": "TESTING"},{"op": "=", "left": "description", "right": "Filesystem /"}]}' \
  --data-urlencode 'columns=host_name' \
  --data-urlencode 'columns=description' \
  --data-urlencode 'columns=plugin_output' \
  "$PROTO://$HOST_NAME/$SITE_NAME/check_mk/api/1.0/domain-types/service/collections/all"

The columns i insert in the request give the following result.

    "value": [
        {
            "domainType": "dict",
            "id": "TESTING:Filesystem /",
            "title": "Filesystem / on TESTING",
            "members": {},
            "extensions": {
                "host_name": "TESTING",
                "description": "Filesystem /",
                "plugin_output": "Used: 58.77% - 16.1 GB of 27.4 GB (warn/crit at 81.85%/90.92% used), trend per 1 day 0 hours: +148 MB, trend per 1 day 0 hours: +0.54%, Time left until disk full: 76 days 6 hours",
            },
        }
    ],

I removed the not relevant parts, normally only the extension section is important for your output. You see all the 3 livestatus columns are there that i used before in the request.
Inside the documentation you find all the available columns.

1 Like

Infinite thanks sir. I’ll check when I’ll come back to the office.

Thank you