From c6daa09ba2c39b51917570a88a750ce6a4febdc8 Mon Sep 17 00:00:00 2001 From: Markus Opolka Date: Thu, 10 Apr 2025 16:52:59 +0200 Subject: [PATCH] Adds a CLI flag `invalid-json-state `to change exit code for invalid JSON --- README.md | 4 +++- check_http_json.py | 12 +++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index dbb4e54..da67522 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,9 @@ options: -t TIMEOUT, --timeout TIMEOUT Connection timeout (seconds) --unreachable-state UNREACHABLE_STATE - Exit with specified code if URL unreachable. Examples: 1 for Warning, 2 for Critical, 3 for Unknown (default: 3) + Exit with specified code when the URL is unreachable. Examples: 1 for Warning, 2 for Critical, 3 for Unknown (default: 3) + --invalid-json-state INVALID_JSON_STATE + Exit with specified code when no valid JSON is returned. Examples: 1 for Warning, 2 for Critical, 3 for Unknown (default: 3) -B AUTH, --basic-auth AUTH Basic auth string "username:password" -D DATA, --data DATA The http payload to send as a POST diff --git a/check_http_json.py b/check_http_json.py index 1ff1718..cd47f4f 100755 --- a/check_http_json.py +++ b/check_http_json.py @@ -520,7 +520,9 @@ def parseArgs(args): parser.add_argument('-t', '--timeout', type=int, help='Connection timeout (seconds)') parser.add_argument('--unreachable-state', type=int, default=3, - help='Exit with specified code if URL unreachable. Examples: 1 for Warning, 2 for Critical, 3 for Unknown (default: 3)') + help='Exit with specified code when the URL is unreachable. Examples: 1 for Warning, 2 for Critical, 3 for Unknown (default: 3)') + parser.add_argument('--invalid-json-state', type=int, default=3, + help='Exit with specified code when no valid JSON is returned. Examples: 1 for Warning, 2 for Critical, 3 for Unknown (default: 3)') parser.add_argument('-B', '--basic-auth', dest='auth', help='Basic auth string "username:password"') parser.add_argument('-D', '--data', dest='data', @@ -728,7 +730,8 @@ def main(cliargs): if "json" in e.info().get_content_subtype(): json_data = e.read() else: - nagios.append_message(UNKNOWN_CODE, " Could not find JSON in HTTP body. HTTPError[%s], url:%s" % (str(e.code), url)) + exit_code = args.invalid_json_state + nagios.append_message(exit_code, " Could not find JSON in HTTP body. HTTPError[%s], url:%s" % (str(e.code), url)) except URLError as e: # Some users might prefer another exit code if the URL wasn't reached exit_code = args.unreachable_state @@ -741,8 +744,11 @@ def main(cliargs): # Loading the JSON data from the request data = json.loads(json_data) except ValueError as e: + exit_code = args.invalid_json_state debugPrint(args.debug, traceback.format_exc()) - nagios.append_message(UNKNOWN_CODE, " JSON Parser error: %s" % str(e)) + nagios.append_message(exit_code, " JSON Parser error: %s" % str(e)) + print(nagios.getMessage()) + sys.exit(nagios.getCode()) else: verbosePrint(args.verbose, 1, json.dumps(data, indent=2))