Merge pull request #98 from drewkerrigan/fix/improve-error-handling

Improve error handling
This commit is contained in:
Markus Opolka 2025-04-09 16:46:22 +02:00 committed by GitHub
commit 2dbb38512f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 2 deletions

View File

@ -172,6 +172,22 @@ options:
] ]
} }
**Data for multiple keys for an object** `-q capacity1.value,True capacity2.value,True capacity3.value,True`
{
"capacity1": {
"value": true
},
"capacity2": {
"value": true
},
"capacity3": {
"value": true
}
}
### Thresholds and Ranges ### Thresholds and Ranges
**Data**: **Data**:

View File

@ -6,6 +6,7 @@ import json
import argparse import argparse
import sys import sys
import ssl import ssl
import traceback
from urllib.error import HTTPError from urllib.error import HTTPError
from urllib.error import URLError from urllib.error import URLError
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
@ -720,6 +721,7 @@ def main(cliargs):
json_data = '' json_data = ''
try: try:
# Requesting the data from the URL
json_data = make_request(args, url, context) json_data = make_request(args, url, context)
except HTTPError as e: except HTTPError as e:
# Try to recover from HTTP Error, if there is JSON in the response # Try to recover from HTTP Error, if there is JSON in the response
@ -736,23 +738,29 @@ def main(cliargs):
sys.exit(nagios.getCode()) sys.exit(nagios.getCode())
try: try:
# 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:
debugPrint(args.debug, traceback.format_exc())
nagios.append_message(UNKNOWN_CODE, " JSON Parser error: %s" % str(e)) nagios.append_message(UNKNOWN_CODE, " JSON Parser error: %s" % str(e))
else: else:
verbosePrint(args.verbose, 1, json.dumps(data, indent=2)) 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) processor = JsonRuleProcessor(data, args)
nagios.append_message(WARNING_CODE, processor.checkWarning()) nagios.append_message(WARNING_CODE, processor.checkWarning())
nagios.append_message(CRITICAL_CODE, processor.checkCritical()) nagios.append_message(CRITICAL_CODE, processor.checkCritical())
nagios.append_metrics(processor.checkMetrics()) nagios.append_metrics(processor.checkMetrics())
nagios.append_message(UNKNOWN_CODE, processor.checkUnknown()) 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 specific string and exit appropriately
print(nagios.getMessage()) print(nagios.getMessage())
sys.exit(nagios.getCode()) sys.exit(nagios.getCode())
if __name__ == "__main__": if __name__ == "__main__":
# Program entry point # Program entry point
main(sys.argv[1:]) main(sys.argv[1:])