From 32401d592956845096e85f0d2c95b28e44d7d054 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Thu, 16 Apr 2015 09:35:57 +0100 Subject: Resolve hostnames for both A + AAAA. The Bytemark-specific notifier, using mauve, appends some text to the bottom of each alert: http://example.com/ resolves to 1.2.3.4 which is INSIDE|OUTSIDE bytemark This text was previously limited to the IPv4 address, but now is repeated for each family which resolves successfully. This closes #10568. This fixes #10568. --- lib/custodian/alerts/mauve.rb | 59 +++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 19 deletions(-) (limited to 'lib') diff --git a/lib/custodian/alerts/mauve.rb b/lib/custodian/alerts/mauve.rb index c3c6521..2b48fbc 100644 --- a/lib/custodian/alerts/mauve.rb +++ b/lib/custodian/alerts/mauve.rb @@ -110,7 +110,7 @@ module Custodian # we're in the working day. # if ((wday != 0) && (wday != 6)) && - (hour >= day_start && hour < day_end) + (hour >= day_start && hour < day_end) working = true end @@ -199,7 +199,7 @@ module Custodian subject = @test.target if (subject =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/) || - (subject =~ /^([0-9a-f:]+)$/) + (subject =~ /^([0-9a-f:]+)$/) res = Custodian::Util::DNS.ip_to_hostname(subject) if res subject = res @@ -289,44 +289,65 @@ module Custodian # - # Resolved IP of the target + # IP addresses we found for the host # - resolved = nil + ips = [] + # # Resolve the target to an IP, unless it is already an address. # if (target =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/) || - (target =~ /^([0-9a-f:]+)$/) - resolved = target + (target =~ /^([0-9a-f:]+)$/) + ips.push(target) else - resolved = Custodian::Util::DNS.hostname_to_ip(target) + + # + # OK if it didn't look like an IP address then attempt to + # look it up, as both IPv4 and IPv6. + # + begin + timeout(30) do + + Resolv::DNS.open do |dns| + + ress = dns.getresources(target, Resolv::DNS::Resource::IN::A) + ress.map { |r| ips.push(r.address.to_s) } + + ress = dns.getresources(target, Resolv::DNS::Resource::IN::AAAA) + ress.map { |r| ips.push(r.address.to_s) } + end + end + rescue Timeout::Error => e + return '' + end end # - # Did we get an error? + # Did we fail to lookup any IPs? # - return '' unless !resolved.nil? + return '' if ips.empty? + # + # The string we return to the caller. + # + result = '' + # # Return the formatted message # - if Custodian::Util::Bytemark.inside?(resolved.to_s) - if (resolved == target) - return "

#{host} is inside the Bytemark network.

" - else - return "

#{host} resolves to #{resolved} which is inside the Bytemark network.

" - end - else - if (resolved == target) - return "

#{host} is OUTSIDE the Bytemark network.

" + ips.each do |result| + + if Custodian::Util::Bytemark.inside?(result.to_s) + result += "

#{host} resolves to #{result} which is inside the Bytemark network.

" else - return "

#{host} resolves to #{resolved} which is OUTSIDE the Bytemark network.

" + result += "

#{host} resolves to #{result} which is OUTSIDE the Bytemark network.

" end end + result end -- cgit v1.2.1