bin/check-http-json.rb in sensu-plugins-http-2.1.0 vs bin/check-http-json.rb in sensu-plugins-http-2.2.0

- old
+ new

@@ -3,11 +3,12 @@ # check-http-json # # DESCRIPTION: # Takes either a URL or a combination of host/path/query/port/ssl, and checks # for valid JSON output in the response. Can also optionally validate simple -# string key/value pairs. +# string key/value pairs, and optionally check if a specified value is within +# bounds. # # OUTPUT: # plain text # # PLATFORMS: @@ -16,12 +17,17 @@ # DEPENDENCIES: # gem: sensu-plugin # gem: json # # USAGE: -# #YELLOW +# Check that will verify http status and JSON validity +# ./check-http-json.rb -u http://my.site.com/health.json # +# Check that will verify http status, JSON validity, and that page.totalElements value is +# greater than 10 +# ./check-http-json.rb -u http://my.site.com/metric.json --key page.totalElements --value-greater-than 10 +# # NOTES: # Based on Check HTTP by Sonian Inc. # # LICENSE: # Copyright 2013 Matt Revell <nightowlmatt@gmail.com> @@ -54,10 +60,12 @@ option :certkey, long: '--cert-key FILE' option :cacert, short: '-C FILE', long: '--cacert FILE' option :timeout, short: '-t SECS', proc: proc(&:to_i), default: 15 option :key, short: '-K KEY', long: '--key KEY' option :value, short: '-v VALUE', long: '--value VALUE' + option :valueGt, long: '--value-greater-than VALUE' + option :valueLt, long: '--value-less-than VALUE' option :whole_response, short: '-w', long: '--whole-response', boolean: true, default: false def run if config[:url] uri = URI.parse(config[:url]) @@ -174,12 +182,25 @@ begin leaf = deep_value(json, config[:key]) raise "could not find key: #{config[:key]}" if leaf.nil? - raise "unexpected value for key: '#{config[:value]}' != '#{leaf}'" unless leaf.to_s == config[:value].to_s - ok "key has expected value: '#{config[:key]}' = '#{config[:value]}'" + message = "key has expected value: '#{config[:key]}' " + if config[:value] + raise "unexpected value for key: '#{leaf}' != '#{config[:value]}'" unless leaf.to_s == config[:value].to_s + message += " equals '#{config[:value]}'" + end + if config[:valueGt] + raise "unexpected value for key: '#{leaf}' not > '#{config[:valueGt]}'" unless leaf.to_f > config[:valueGt].to_f + message += " greater than '#{config[:valueGt]}'" + end + if config[:valueLt] + raise "unexpected value for key: '#{leaf}' not < '#{config[:valueLt]}'" unless leaf.to_f < config[:valueLt].to_f + message += " less than '#{config[:valueLt]}'" + end + + ok message rescue => e critical "key check failed: #{e}" end end end