Adds a CLI flag invalid-json-state to change exit code for invalid JSON

This commit is contained in:
Markus Opolka 2025-04-10 16:52:59 +02:00
parent 3a1e7d90d0
commit c6daa09ba2
2 changed files with 12 additions and 4 deletions

View File

@ -93,7 +93,9 @@ options:
-t TIMEOUT, --timeout TIMEOUT -t TIMEOUT, --timeout TIMEOUT
Connection timeout (seconds) Connection timeout (seconds)
--unreachable-state UNREACHABLE_STATE --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 -B AUTH, --basic-auth AUTH
Basic auth string "username:password" Basic auth string "username:password"
-D DATA, --data DATA The http payload to send as a POST -D DATA, --data DATA The http payload to send as a POST

View File

@ -520,7 +520,9 @@ def parseArgs(args):
parser.add_argument('-t', '--timeout', type=int, parser.add_argument('-t', '--timeout', type=int,
help='Connection timeout (seconds)') help='Connection timeout (seconds)')
parser.add_argument('--unreachable-state', type=int, default=3, 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', parser.add_argument('-B', '--basic-auth', dest='auth',
help='Basic auth string "username:password"') help='Basic auth string "username:password"')
parser.add_argument('-D', '--data', dest='data', parser.add_argument('-D', '--data', dest='data',
@ -728,7 +730,8 @@ def main(cliargs):
if "json" in e.info().get_content_subtype(): if "json" in e.info().get_content_subtype():
json_data = e.read() json_data = e.read()
else: 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: except URLError as e:
# Some users might prefer another exit code if the URL wasn't reached # Some users might prefer another exit code if the URL wasn't reached
exit_code = args.unreachable_state exit_code = args.unreachable_state
@ -741,8 +744,11 @@ def main(cliargs):
# Loading the JSON data from the request # Loading the JSON data from the request
data = json.loads(json_data) data = json.loads(json_data)
except ValueError as e: except ValueError as e:
exit_code = args.invalid_json_state
debugPrint(args.debug, traceback.format_exc()) 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: else:
verbosePrint(args.verbose, 1, json.dumps(data, indent=2)) verbosePrint(args.verbose, 1, json.dumps(data, indent=2))