From 35c0a672bde3013df1ce544a77463edcc27b5903 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Wed, 14 Nov 2012 19:28:19 +0000 Subject: SHow if ips are inside our network --- lib/custodian/alerter.rb | 55 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) (limited to 'lib/custodian') 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 "

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

" + else + return "

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

" + 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}

The #{@details['test_type']} test failed against #{@details['target_host']}: #{detail}

" 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}

The #{@details['test_type']} test succeeded against #{@details['target_host']}

" alert.clear_time = Time.now.to_i update.alert << alert -- cgit v1.2.1