Creation of host labels - Format of section

Hi,

I’ve been reading the documentation page Labels (checkmk.com) on how to create host labels from agent plugins. The parser seems to be very picky, and I think the documentation does not match what is expected.

The documentation page shows the example:

Die Label-Ausgabe ist hierbei als Python Dictionary zu formatieren, wie im folgenden Beispiel:
<<<labels>>>
{"cpu/vendor": "zilog"}

First I tried just printing a python dictionary with print:

<<<labels>>>
{'a': 'b'}

The parser in CheckMk then complains about the single quotes, ok. So I switched to using json.dumps to create output with double quotes that looks exactly like in the example:

<<<labels>>>
{"a": "b"}

Now it fails with the following error:

  File "/omd/sites/monitoring/lib/python3.11/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 6 (char 5)

I then tested around it found it does not like the space behind the colon, so I’m using the following parameters to get it working:

print(json.dumps(labels, separators=(',', ':')))

<<<labels>>>
{"a":"b"}

The parser seems to be really picky here, and I think the correct format should be shown in the documentation. The example from the documentation page does not work.

I did my test on the following version of CheckMk:

Checkmk Cloud Edition 2.2.0p20
License state: Free

Regards,
Sven

Maybe that’s an error in the docs. Usually checkmk separates the agent data at whitespace. We don’t need that for JSON data. Instead use 0 character as separator so that no splitting takes place:

<<<labels:sep(0)>>>
{"cpu/vendor": "zilog"}

If you like, you can have separate (complete!) JSON structures on separate lines:

<<<labels:sep(0)>>>
{"cpu/vendor": "zilog"}
{"cpu/voltage": "high"}

But don’t split one structure into multiple lines. This is WRONG:

<<<labels:sep(0)>>>
{"cpu/vendor": 
 "zilog"}
4 Likes

Thanks for the info. I was not aware the labels section is split like the agent sections. That example in the docs looked to me like it just expects a json structure below the labels header.

Thanks,
Sven

1 Like