使用 Nagios 插件进行 JSON 解析

使用 Nagios 插件进行 JSON 解析

我希望使用 GitHub 上的 Drew Kerrigan 的 JSON Nagios 插件(drewkerrigan/nagios-http-json)来监控来自 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"}
]}

Drew 的插件有一个相等性测试开关:

-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 给出了如何为各种键/值语法构建查询的示例,但我似乎无法正确理解,并且出现了 Python 错误,我预计这是因为我的查询语法没有反映 JSON 结构。

有人能帮我举个例子来检查“可以读取主表”是否“通过”吗?

答案1

Drew 的插件没有编码来处理特定的 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]

相关内容