bin/check-http-json.rb in sensu-plugins-http-0.1.2 vs bin/check-http-json.rb in sensu-plugins-http-0.2.0

- old
+ new

@@ -41,10 +41,12 @@ option :url, short: '-u URL' option :host, short: '-h HOST' option :path, short: '-p PATH' option :query, short: '-q QUERY' option :port, short: '-P PORT', proc: proc(&:to_i) + option :method, short: '-m GET|POST' + option :postbody, short: '-b /file/with/post/body' option :header, short: '-H HEADER', long: '--header HEADER' option :ssl, short: '-s', boolean: true, default: false option :insecure, short: '-k', boolean: true, default: false option :user, short: '-U', long: '--username USER' option :password, short: '-a', long: '--password PASS' @@ -86,11 +88,11 @@ return true rescue JSON::ParserError return false end - def acquire_resource # rubocop:disable all + def acquire_resource http = Net::HTTP.new(config[:host], config[:port]) if config[:ssl] http.use_ssl = true if config[:cert] @@ -100,11 +102,19 @@ end http.ca_file = config[:cacert] if config[:cacert] http.verify_mode = OpenSSL::SSL::VERIFY_NONE if config[:insecure] end - req = Net::HTTP::Get.new([config[:path], config[:query]].compact.join('?')) + if config[:method] == 'POST' + req = Net::HTTP::Post.new([config[:path], config[:query]].compact.join('?')) + else + req = Net::HTTP::Get.new([config[:path], config[:query]].compact.join('?')) + end + if config[:postbody] + post_body = IO.readlines(config[:postbody]) + req.body = post_body.join + end if !config[:user].nil? && !config[:password].nil? req.basic_auth config[:user], config[:password] end if config[:header] config[:header].split(',').each do |header| @@ -126,10 +136,10 @@ leaf = keys.reduce(json) do |tree, key| fail "could not find key: #{config[:key]}" unless tree.key?(key) tree[key] end - fail "unexpected value for key: '#{config[:value]}' != '#{leaf}'" unless leaf == config[:value] + fail "unexpected value for key: '#{config[:value]}' != '#{leaf}'" unless leaf.to_s == config[:value].to_s ok "key has expected value: '#{config[:key]}' = '#{config[:value]}'" rescue => e critical "key check failed: #{e}" end