JSONparsing与Nagios插件

我期待在GitHub(drewkerrigan / nagios-http-json)上使用Drew Kerrigan的JSON Nagios插件来监视REST API的输出,但是我正在努力使查询语法正确。

以下是我在访问Web API时返回的JSON示例:

{"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"} ]} 

德鲁的插件有一个相等testing开关:

 -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 

和Drew给出了如何构build各种键/值语法的查询的例子,但我似乎无法得到正确的,我得到的python错误,我期望下到我的查询语法不反映JSON结构。

任何人都可以帮助我一个例子来检查是否“可以读取主表”是“通过”。

德鲁的插件没有编码来处理特定的JSON格式,所以我砍了一个小的修复,把我们的JSON数据,并将其转换为更多插件友好的结构(简单的键:值对) – 我不能改变格式JSON我们的应用程序生成,因为它在别处使用,太多会打破。 在主程序块中:

 [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]