drewkerrigan / nagios-http-json

A generic plugin for Nagios which checks json values from a given HTTP endpoint against argument specified rules and determines the status and performance data for that service.
Other
67 stars 60 forks source link

Question: How to use multiple keys? #92

Open Napsty opened 2 months ago

Napsty commented 2 months ago

Hi there and thanks for the plugin,

I am struggling to figure out how to use multiple keys and their value lookups in one plugin call using a wildcard operator. From the documentation description it seems possible:

-q ... Checks equality of these keys and values (key[>alias],value key2,value2) to determine status.

And in an example:

Data for keys of all items in a list (*).capacity.value:

I have the following JSON output:

{
  "service1": {
    "status": true
  },
  "service2": {
    "status": true,
    "meta": {
      "res": "PONG"
    }
  },
  "service3": {
    "status": true,
    "meta": {
      "took": 9,
      "timed_out": false,
      "_shards": {
        "total": 0,
        "successful": 0,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": {
          "value": 10000,
          "relation": "gte"
        },
        "max_score": null,
        "hits": []
      }
    }
  },
  "service4": {
    "status": true,
    "meta": {
      "status": "ok"
    }
  }
}

I obviously want to monitor the status key of each service.

But running the plugin against it using a wildcard does not work:

$ ./check_http_json.py -H api.example.com -s -p "_healthcheck" -q '(*).status,True'
Traceback (most recent call last):
  File "/home/ckadm/Git/nagios-http-json/./check_http_json.py", line 672, in <module>
    main(sys.argv[1:])
  File "/home/ckadm/Git/nagios-http-json/./check_http_json.py", line 660, in main
    nagios.append_message(WARNING_CODE, processor.checkWarning())
  File "/home/ckadm/Git/nagios-http-json/./check_http_json.py", line 338, in checkWarning
    failure += self.checkEquality(self.key_value_list)
  File "/home/ckadm/Git/nagios-http-json/./check_http_json.py", line 269, in checkEquality
    if not self.helper.equals(key, v):
  File "/home/ckadm/Git/nagios-http-json/./check_http_json.py", line 129, in equals
    return self.exists(key) and \
  File "/home/ckadm/Git/nagios-http-json/./check_http_json.py", line 145, in exists
    return (self.get(key) != (None, 'not_found'))
  File "/home/ckadm/Git/nagios-http-json/./check_http_json.py", line 164, in get
    return self.getSubArrayElement(key, data)
  File "/home/ckadm/Git/nagios-http-json/./check_http_json.py", line 124, in getSubArrayElement
    return self.get(remainingKey, data[index])
KeyError: 0

Adding each key manually into the plugin's -q parameter seems to work:

$ ./check_http_json.py -H api.example.com -s -p "_healthcheck" -q service1.status,False service2.status,False service3.status,False
WARNING: Status WARNING. Key service1.status mismatch. False != True Key service2.status mismatch. False != True Key service3.status mismatch. False != True

But this (static definition of keys to be monitored) is obviously prone to error as the number of services can change in the JSON output.

Do you have an idea how to best solve this?

martialblog commented 1 month ago

Hi, like you described here #93 the solution could look like this

./check_http_json.py -H api.example.com -s -p "health.json" -q service1.status,True service2.status,True service3.status,True

I'll add some examples to the README.