diff --git a/check_http_json.rb b/check_http_json.rb index 55a359b..6477def 100755 --- a/check_http_json.rb +++ b/check_http_json.rb @@ -231,8 +231,14 @@ def uri_target(options) say(options[:v], "RESPONSE:\n---\n%s\n---" % [response.body]) - # Make a JSON object from the response. - json = JSON.parse response.body + begin + # Make a JSON object from the response. + json = JSON.parse response.body + rescue Exception => e + say(options[:v], 'Could not parse JSON from HTTP response: %s.' % [e]) + msg = 'Parsing JSON failed.' + Nagios.do_exit(3, msg) + end return json end @@ -253,8 +259,14 @@ def file_target(options) Nagios.do_exit(2, msg) end - # Make a JSON object from the contents of the file. - json = JSON.parse(File.read(options[:file])) + begin + # Make a JSON object from the contents of the file. + json = JSON.parse(File.read(options[:file])) + rescue Exception => e + say(options[:v], 'Could not parse JSON from input file: %s.' % [e]) + msg = 'Parsing JSON failed.' + Nagios.do_exit(3, msg) + end return json end @@ -516,7 +528,7 @@ def sanity_check(options) options[:perf_regex].each do |x| json_flat.each do |k, _| next unless k =~ Regexp.new(x) - + say(options[:v], 'Found perf %s as %s' % [x, k]) p.push("%s=%s" % [k, json_flat[k]]) # do not add all elements if not enabled @@ -619,7 +631,7 @@ def sanity_check(options) # check next element next end - + # If we're specifying critical & warning regex... if options[:result_regex_warn] && options[:result_regex_crit] say(options[:v], '%s should not match against \'%s\' (REGEX), else CRIT' % [element, options[:result_regex_crit]])