diff --git a/check_http_json.py b/check_http_json.py index f772cb3..3f1ff90 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -155,24 +155,30 @@ def get(self, key, temp_data=''): 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) < 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 it's not ([0-9*]) then it might just be a string + if key[key.find(self.arrayOpener)+1].isnumeric() or key[key.find(self.arrayOpener)+1] == "*": + return self.getSubArrayElement(key, data) + elif key in data: + return data[key] + + 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 837f014..b39e06a 100644 --- a/test/test_check_http_json.py +++ b/test/test_check_http_json.py @@ -353,7 +353,7 @@ def test_key_time(self): data = "{\"timestamp\": \"%s\",\"timestamp2\": \"%s\"}" % (now, now) self.check_data(RulesHelper().dash_dash_key_time(['timestamp,30s', 'timestamp2,30s']), data, OK_CODE) - + self.check_data(RulesHelper().dash_dash_key_time(['timestamp,30m']), data, OK_CODE) self.check_data(RulesHelper().dash_dash_key_time_critical(['timestamp,1h']), data, OK_CODE) self.check_data(RulesHelper().dash_dash_key_time(['timestamp,3h']), data, OK_CODE) @@ -482,3 +482,26 @@ def test_key_time(self): self.check_data(RulesHelper().dash_dash_key_time_critical(['timestamp,@-1h']), data, CRITICAL_CODE) self.check_data(RulesHelper().dash_dash_key_time(['timestamp,@-3h']), data, WARNING_CODE) self.check_data(RulesHelper().dash_dash_key_time_critical(['timestamp,@-2d']), 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)