From 8925dae5a175620423358e4b4c01216c80fe7af0 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Tue, 20 Nov 2012 15:39:06 +0000 Subject: Refactor the generation of the alerting, to avoid code-duplication. --- lib/custodian/alerter.rb | 128 +++++++++++++++++++++++------------------------ 1 file changed, 63 insertions(+), 65 deletions(-) (limited to 'lib') diff --git a/lib/custodian/alerter.rb b/lib/custodian/alerter.rb index 086d5b3..aafd089 100644 --- a/lib/custodian/alerter.rb +++ b/lib/custodian/alerter.rb @@ -133,88 +133,96 @@ class Alerter # # Generate an alert-message which will be raised via mauve. # - # TODO: Refactor into common code: raise/clear duplicate too much. - # def raise( detail ) # - # Is this alert affecting a machine inside/outside our network + # Get ready to send to mauve. # - inside = expand_inside_bytemark( @details["target_host"] ) - + update = Mauve::Proto::AlertUpdate.new + update.alert = [] + update.source = "custodian" + update.replace = false # - # Subject of the alert. - # - # If it is purely numeric then resolve it + # Construct an alert with our test details. # - subject = @details['target_host'] - if ( ( subject =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/ ) || - ( subject =~ /^([0-9a-f:]+)$/ ) ) - res = DNSUtil.ip_to_hostname( subject ) + alert = get_alert() - if ( res ) - subject = res - end - end + # + # We're raising this alert. + # + alert.raise_time = Time.now.to_i # - # Document the hostname if the alert relates to an IP address. + # Update it and send it # - resolved = "" - if ( ( @details["target_host"] =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/ ) || - ( @details["target_host"] =~ /^([0-9a-f:]+)$/ ) ) + update.alert << alert + Mauve::Sender.new("alert.bytemark.co.uk").send(update) + end - resolved = DNSUtil.ip_to_hostname( @details["target_host"] ) - if ( resolved.nil? ) - resolved = "" - else - resolved = "The IP address #{@details["target_host"]} resolves to #{resolved}." - end - end + # + # Generate an alert-message which will be cleared via mauve. + # + def clear + + # + # Get ready to send to mauve. + # update = Mauve::Proto::AlertUpdate.new update.alert = [] update.source = "custodian" - - # be explicit about raising/clearing update.replace = false - alert = Mauve::Proto::Alert.new - # e.g. ping-example.vm.bytemark.co.uk - # e.g. http-http://example.com/page1 - alert.id = "#{@details['test_type']}-#{@details['target_host']}" + # + # Construct an alert with our test details. + # + alert = get_alert() - alert.subject = subject - alert.summary = @details['test_alert'] - alert.detail = "#{inside}

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

#{resolved}

" - alert.raise_time = Time.now.to_i - update.alert << alert + # + # We're clearing this alert. + # + alert.clear_time = Time.now.to_i + # + # Update it and send it + # + update.alert << alert Mauve::Sender.new("alert.bytemark.co.uk").send(update) - end + # - # Generate an alert-message which will be cleared via mauve. + # Using the test-data-hash which was set in the constructor + # generate a useful alert that can be fired off to mauve. # - # TODO: Refactor into common code: raise/clear duplicate too much. + # Most of the mess of this method is ensuring there is some + # "helpful" data in the detail-field of the alert. # - def clear + def get_alert # - # Is this alert affecting a machine inside/outside our network + # Is this alert affecting a machine inside/outside our network? # inside = expand_inside_bytemark( @details["target_host"] ) + + # + # The subject of an alert should be one of: + # + # 1. Hostname. # - # Subject of the alert. + # 2. IP address + # + # 3. A URL. + # + # + # We attempt to resolve the alert to the hostname, as that is readable. # - # If it is purely numeric then resolve it # subject = @details['target_host'] if ( ( subject =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/ ) || @@ -241,27 +249,17 @@ class Alerter end - update = Mauve::Proto::AlertUpdate.new - update.alert = [] - update.source = "custodian" - - # be explicit about raising/clearing - update.replace = false - - - alert = Mauve::Proto::Alert.new - - # e.g. ping-example.vm.bytemark.co.uk - # e.g. http-http://example.com/page1 - alert.id = "#{@details['test_type']}-#{@details['target_host']}" - - alert.subject = subject - alert.summary = @details['test_alert'] - alert.detail = "#{inside}

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

#{resolved}

" - alert.clear_time = Time.now.to_i - update.alert << alert + alert = Mauve::Proto::Alert.new + alert.id = "#{@details['test_type']}-#{@details['target_host']}" + alert.subject = subject + alert.summary = @details['test_alert'] + alert.detail = "#{inside}

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

#{resolved}

" - Mauve::Sender.new("alert.bytemark.co.uk").send(update) + # + # Return the alert to the caller. + # + alert end + end -- cgit v1.2.1