bin/check-http.rb in sensu-plugins-http-0.2.1 vs bin/check-http.rb in sensu-plugins-http-0.3.0
- old
+ new
@@ -14,12 +14,22 @@
#
# DEPENDENCIES:
# gem: sensu-plugin
#
# USAGE:
-# #YELLOW
+# Basic HTTP check - expect a 200 response
+# check-http.rb -u http://my.site.com
#
+# Pattern check - expect a 200 response and the string 'OK' in the body
+# check-http.rb -u http://my.site.com/health -q 'OK'
+#
+# Check response code - expect a 301 response
+# check-http.rb -u https://my.site.com/redirect --response-code 301 -r
+#
+# Use a proxy to check a URL
+# check-http.rb -u https://www.google.com --proxy-url http://my.proxy.com:3128
+#
# NOTES:
#
# LICENSE:
# Copyright 2011 Sonian, Inc <chefs@sonian.net>
# Updated by Lewis Preson 2012 to accept basic auth credentials
@@ -63,15 +73,27 @@
option :request_uri,
short: '-p PATH',
long: '--request-uri PATH',
description: 'Specify a uri path'
+ option :method,
+ short: '-m GET|POST',
+ long: '--method GET|POST',
+ description: 'Specify a GET or POST operation; defaults to GET',
+ in: %w(GET POST),
+ default: 'GET'
+
option :header,
short: '-H HEADER',
long: '--header HEADER',
- description: 'Check for a HEADER'
+ description: 'Send one or more comma-separated headers with the request'
+ option :body,
+ short: '-b BODY',
+ long: '--body BODY',
+ description: 'Send a body string with the request'
+
option :ssl,
short: '-s',
boolean: true,
description: 'Enabling SSL connections',
default: false
@@ -178,11 +200,11 @@
end
config[:port] ||= config[:ssl] ? 443 : 80
end
begin
- timeout(config[:timeout]) do
+ Timeout.timeout(config[:timeout]) do
acquire_resource
end
rescue Timeout::Error
critical 'Request timed out'
rescue => e
@@ -230,32 +252,39 @@
preverify_ok
end
end
end
- req = Net::HTTP::Get.new(config[:request_uri], 'User-Agent' => config[:ua])
+ req = case config[:method]
+ when 'GET'
+ Net::HTTP::Get.new(config[:request_uri], 'User-Agent' => config[:ua])
+ when 'POST'
+ Net::HTTP::Post.new(config[:request_uri], 'User-Agent' => config[:ua])
+ 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|
h, v = header.split(':', 2)
- req[h] = v.strip
+ req[h.strip] = v.strip
end
end
+ req.body = config[:body] if config[:body]
+
res = http.request(req)
- if config[:whole_response]
- body = "\n" + res.body
- else
- if config[:response_bytes]
- body = "\n" + res.body[0..config[:response_bytes]]
- else
- body = ''
- end
- end
+ body = if config[:whole_response]
+ "\n" + res.body
+ else
+ body = if config[:response_bytes] # rubocop:disable Lint/UselessAssignment
+ "\n" + res.body[0..config[:response_bytes]]
+ else
+ ''
+ end
+ end
if config[:require_bytes] && res.body.length != config[:require_bytes]
critical "Response was #{res.body.length} bytes instead of #{config[:require_bytes]}" + body
end
@@ -296,13 +325,12 @@
end
when /^4/, /^5/
critical(res.code + body) unless config[:response_code]
else
warning(res.code + body) unless config[:response_code]
- end
+ end
- # #YELLOW
- if config[:response_code] # rubocop:disable GuardClause
+ if config[:response_code]
if config[:response_code] == res.code
ok "#{res.code}, #{size} bytes" + body
else
critical res.code + body
end