REST API downtime - query not behaving as expected

CMK version: 2.2.0p12 (cre)
OS version: Debian GNU/Linux 11 (bullseye)

Error message: (no error)

I am trying to retrieve the downtimes via the rest API, however there appears to be no filtering of the output by the query I specified. In the example below, I am trying to restrict the response by hostname and the comment on the downtime - but I get back data for all hosts / all downtimes.

Oddly, despite “site_id” being a required paramter (according to the docs), output is not filtered by this either in my code.

How can I restrict the returned data?

HDR="Content-Type: application/json"
AUTHHDR="Authorization: Bearer ${CMKUSER} ${CMKSECRET}"
FILTER='{"op": "and", "expr": [{"op": "~", "left": "comment", "right": "banana"}, {"op": "=", "left": "host_name", "right": "cmk-satellite.southwold.net"}]}'
CMKSITE='southwold'

RES=$( curl -i -w "\n%{http_code}" -X "GET" \
       --data-urlencode 'downtime_type=both' \
       --data-urlencode "site_id=${CMKSITE}" \
       --data-urlencode "query=${FILTER}" \
       "${CMKURL}/domain-types/downtime/collections/all" \
       -H "Accept: application/json" -H "$HDR" -H "$AUTHHDR"

(truncated output below)

      "domainType": "downtime",
      "id": "4",
      "title": "Downtimeforservice:OMDsatelliteperformance",
      "members": {},
      "extensions": {
        "site_id": "southwold",
        "host_name": "cmk-satellite.southwold.net",
        "author": "cmkadmin",
        "is_service": "yes",
        "start_time": "2023-11-28T10:43:00+00:00",
        "end_time": "2023-11-28T11:03:00+00:00",
        "recurring": "no",
        "comment": "testinggetmaintenance"
      }
    },
    {
      "domainType": "downtime",
      "id": "5",
      "title": "Downtimeforservice:SystemdSocketSummary",
      "members": {},
      "extensions": {
        "site_id": "southwold",
        "host_name": "cmk-satellite.southwold.net",
        "author": "cmkadmin",
        "is_service": "yes",
        "start_time": "2023-11-28T10:43:00+00:00",
        "end_time": "2023-11-28T11:03:00+00:00",
        "recurring": "no",
        "comment": "testinggetmaintenance"
      }
    },
    {
      "domainType": "downtime",
      "id": "6",
      "title": "Downtimeforservice:SystemdServiceSummary",
      "members": {},
      "extensions": {
        "site_id": "satellite",
        "host_name": "glue.southwold.net",
        "author": "cmkadmin",
        "is_service": "yes",
        "start_time": "2023-11-28T10:48:46+00:00",
        "end_time": "2023-11-28T11:08:46+00:00",
        "recurring": "no",
        "comment": "testinggetmaintenance"
      }
    }

The best way is to extract “extensions” from your JSON. This poit held all the needed information.

Rg, Christian

The best way is to extract “extensions” from your JSON

Do you mean filter the data returned by this endpoint? If so, I’m not understanding why this is the best way - I would have expected filtering on the check_mk server to be a lot more efficient than client side. Its certainly less efficient for me to recreate functionality which should already be available in check_mk (but more than happy to have the errors in my code pointed out).

Or can I get the services currently in downtime for a given host via a different URL?

If you use Show all downtime you will get the wholem list. You can use the parameter query to filter the list. This info you can find in the RestAPI documentation.

Rg, Christian

You can use the parameter query to filter the list

I thought that was what I was already doing and the exact point of my post?

FILTER=‘{“op”: “and”, “expr”: [{“op”: “~”, “left”: “comment”, “right”: “banana”}, {“op”: “=”, “left”: “host_name”, “right”: “cmk-satellite.southwold.net”}]}’
CMKSITE=‘southwold’

Hi.

Did you try your payload in the interactive GUI for RestAPI calls?

Rg, Christian

Did you try your payload in the interactive GUI for RestAPI calls?

I hadn’t, and this is a good suggestion. Unfortunately its not helping to progress matters.

I applied a MINIMAL edit to the example parameters using this as the query object:

{"op": "=", "left": "host_name", "right": "cmk-satellite.southwold.net"}

(service_description left blank, host_name left blank)

This generated the URL

http://cmk-primary.southwold.net/southwold/check_mk/api/1.0/domain-types/downtime/collections/all?op=%3D&left=host_name&right=cmk-satellite.southwold.net&downtime_type=both&site_id=southwold

But I get a 400 response:

{
  "title": "Bad Request",
  "status": 400,
  "detail": "These fields have problems: right, op, left",
  "fields": {
    "right": [
      "Unknown field."
    ],
    "op": [
      "Unknown field."
    ],
    "left": [
      "Unknown field."
    ]
  }
}

Indeed, using the PROVIDED EXAMPLE DATA, changing only the site id, I get the same error.

According to the “RestAPI documentation”, the filter is passed in the query part of the URI as a parameter named “query” but swagger is clearly parsing the JSON argument and generating named parameters from the content of the JSON.

If nothing else, this has painted a picture of the quality of the product / documentation.

This is the issue.

The --data-urlencode behaviour in curl requires that request be made with curl -G to include the parameter in the URL query. Using curl -X "GET" resuls in the aprameter being silently dropped.

1 Like

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.