JSON parsing with Nagios plugin

jsonnagios

I am looking to use Drew Kerrigan's JSON Nagios plugin on GitHub (drewkerrigan/nagios-http-json) to monitor the output from a REST API, but I am struggling to get the query syntax right.

Here is a sample of the JSON that's returned when I hit the Web API:

{"Checks":[
{"description":"can read interconnect 0910", 
"result":"passed"},
{"description":"can read interconnect 1011", 
"result":"passed"},
{"description":"can read linknode 1112", 
"result":"passed"},
{"description":"can read linknode 1213", 
"result":"passed"},
{"description":"can read dbnode 1314", 
"result":"passed"},
{"description":"can read dbnode 1415", 
"result":"passed"},
{"description":"can read dbnode 1516", 
"result":"passed"},
{"description":"can read dbnode 1617", 
"result":"passed"},
{"description":"can read dbnode 1718", 
"result":"passed"},
{"description":"can read main table", 
"result":"passed"},
{"description":"can read secondary table", 
"result":"passed"},
{"description":"can read postcode table", 
"result":"passed"},
{"description":"can read/write to docs folder", 
"result":"passed"},
{"description":"can read/write to config folder", 
"result":"passed"},
{"description":"la integration enabled", 
"result":"passed"},
{"description":"webservice password", 
"result":"passed"},
{"description":"can authenticate in largedb", 
"result":"passed"},
{"description":"can import locales", 
"result":"passed"}
]}

Drew's plugin has an equality test switch:

-q [KEY_VALUE_LIST [KEY_VALUE_LIST ...]], --key_equals [KEY_VALUE_LIST [KEY_VALUE_LIST ...]]
                        Checks equality of these keys and values
                        (key[>alias],value key2,value2) to determine status.
                        Multiple key values can be delimited with colon
                        (key,value1:value2). Return warning if equality check
                        fails

and Drew gives examples of how to structure a query for various key/value syntaxes, but I cannot seem to get it right and I am getting python errors which I expect are down to my query syntax not reflecting the JSON structure.

Can anyone help me with an example for checking whether, say, "can read main table" is "passed".

Best Answer

Drew's plugin is not coded to handle that specific JSON format, so I hacked in a small fix to take our JSON data and convert it to a more plugin-friendly structure (simple key:value pairs) - I couldn't change the format of the JSON our app generates as it is used elsewhere and too much would break. In the main program block:

[SNIP]
else:
    debugPrint(args.debug, 'DEBUG: In expected loop')
    jsondata = response.read()
    data = json.loads(jsondata)
    # Modify the data structure to a format more friendly to the plugin
    newD={}
    for item in data["Checks"]:
            newD[item.get("description")]=item.get("result")
    data = newD
    # End of modification
    debugPrint(args.debug, 'json:')
    debugPrint(args.debug, data, True)
    # Apply rules to returned JSON data
    processor = JsonRuleProcessor(data, args)
[SNIP]
Related Topic