we are migrating CheckMK 1 plugins to 2 with the new API.
For some CheckMK Agent outputs we use JSON that was parsed with the “parse_json” parse_function in CheckMK 1. In CheckMK 2 i dont see any equal function, when using the register.agent_section() you can define a parse_function but you already get data that was already parsed by internal checkMK.
Is there a chance you can get the raw agent output or at least define that the agent output is JSON and so the checkmk check plugin gets the parsed JSON?
import json
def parse_json(string_table):
# json input all in one line:
json_string = ''.join(word for line in string_table for word in line)
parsed = json.loads(json_string)
return parsed
This will first join all the agent output of this section into one long line and then convert it to a Python structure like so:
{
"field1": "value1",
"field2": 42,
"field3": "value with spaces"
}
If your agent output valid JSON then no such function is needed.
All my special agents use pure JSON output and a “json.loads(section[0][0])” is enough.
A simple parse function for JSON section could look like this.
def parse_redfish(string_table: StringTable) -> RedfishAPIData:
"""parse one line of data to dictionary"""
try:
return json.loads(string_table[0][0])
except (IndexError, json.decoder.JSONDecodeError):
return {}
The RedfishAPIData can be ignored.
For multiline JSON every line need to be a valid JSON structure and the parse function need to respect this.
Sure, the parse function can be simplified or dropped completely depending on how the agent output is formatted. For example, the approach with string_table[0][0] won’t work if the agent output looks like this (which is also valid JSON):