Skip to content

Commit

Permalink
Commit WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
johanek committed Jan 15, 2019
1 parent e56904e commit e075be6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
36 changes: 24 additions & 12 deletions bin/check-http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ class CheckHttp < Sensu::Plugin::Check::CLI
description: 'Number of seconds to wait for one block to be read',
default: 15

option :dns_timeout,
long: '--dns-resolution-timeout SECS',
proc: proc(&:to_f),
description: 'Number of seconds to allow for DNS resolution. Accepts decimal number.',
default: 0.8

option :redirectok,
short: '-r',
boolean: true,
Expand Down Expand Up @@ -263,15 +269,21 @@ def run
config[:port] ||= config[:ssl] ? 443 : 80
end

# Use Ruby DNS Resolver and set DNS resolution timeout to 800ms
# Use Ruby DNS Resolver and set DNS resolution timeout to dns_timeout value
hosts_resolver = Resolv::Hosts.new
dns_resolver = Resolv::DNS.new
dns_resolver.timeouts = 0.8
Resolv::DefaultResolver.replace_resolvers([dns_resolver])
dns_resolver.timeouts = config[:dns_timeout]
Resolv::DefaultResolver.replace_resolvers([hosts_resolver, dns_resolver])
resolved_hostname = Resolv.getaddress(config[:host])

begin
Timeout.timeout(config[:timeout]) do
acquire_resource
acquire_resource(resolved_hostname)
end
rescue Resolv::ResolvTimeout
critical 'Timed out resolving DNS'
rescue Resolv::ResolvError => e
critical "Unable to resolve DNS: #{e.message}"
rescue Net::OpenTimeout
critical 'Request timed out opening connection'
rescue Net::ReadTimeout
Expand All @@ -283,23 +295,23 @@ def run
end
end

def acquire_resource
def acquire_resource(endpoint)
http = nil

if config[:no_proxy]
http = Net::HTTP.new(config[:host], config[:port], nil, nil)
http = Net::HTTP.new(endpoint, config[:port], nil, nil)
elsif config[:proxy_url]
proxy_uri = URI.parse(config[:proxy_url])
if proxy_uri.host.nil?
unknown 'Invalid proxy url specified'
end
http = if proxy_uri.user && proxy_uri.password
Net::HTTP.new(config[:host], config[:port], proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
Net::HTTP.new(endpoint, config[:port], proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
else
Net::HTTP.new(config[:host], config[:port], proxy_uri.host, proxy_uri.port)
Net::HTTP.new(endpoint, config[:port], proxy_uri.host, proxy_uri.port)
end
else
http = Net::HTTP.new(config[:host], config[:port])
http = Net::HTTP.new(endpoint, config[:port])
end
http.read_timeout = config[:read_timeout]
http.open_timeout = config[:open_timeout]
Expand Down Expand Up @@ -333,11 +345,11 @@ def acquire_resource

req = case config[:method]
when 'GET'
Net::HTTP::Get.new(config[:request_uri], 'User-Agent' => config[:ua])
Net::HTTP::Get.new(config[:request_uri], 'User-Agent' => config[:ua], 'Host' => config[:host])
when 'POST'
Net::HTTP::Post.new(config[:request_uri], 'User-Agent' => config[:ua])
Net::HTTP::Post.new(config[:request_uri], 'User-Agent' => config[:ua], 'Host' => config[:host])
when 'PUT'
Net::HTTP::Put.new(config[:request_uri], 'User-Agent' => config[:ua])
Net::HTTP::Put.new(config[:request_uri], 'User-Agent' => config[:ua], 'Host' => config[:host])
end

unless config[:user].nil? && config[:password].nil?
Expand Down
10 changes: 8 additions & 2 deletions test/integration/helpers/serverspec/check-http-shared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@
describe command("#{check} --url https://#{domain}/okay") do
its(:exit_status) { should eq 2 }
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.3.0')
its(:stdout) { should match(/CheckHttp CRITICAL: Request error: Failed to open TCP connection to localhost:443/) }
its(:stdout) { should match(/CheckHttp CRITICAL: Request error: Failed to open TCP connection to 127.0.0.1:443/) }
else
its(:stdout) { should match(/CheckHttp CRITICAL: Request error: Cannot assign requested address - connect\(2\) for "localhost" port 443/) }
its(:stdout) { should match(/CheckHttp CRITICAL: Request error: Cannot assign requested address - connect\(2\) for "127.0.0.1" port 443/) }
end
end

# DNS timeout
describe command("#{check} --url http://nonexistantdomain/okay --dns-resolution-timeout 0.0001") do
its(:exit_status) { should eq 2 }
its(:stdout) { should match(/CheckHttp CRITICAL: Timed out resolving DNS/) }
end

0 comments on commit e075be6

Please sign in to comment.