Request error while creating a Custom URL Rule via the checkmk API

CMK version:
Checkmk Raw Edition 2.1.0p9

OS version:
Debian 11 on both checkmk server and monitored system

Description of the problem:

I want to implement a custom URL rule via the checkmk API but no matter what I do I get the same error and I have no idea how to resolve it. I’ve made the same rule via the web interface and copied the Rule Value Representation in “value_raw”, but it still doesn’t work and I still have the same error.

Error message:

{
  "title": "Bad Request",
  "status": 400,
  "detail": "These fields have problems: value_raw",
  "fields": {
    "value_raw": [
      "Syntax Error: invalid syntax in 'https://google.com'"
    ]
  }
}

API request:

#!/bin/bash

# NOTE: We recommend all shell users to use the "httpie" examples instead.

HOST_NAME="localhost"
SITE_NAME="monitoring"
API_URL="http://$HOST_NAME/$SITE_NAME/check_mk/api/1.0"

USERNAME="automation"
PASSWORD="redacted"

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 '{
          "conditions": {},
          "folder": "/",
          "properties": {
            "disabled": false
          },
          "ruleset": "extra_host_conf:notes_url",
          "value_raw": "'https://google.com'",
          "conditions": {
            "host_name": {
              "match_on": [
                "test-API"
              ],
              "operator": "one_of"
            },
           "host_tags": [],
           "host_labels": [],
           "service_labels": []
          }

        }' \
    "$API_URL/domain-types/rule/collections/all")

resp=$( echo "${out}" | grep -v "xxx-status_code" )
code=$( echo "${out}" | awk -F"=" '/^xxx-status_code/ {print $2}')

# For indentation, please install 'jq' (JSON query tool)
echo "$resp" | jq
# echo "$resp"

if [[ $code -lt 400 ]]; then
    echo "OK"
    exit 0
else
    echo "Request error"
fi

Have you tried removing the single quotation marks around the URL?

Yes, I also get the exact same error.

UPDATE: must have the
"conditions": {}
included in the request.
It is now working.

I have this same problem.
It seems that the value_raw field cannot be just a string but needs to be a python data structure.
Not really sure what that means. the docs just say

value_raw	
string

The raw parameter value for this rule. To create the correct structure, for now use the 'export for API' menu item in the Rule Editor of the GUI. The value is expected to be a valid Python type.

I’ve searched in the checkmk gui for “export for API” but found nothing.

This is what i have tried.

when i make it a raw string I get the error

<Response [400]>
{'title': 'Bad Request', 'status': 400, 'detail': 'These fields have problems: value_raw', 'fields': {'value_raw': ["Not a Python data structure: 'virtual_machines'"]}}

when i then follow what the interactive docs and i create the following json structure, it works. note that the interactive docs creates the example for curl and not python.

{
  "ruleset": "host_groups",
  "folder": "~virtual_machines",
  "properties": {
    "disabled": false
  },
  "value_raw": "'virtual_machines'",
  "conditions": {}
}
curl -X 'POST' \
  'http://192.168.179.178/monitoring/check_mk/api/1.0/domain-types/rule/collections/all' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "ruleset": "host_groups",
  "folder": "~virtual_machines",
  "properties": {
    "disabled": false
  },
  "value_raw": "'\''virtual_machines'\''",
  "conditions": {}
}'

so now when I try to put the extra ’ or " in my python code, I now get JSON Decode Error with the follow data structure.

{'ruleset': 'host_groups', 'folder': '~virtual_machines', 'properties': {'disabled': False}, 'value_raw': "'virtual_machines'"}
{'ruleset': 'host_groups', 'folder': '~virtual_machines', 'properties': {'disabled': False}, 'value_raw': '"virtual_machines"'}
    raise RuntimeError(pprint.pformat(resp.json()))
  File "C:\Program Files\Python39\lib\site-packages\requests\models.py", line 900, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Program Files\Python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files\Python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files\Python39\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

When I remove the Raise Exception I just get <Response [500] Internal Server Error>
When I check the logs it says

Any ideas ??

1 Like

The “Export this rule for API” is a button available next to the “Create a copy of this rule” button.
Once you click on it, you will see a structure like this:

Then you just need to copy the enite text and paste it as a vlaue to to the “value_raw” parameter.

1 Like

Try escaping it like this:

“value_raw”: “"MyHostGroup"”,