Migrate CheckMK Plugin from 1.x to 2.x, whats equal to parse_json

Hi René!

I wasn’t even aware that we had a parse_json function in cmk 1.x and wrote my own. However, that’s very easy:

First, your agent should use a different separator (default is whitespace, which we don’t want here). So:

<<<my_section:sep(0)>>>
{ "field1": "value1", "field2": 42, "field3": "value with spaces" }

This separator 0 means: “don’t separate anything whithin the lines; do not split at whitespace”.

On the server side this will appear as a list of lists (as usual) but each “inner list” has just one element (the complete line). It appears as:

section = [
    [ '{ "field1": "value1", "field2": 42, "field3": "value with spaces" }' ]
]

Your parse function can then be as easy as this:

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"
}
1 Like