Custom Host Tags with REST API is broken (restart Checkmk to fix)

@robin.gierse as requested…

Checkmk Enterprise Free Edition 2.0.0p23

Reproduction steps:

  1. Create new custom Tag using UI
  2. Create new Host and assign the custom Tag using UI
  3. Save config using UI
  4. Show the Host using REST API - fails with 500
  5. Create a new Host with custom Tag using REST API - fails with 400
  6. Restart Checkmk - omd stop; omd start
  7. Show the Host using REST API - succeeds
  8. Create a new Host with custom Tag using REST API - succeeds

REST API request/response snips below:

// SHOW HOST REQUEST
curl -X 'GET' \
  'http://10.20.163.153/enea/check_mk/api/v0/objects/host_config/test001?effective_attributes=true' \
  -H 'accept: application/json'

// SHOW HOST RESPONSE BEFORE CHECKMK RESTART
{
  "title": "Server was about to send an invalid response.",
  "status": 500,
  "detail": "This is an error of the implementation.",
  "errors": {
    "_schema": {
      "tag_custom_tag": "Unknown field.",
      "_schema": [
        "tag_custom_tag is not a custom host attribute."
      ]
    }
  },

// SHOW HOST RESPONSE AFTER CHECKMK RESTART
...
     "locked_attributes": [],
      "labels": {},
      "tag_address_family": "ip-v4-only",
      "tag_custom_tag": "option2",            <--- HERE
      "tag_agent": "cmk-agent",
      "tag_piggyback": "auto-piggyback",
      "tag_snmp_ds": "no-snmp",
      "tag_criticality": "prod",
      "tag_networking": "lan"
...


// CREATE HOST REQUEST
curl -X 'POST' \
  'http://10.20.163.153/enea/check_mk/api/v0/domain-types/host_config/collections/all' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "folder": "/",
  "host_name": "test002",
  "attributes": {
    "ipaddress": "192.168.0.123",
    "tag_custom_tag": "option1"
  }
}'

// CREATE HOST REQUEST BEFORE CHECKMK RESTART
{
  "title": "Bad Request",
  "status": 400,
  "detail": "These fields have problems: attributes",
  "fields": {
    "attributes": {
      "_schema": [
        "tag_custom_tag is not a custom host attribute."
      ]
    }
  }
}

// CREATE HOST REQUEST AFTER CHECKMK RESTART
  "domainType": "host_config",
  "id": "test002",
  "title": "test002",
  "members": {},
  "extensions": {
    "folder": "",
    "attributes": {
      "ipaddress": "192.168.0.123",
      "meta_data": {
        "created_at": "2022-05-19T14:53:19.255259+00:00",
        "updated_at": "2022-05-19T14:53:19.309193+00:00",
        "created_by": "cmkadmin"
      },
      "tag_custom_tag": "option1"   <--- HERE
    },
    "effective_attributes": null,
    "is_cluster": false,
    "is_offline": false,
    "cluster_nodes": null
  }
}

Quick update: This is already fixed in Checkmk 2.1.

@edit: I cannot reproduce this in 2.0.

@Asallante: Are we talking about “Custom host attributes”, or normal “Host tags”?

Custom host attributes:
image

Host tags:
image

1 Like

@robin.gierse This issue concerns Host Tags…

image

I still can not reproduce your issue.
This seems to be a local problem in your instance.
Have you tried this with a freshly installed site?

I am able to reproduce this on a fresh 2.0.0p25.cee site. It was reported to me by a customer.

1- Create base site: Created new host tag group with two options, activate changes, and restart site

2- Tried to create host without new option in tag group (probably not helpful)

root@foobar:/tmp# bash cmk_host_add_test.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   407  100   214  100   193  10700   9650 --:--:-- --:--:-- --:--:-- 20350
{
  "title": "Bad Request",
  "status": 400,
  "detail": "These fields have problems: attributes",
  "fields": {
    "attributes": {
      "_schema": {
        "tag_server_site": [
          "'test1' is not one of the enum values: ['none', 'unknown']"
        ]
      }
    }
  }
}
Request error
root@foobar:/tmp#

3- Tried to create host after creating and activating new host tag group option, but fails to be seen by api

root@foobar:/tmp# bash cmk_host_add_test.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   407  100   214  100   193  10700   9650 --:--:-- --:--:-- --:--:-- 21421
{
  "title": "Bad Request",
  "status": 400,
  "detail": "These fields have problems: attributes",
  "fields": {
    "attributes": {
      "_schema": {
        "tag_server_site": [
          "'test1' is not one of the enum values: ['none', 'unknown']"
        ]
      }
    }
  }
}
Request error
root@foobar:/tmp#

4- Post site restart, host adds successfully with new tag group option

root@foobar:/tmp# bash cmk_host_add_test.sh
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3783  100  3590  100   193   1070     57  0:00:03  0:00:03 --:--:--  1127
{
  "domainType": "host_config",
  "id": "example.com",
  "title": "example.com",
  "members": {},
  "extensions": {
    "folder": "",
    "attributes": {
      "ipaddress": "192.168.0.123",
      "meta_data": {
        "created_at": "2022-06-20T19:43:56.452605+00:00",
        "updated_at": "2022-06-20T19:43:56.491707+00:00",
        "created_by": "automation"
      },
      "tag_server_site": "test1"
    },
    "effective_attributes": null,
    "is_cluster": false,
    "is_offline": false,
    "cluster_nodes": null
  }
}
OK
root@foobar:/tmp#

This is the API call being used

#!/bin/bash

HOST_NAME="192.168.1.100"
SITE_NAME="fresh_site"
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 '{
          "attributes": {
            "ipaddress": "192.168.0.123",
            "tag_site": "test1"
          },
          "folder": "\/",
          "host_name": "example.com"
        }' \
    "$API_URL/domain-types/host_config/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"
    exit 1
fi

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.