Skip to content

Commit c6daa09

Browse files
committed
Adds a CLI flag invalid-json-state to change exit code for invalid JSON
1 parent 3a1e7d9 commit c6daa09

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ options:
9393
-t TIMEOUT, --timeout TIMEOUT
9494
Connection timeout (seconds)
9595
--unreachable-state UNREACHABLE_STATE
96-
Exit with specified code if URL unreachable. Examples: 1 for Warning, 2 for Critical, 3 for Unknown (default: 3)
96+
Exit with specified code when the URL is unreachable. Examples: 1 for Warning, 2 for Critical, 3 for Unknown (default: 3)
97+
--invalid-json-state INVALID_JSON_STATE
98+
Exit with specified code when no valid JSON is returned. Examples: 1 for Warning, 2 for Critical, 3 for Unknown (default: 3)
9799
-B AUTH, --basic-auth AUTH
98100
Basic auth string "username:password"
99101
-D DATA, --data DATA The http payload to send as a POST

check_http_json.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,9 @@ def parseArgs(args):
520520
parser.add_argument('-t', '--timeout', type=int,
521521
help='Connection timeout (seconds)')
522522
parser.add_argument('--unreachable-state', type=int, default=3,
523-
help='Exit with specified code if URL unreachable. Examples: 1 for Warning, 2 for Critical, 3 for Unknown (default: 3)')
523+
help='Exit with specified code when the URL is unreachable. Examples: 1 for Warning, 2 for Critical, 3 for Unknown (default: 3)')
524+
parser.add_argument('--invalid-json-state', type=int, default=3,
525+
help='Exit with specified code when no valid JSON is returned. Examples: 1 for Warning, 2 for Critical, 3 for Unknown (default: 3)')
524526
parser.add_argument('-B', '--basic-auth', dest='auth',
525527
help='Basic auth string "username:password"')
526528
parser.add_argument('-D', '--data', dest='data',
@@ -728,7 +730,8 @@ def main(cliargs):
728730
if "json" in e.info().get_content_subtype():
729731
json_data = e.read()
730732
else:
731-
nagios.append_message(UNKNOWN_CODE, " Could not find JSON in HTTP body. HTTPError[%s], url:%s" % (str(e.code), url))
733+
exit_code = args.invalid_json_state
734+
nagios.append_message(exit_code, " Could not find JSON in HTTP body. HTTPError[%s], url:%s" % (str(e.code), url))
732735
except URLError as e:
733736
# Some users might prefer another exit code if the URL wasn't reached
734737
exit_code = args.unreachable_state
@@ -741,8 +744,11 @@ def main(cliargs):
741744
# Loading the JSON data from the request
742745
data = json.loads(json_data)
743746
except ValueError as e:
747+
exit_code = args.invalid_json_state
744748
debugPrint(args.debug, traceback.format_exc())
745-
nagios.append_message(UNKNOWN_CODE, " JSON Parser error: %s" % str(e))
749+
nagios.append_message(exit_code, " JSON Parser error: %s" % str(e))
750+
print(nagios.getMessage())
751+
sys.exit(nagios.getCode())
746752
else:
747753
verbosePrint(args.verbose, 1, json.dumps(data, indent=2))
748754

0 commit comments

Comments
 (0)