diff options
-rw-r--r-- | lib/custodian/alerter.rb | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/lib/custodian/alerter.rb b/lib/custodian/alerter.rb index 751b368..c21e1e9 100644 --- a/lib/custodian/alerter.rb +++ b/lib/custodian/alerter.rb @@ -3,6 +3,9 @@ require 'mauve/sender' require 'mauve/proto' +require 'ipaddr' +require 'socket' + # @@ -10,18 +13,64 @@ require 'mauve/proto' # class Alerter + attr_reader :details + BYTEMARK_RANGES = %w(80.68.80.0/20 89.16.160.0/19 212.110.160.0/19 46.43.0.0/18 91.223.58.0/24 213.138.96.0/19 5.153.224.0/21 2001:41c8::/32).collect{|i| IPAddr.new(i)} + def initialize( test_details ) @details = test_details end # + # Expand to a message indicating whether a hostname is inside bytemark. + # + def expand_inside_bytemark( host ) + + target = host + if ( target =~ /https?:\/\/([^\/]+)/ ) + target = $1.dup + end + + + # + # Resolved IP of the target + # + resolved = nil + + # + # Resolve the target to an IP + # + begin + Socket.getaddrinfo(target, 'echo').each do |a| + resolved = a[3] if ( a ) + end + rescue SocketError + end + + return "" unless ( !resolved.nil? ) + + # + # Test trange + # + if ( BYTEMARK_RANGES.any?{|range| range.include?(IPAddr.new(resolved.to_s))} ) + return "<p>#{host} resolves to #{target} which is inside the Bytemark network.</p>" + else + return "<p>#{host} resolves to #{target} which <b>is not</b> inside the Bytemark network.</p>" + end + + end + + + # # Raise the alert. # def raise( detail ) + inside = expand_inside_bytemark( @details["target_host"] ) + + update = Mauve::Proto::AlertUpdate.new update.alert = [] update.source = "custodian" @@ -37,7 +86,7 @@ class Alerter alert.subject = @details['target_host'] alert.summary = @details['test_alert'] - alert.detail = "The #{@details['test_type']} test failed against #{@details['target_host']}: #{detail}" + alert.detail = "#{inside} <p>The #{@details['test_type']} test failed against #{@details['target_host']}: #{detail}</p>" alert.raise_time = Time.now.to_i update.alert << alert @@ -50,6 +99,8 @@ class Alerter # def clear + inside = expand_inside_bytemark( @details["target_host"] ) + update = Mauve::Proto::AlertUpdate.new update.alert = [] update.source = "custodian" @@ -66,7 +117,7 @@ class Alerter alert.subject = @details['target_host'] alert.summary = @details['test_alert'] - alert.detail = "The #{@details['test_type']} test succeeded against #{@details['target_host']}" + alert.detail = "#{inside} <p>The #{@details['test_type']} test succeeded against #{@details['target_host']}</p>" alert.clear_time = Time.now.to_i update.alert << alert |