mirror of
https://github.com/drewkerrigan/nagios-http-json.git
synced 2024-11-21 18:03:48 +01:00
Improve error handling
- Added another try-catch around the CLI Rules parsing to make sure that users get a clean exit code and error messages
This commit is contained in:
parent
9d344f5a7a
commit
d3a2f3ed9e
@ -6,6 +6,7 @@ import json
|
||||
import argparse
|
||||
import sys
|
||||
import ssl
|
||||
import traceback
|
||||
from urllib.error import HTTPError
|
||||
from urllib.error import URLError
|
||||
from datetime import datetime, timedelta, timezone
|
||||
@ -720,6 +721,7 @@ def main(cliargs):
|
||||
json_data = ''
|
||||
|
||||
try:
|
||||
# Requesting the data from the URL
|
||||
json_data = make_request(args, url, context)
|
||||
except HTTPError as e:
|
||||
# Try to recover from HTTP Error, if there is JSON in the response
|
||||
@ -736,23 +738,29 @@ def main(cliargs):
|
||||
sys.exit(nagios.getCode())
|
||||
|
||||
try:
|
||||
# Loading the JSON data from the request
|
||||
data = json.loads(json_data)
|
||||
except ValueError as e:
|
||||
debugPrint(args.debug, traceback.format_exc())
|
||||
nagios.append_message(UNKNOWN_CODE, " JSON Parser error: %s" % str(e))
|
||||
else:
|
||||
verbosePrint(args.verbose, 1, json.dumps(data, indent=2))
|
||||
# Apply rules to returned JSON data
|
||||
|
||||
try:
|
||||
# Applying rules to returned JSON data
|
||||
processor = JsonRuleProcessor(data, args)
|
||||
nagios.append_message(WARNING_CODE, processor.checkWarning())
|
||||
nagios.append_message(CRITICAL_CODE, processor.checkCritical())
|
||||
nagios.append_metrics(processor.checkMetrics())
|
||||
nagios.append_message(UNKNOWN_CODE, processor.checkUnknown())
|
||||
except Exception as e: # pylint: disable=broad-exception-caught
|
||||
debugPrint(args.debug, traceback.format_exc())
|
||||
nagios.append_message(UNKNOWN_CODE, " Rule Parser error: %s" % str(e))
|
||||
|
||||
# Print Nagios specific string and exit appropriately
|
||||
print(nagios.getMessage())
|
||||
sys.exit(nagios.getCode())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Program entry point
|
||||
main(sys.argv[1:])
|
||||
|
Loading…
Reference in New Issue
Block a user