Skip to content

Commit

Permalink
Add hosts resolver, and add test for DNS timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
johanek authored and majormoses committed Feb 17, 2019
1 parent a13a0ec commit e671314
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
13 changes: 10 additions & 3 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-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,10 +269,11 @@ 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])

begin
Timeout.timeout(config[:timeout]) do
Expand Down
12 changes: 9 additions & 3 deletions test/integration/helpers/serverspec/check-http-shared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
gem_path = '/usr/local/bin'
check_name = 'check-http.rb'
check = "#{gem_path}/#{check_name}"
domain = 'localhost'
domain = '127.0.0.1'

describe 'ruby environment' do
it_behaves_like 'ruby checks', check
Expand Down 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://blackhole.webpagetest.org/okay --dns-timeout 0.00001") do
its(:exit_status) { should eq 2 }
its(:stdout) { should match(/CheckHttp CRITICAL: Request error: Failed to open TCP connection to blackhole.webpagetest.org/) }
end

0 comments on commit e671314

Please sign in to comment.