Skip to content

Commit

Permalink
Add handling of empty JSON return values
Browse files Browse the repository at this point in the history
 - Will now throw a CRITICAL
  • Loading branch information
martialblog committed Jun 26, 2020
1 parent 47bdea7 commit 25fb340
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions check_http_json.py
Expand Up @@ -346,6 +346,8 @@ def checkWarning(self):

def checkCritical(self):
failure = ''
if not self.data:
failure = " Empty JSON data."
if self.key_threshold_critical is not None:
failure += self.checkThresholds(self.key_threshold_critical)
if self.key_value_list_critical is not None:
Expand Down
22 changes: 21 additions & 1 deletion test/test_check_http_json.py
Expand Up @@ -256,7 +256,7 @@ def test_array_with_missing_element(self):

# This should not throw a KeyError
data = '{}'
self.check_data(rules.dash_q(['(0).Node,foobar', '(1).Node,missing']), data, WARNING_CODE)
self.check_data(rules.dash_q(['(0).Node,foobar', '(1).Node,missing']), data, CRITICAL_CODE)

def test_subelem(self):

Expand All @@ -274,3 +274,23 @@ def test_subarrayelem_missing_elem(self):
self.check_data(rules.dash_E(['(*).capacity.value.too_deep']), data, CRITICAL_CODE)
# Should not throw keyerror
self.check_data(rules.dash_E(['foo']), data, CRITICAL_CODE)


def test_empty_key_value_array(self):
"""
https://github.com/drewkerrigan/nagios-http-json/issues/61
"""

rules = RulesHelper()

# This should simply work
data = '[{"update_status": "finished"},{"update_status": "finished"}]'
self.check_data(rules.dash_q(['(*).update_status,finished']), data, OK_CODE)

# This should warn us
data = '[{"update_status": "finished"},{"update_status": "failure"}]'
self.check_data(rules.dash_q(['(*).update_status,finished']), data, WARNING_CODE)

# This should throw an error
data = '[]'
self.check_data(rules.dash_q(['(*).update_status,warn_me']), data, CRITICAL_CODE)
2 changes: 1 addition & 1 deletion test/test_main.py
Expand Up @@ -12,7 +12,7 @@


class MockResponse():
def __init__(self, status_code=200, content='{}'):
def __init__(self, status_code=200, content='{"foo": "bar"}'):
self.status_code = status_code
self.content = content

Expand Down

0 comments on commit 25fb340

Please sign in to comment.