From dfcdf4d87220a0a8f2e19cd154778a9899970301 Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Wed, 20 Mar 2024 16:19:41 +0100 Subject: [PATCH] Rework JsonHelper.get() to work with bracket in keys --- check_http_json.py | 31 ++++++++++++++++++------------- test/test_check_http_json.py | 27 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/check_http_json.py b/check_http_json.py index 3eb7c50..40a449d 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -156,25 +156,30 @@ class JsonHelper: data = temp_data else: data = self.data + if len(key) <= 0: return data - if key.find(self.separator) != -1 and \ - key.find(self.arrayOpener) != -1: + + + if key.find(self.separator) != -1 and key.find(self.arrayOpener) != -1: if key.find(self.separator) < key.find(self.arrayOpener): return self.getSubElement(key, data) else: return self.getSubArrayElement(key, data) - else: - if key.find(self.separator) != -1: - return self.getSubElement(key, data) - else: - if key.find(self.arrayOpener) != -1: - return self.getSubArrayElement(key, data) - else: - if isinstance(data, dict) and key in data: - return data[key] - else: - return (None, 'not_found') + + if key.find(self.separator) != -1: + return self.getSubElement(key, data) + + if key.find(self.arrayOpener) != -1: + # If we got an arrayOpener but the next char is not [0-9] or * then it might just be a string + # This isn't optimal since this 'update (foobar)(0)' still won't work + if key[key.find(self.arrayOpener)+1].isnumeric() or key[key.find(self.arrayOpener)+1] == "*": + return self.getSubArrayElement(key, data) + + if isinstance(data, dict) and key in data: + return data[key] + + return (None, 'not_found') def expandKey(self, key, keys): if '(*)' not in key: diff --git a/test/test_check_http_json.py b/test/test_check_http_json.py index 73904c7..aefe0a2 100644 --- a/test/test_check_http_json.py +++ b/test/test_check_http_json.py @@ -302,3 +302,30 @@ class UtilTest(unittest.TestCase): # This should throw an error data = '[]' self.check_data(rules.dash_q(['(*).update_status,warn_me']), data, CRITICAL_CODE) + + def test_bracket_in_key(self): + """ + https://github.com/drewkerrigan/nagios-http-json/issues/76 + """ + + rules = RulesHelper() + + # This should work + data = '[{"update status": "failure"}]' + self.check_data(rules.dash_q(['(*).update status,failure']), data, OK_CODE) + + data = '[{"update (status)": "failure"}]' + self.check_data(rules.dash_q(['(*).update (status),failure']), data, OK_CODE) + + data = '[{"update (((status)": "failure"}]' + self.check_data(rules.dash_q(['(*).update (((status),failure']), data, OK_CODE) + + data = '[{"update )status)": "failure"}]' + self.check_data(rules.dash_q(['(*).update )status),failure']), data, OK_CODE) + + data = '[{"update (status": "failure"}]' + self.check_data(rules.dash_q(['(*).update (status),failure']), data, WARNING_CODE) + + # Does not yet work + # data = '{"update (foobar)": ["bar"]}' + # self.check_data(rules.dash_q(['update (foobar)(0),bar']), data, OK_CODE)