Introduction
All examples on this page are based on cURL, because most Python programmers can easily transfer that to a requests call. The other way round it's more difficult.
Hosts
Showing all Attributes of a Host
Request
Showing the explicit attributes of a host
HOST_NAME="localhost" PROTOCOL="https" USERNAME="automation" SITE_NAME="klapplg" QUERY_HOST="klappedieerste" PASSWORD="********" API_URL="$PROTOCOL://$HOST_NAME/$SITE_NAME/check_mk/api/1.0" out=$( curl \ -G \ --request GET \ --write-out "\nxxx-status_code=%{http_code}\n" \ --header "Authorization: Bearer $USERNAME $PASSWORD" \ --header "Accept: application/json" \ --data-urlencode 'effective_attributes=false' \ "$API_URL/objects/host_config/$QUERY_HOST") resp=$( echo "${out}" | grep -v "xxx-status_code" ) code=$( echo "${out}" | awk -F"=" '/^xxx-status_code/ {print $2}') echo "$resp" | jq if [[ $code -lt 400 ]]; then echo "OK"; else echo "Request error"; fi;
Response
Restrictions
- Custom attributes of a host are currently only visible when setting "effective_attributes=true".
Services
Showing a particular service
Request
Showing the Service "Filesystem /boot" for a host
HOST_NAME="localhost" PROTOCOL="https" USERNAME="automation" SITE_NAME="klapplg" QUERY_HOST="klappedieerste" PASSWORD="********" API_URL="$PROTOCOL://$HOST_NAME/$SITE_NAME/check_mk/api/1.0" out=$( curl \ -G \ --request GET \ --write-out "\nxxx-status_code=%{http_code}\n" \ --header "Authorization: Bearer $USERNAME $PASSWORD" \ --header "Accept: application/json" \ --data-urlencode 'service_description=Filesystem /boot' \ "$API_URL/objects/host/$QUERY_HOST/actions/show_service/invoke") resp=$( echo "${out}" | grep -v "xxx-status_code" ) code=$( echo "${out}" | awk -F"=" '/^xxx-status_code/ {print $2}') echo "$resp" | jq if [[ $code -lt 400 ]]; then echo "OK"; else echo "Request error"; fi
Response
Acknowledge a Service Problem
Request
Acknowledge a service problem
HOST_NAME="localhost" PROTOCOL="https" USERNAME="automation" SITE_NAME="klapplg" QUERY_HOST="klappedieerste" PASSWORD="********" API_URL="$PROTOCOL://$HOST_NAME/$SITE_NAME/check_mk/api/1.0" out=$( curl \ --request POST \ --write-out "\nxxx-status_code=%{http_code}\n" \ --header "Authorization: Bearer $USERNAME $PASSWORD" \ --header "Content-Type: application/json" \ --header "Accept: */*" \ --data '{ "acknowledge_type": "service", "sticky": false, "persistent": false, "notify": false, "comment": "This was expected.", "host_name": "'$QUERY_HOST'", "service_description": "FlavorUsage klapplg" }' \ "$API_URL/domain-types/acknowledge/collections/service") resp=$( echo "${out}" | grep -v "xxx-status_code" ) code=$( echo "${out}" | awk -F"=" '/^xxx-status_code/ {print $2}') echo "$resp" | jq if [[ $code -lt 400 ]]; then echo "OK"; else echo "Request error"; fi;
Response
This Request doesn't send a response body, only an HTTP return code.
Queries
The endpoint /domain-types/service/collections/all provides limited access to Livestatus tables. Using queries, you can access tables and filter data.
Query with multiple columns
Request
Query with multiple columns
HOST_NAME="localhost" PROTOCOL="https" USERNAME="automation" SITE_NAME="klapplg" QUERY_HOST="klappedieerste" PASSWORD="********" API_URL="$PROTOCOL://$HOST_NAME/$SITE_NAME/check_mk/api/1.0" out=$( curl \ -v -G \ --request GET \ --write-out "\nxxx-status_code=%{http_code}\n" \ --header "Authorization: Bearer $USERNAME $PASSWORD" \ --header "Accept: */*" \ --data-urlencode 'query={"op": "=", "left": "host_name", "right": "'$QUERY_HOST'"}' \ --data-urlencode 'columns=host_name' \ --data-urlencode 'columns=description' \ "$API_URL/domain-types/service/collections/all" ) resp=$( echo "${out}" | grep -v "xxx-status_code" ) code=$( echo "${out}" | awk -F"=" '/^xxx-status_code/ {print $2}') echo "$resp" | jq if [[ $code -lt 400 ]]; then echo "OK"; else echo "Request error"; fi
Response
Complex Queries
Request
Complex Query with AND
HOST_NAME="localhost" PROTOCOL="https" USERNAME="automation" SITE_NAME="klapplg" QUERY_HOST="klappedieerste" PASSWORD="********" API_URL="$PROTOCOL://$HOST_NAME/$SITE_NAME/check_mk/api/1.0" out=$( curl \ -v -G \ --request GET \ --write-out "\nxxx-status_code=%{http_code}\n" \ --header "Authorization: Bearer $USERNAME $PASSWORD" \ --header "Accept: */*" \ --data-urlencode 'query={"op": "and", "expr":[{"op": "=", "left": "host_name", "right": "'$QUERY_HOST'"}, {"op": "=", "left": "description", "right": "CPU utilization"}]}' \ --data-urlencode 'columns=host_name' \ --data-urlencode 'columns=description' \ "$API_URL/domain-types/service/collections/all" ) resp=$( echo "${out}" | grep -v "xxx-status_code" ) code=$( echo "${out}" | awk -F"=" '/^xxx-status_code/ {print $2}') echo "$resp" | jq if [[ $code -lt 400 ]]; then echo "OK"; else echo "Request error"; fi
Response
Request
Complex Query to set a host downtime
HOST_NAME="localhost" PROTOCOL="https" USERNAME="automation" SITE_NAME="klapplg" QUERY_HOST="klappedieerste" PASSWORD="********" API_URL="$PROTOCOL://$HOST_NAME/$SITE_NAME/check_mk/api/1.0" out=$( curl \ --request POST \ --write-out "\nxxx-status_code=%{http_code}\n" \ --header "Authorization: Bearer $USERNAME $PASSWORD" \ --header "Accept: application/json" \ --header "Content-Type: application/json" \ --data '{"comment": "Schedule downtime on host which are in two groups", "downtime_type": "host_by_query", "duration": 0, "end_time": "2022-05-03T13:18:40Z", "recur": "fixed", "start_time": "2022-05-03T11:48:40Z", "query": {"op": "and", "expr":[{"op": ">=", "left": "groups", "right": "hostgroupA"}, {"op": ">=", "left": "groups", "right": "hostgroupB"}]}}' \ "$API_URL/domain-types/downtime/collections/host") resp=$( echo "${out}" | grep -v "xxx-status_code" ) code=$( echo "${out}" | awk -F"=" '/^xxx-status_code/ {print $2}') echo "$resp" | jq if [[ $code -lt 400 ]]; then echo "OK"; else echo "Request error"; fi
Restrictions
- Currently, it's not possible to restrict the output to a single column, you have to request at least two columns.
- It's not possible to query data from columns of the type "blob", like "mk_inventory".