Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How to use multiple keys? #92

Open
Napsty opened this issue Jul 16, 2024 · 1 comment
Open

Question: How to use multiple keys? #92

Napsty opened this issue Jul 16, 2024 · 1 comment
Assignees
Milestone

Comments

@Napsty
Copy link

Napsty commented Jul 16, 2024

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 martialblog self-assigned this Jul 29, 2024
@martialblog martialblog added this to the v2.3.0 milestone Jul 29, 2024
@martialblog
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants