Nagios

使用 Nagios 外掛解析 JSON

  • August 25, 2017

我希望在 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"}
]}

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 結構。

誰能幫我舉個例子來檢查“可以讀取主表”是否“通過”。

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]

引用自:https://serverfault.com/questions/868979