@@ -520,7 +520,9 @@ def parseArgs(args):
520
520
parser .add_argument ('-t' , '--timeout' , type = int ,
521
521
help = 'Connection timeout (seconds)' )
522
522
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)' )
524
526
parser .add_argument ('-B' , '--basic-auth' , dest = 'auth' ,
525
527
help = 'Basic auth string "username:password"' )
526
528
parser .add_argument ('-D' , '--data' , dest = 'data' ,
@@ -728,7 +730,8 @@ def main(cliargs):
728
730
if "json" in e .info ().get_content_subtype ():
729
731
json_data = e .read ()
730
732
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 ))
732
735
except URLError as e :
733
736
# Some users might prefer another exit code if the URL wasn't reached
734
737
exit_code = args .unreachable_state
@@ -741,8 +744,11 @@ def main(cliargs):
741
744
# Loading the JSON data from the request
742
745
data = json .loads (json_data )
743
746
except ValueError as e :
747
+ exit_code = args .invalid_json_state
744
748
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 ())
746
752
else :
747
753
verbosePrint (args .verbose , 1 , json .dumps (data , indent = 2 ))
748
754
0 commit comments