Fetch a service status detail via CheckMk REST API

How to fetch the service detail of a check with CheckMk REST API?

I had to write a script that fetch from a CheckMk monitored service the status of that service and do something.

I tried to ask how to do this to several friends that are using CheckMk… no one had ever do this before ;(

In my real scenario I need to fetch a specific service status:

  • Critical
  • Contains some specific text matching in the perf data field.

TL;DR;

USERNAME=$USERNAME$
PASSWORD=$PASSWORD$
curl -s -G \
  --header "Accept: */*" \
  -H "Authorization: Bearer $USERNAME $PASSWORD" \
  --data-urlencode 'columns=host_name' \
  --data-urlencode 'columns=perf_data' \
  --data-urlencode 'query= \
  { "op": "and", "expr":[{"op": "=", "left": "host_name", "right": "$HOSTNAME$"}, \
  { "op": "=", "left": "description", "right": "$SERVICE_NAME$"}, \
  { "op": "=", "left": "state", "right": "2" }, \
  { "op": "~~", "left": "perf_data", "right": "$SOMETHING_TO_FILTER" } ]}' \
  "$CHECKMK_URL$/$SITE_NAME"/check_mk/api/1.0/domain-types/service/collections/all

In this example the shell lines do a CURL request to the CheckMk REST API and fetch only the status of the service $SERVICE_NAME$ that are in a Critical status and contains the word $SOMETHING_TO_FILTER$. The query it’s very useful as you can fine match what do you want without write any code outside. The query is wrote in the livestatus “language”. As you can see there are multiple columns added, these are not provided by default in the results, so I added them because were needed in my scenario.

Some links

Matteo Sgalaberni
Matteo Sgalaberni
CTO Ehiweb.it / CEO Ovus.it

IT-Telco/System/Network Admin