bin/check-http.rb in sensu-plugins-http-5.1.1 vs bin/check-http.rb in sensu-plugins-http-6.0.0

- old
+ new

@@ -91,21 +91,25 @@ short: '-p PATH', long: '--request-uri PATH', description: 'Specify a uri path' option :method, - short: '-m GET|POST', - long: '--method GET|POST|PUT', - description: 'Specify a GET, POST, or PUT operation; defaults to GET', - in: %w[GET POST PUT], + short: '-m GET|HEAD|POST|PUT', + long: '--method GET|HEAD|POST|PUT', + description: 'Specify a GET, HEAD, POST, or PUT operation; defaults to GET', + in: %w[GET HEAD POST PUT], default: 'GET' option :header, short: '-H HEADER', long: '--header HEADER', description: 'Send one or more comma-separated headers with the request' + option :headerfile, + long: '--headerfile FILE', + description: 'Send headers with the request, read from FILE, separated by newline' + option :body, short: '-d BODY', long: '--body BODY', description: 'Send a data body string with the request' @@ -339,10 +343,12 @@ end req = case config[:method] when 'GET' Net::HTTP::Get.new(config[:request_uri], 'User-Agent' => config[:ua]) + when 'HEAD' + Net::HTTP::Head.new(config[:request_uri], 'User-Agent' => config[:ua]) when 'POST' Net::HTTP::Post.new(config[:request_uri], 'User-Agent' => config[:ua]) when 'PUT' Net::HTTP::Put.new(config[:request_uri], 'User-Agent' => config[:ua]) end @@ -354,18 +360,26 @@ config[:header].split(',').each do |header| h, v = header.split(':', 2) req[h.strip] = v.strip end end + + if config[:headerfile] + File.readlines(config[:headerfile]).each do |line| + h, v = line.split(':', 2) + req[h.strip] = v.strip + end + end + req.body = config[:body] if config[:body] req = apply_v4_signature(http, req, config) if config[:aws_v4] res = http.request(req) body = if config[:whole_response] - "\n" + res.body + "\n" + res.body.to_s else body = if config[:response_bytes] # rubocop:disable Lint/UselessAssignment "\n" + res.body[0..config[:response_bytes]] else '' @@ -417,13 +431,13 @@ end when /^3/ if config[:redirectok] || config[:redirectto] if config[:redirectok] # #YELLOW - ok("#{res.code}, #{size} bytes" + body) unless config[:response_code] # rubocop:disable BlockNesting + ok("#{res.code}, #{size} bytes" + body) unless config[:response_code] # rubocop:disable Metrics/BlockNesting elsif config[:redirectto] # #YELLOW - if config[:redirectto] == res['Location'] # rubocop:disable BlockNesting + if config[:redirectto] == res['Location'] # rubocop:disable Metrics/BlockNesting ok "#{res.code} found redirect to #{res['Location']}" + body else critical "Expected redirect to #{config[:redirectto]} instead redirected to #{res['Location']}" + body end end