Skip to content

Commit

Permalink
Merge pull request #52 from candux/exitGracefully
Browse files Browse the repository at this point in the history
Exit gracefully when JSON can't be parsed
  • Loading branch information
phrawzty authored May 29, 2023
2 parents 45d649f + 1e0734b commit 09cd031
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions check_http_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]])
Expand Down

0 comments on commit 09cd031

Please sign in to comment.