summaryrefslogtreecommitdiff
path: root/lib/custodian/alerter.rb
diff options
context:
space:
mode:
authorSteve Kemp <steve@steve.org.uk>2012-11-20 15:39:06 +0000
committerSteve Kemp <steve@steve.org.uk>2012-11-20 15:39:06 +0000
commit8925dae5a175620423358e4b4c01216c80fe7af0 (patch)
tree49e35d6459066ad8c744df72cc8aeb6e5f392ae0 /lib/custodian/alerter.rb
parent2e53226ecaa613677007c6b1950d7b3f0b57bf2a (diff)
Refactor the generation of the alerting, to avoid code-duplication.
Diffstat (limited to 'lib/custodian/alerter.rb')
-rw-r--r--lib/custodian/alerter.rb128
1 files changed, 63 insertions, 65 deletions
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} <p>The #{@details['test_type']} test failed against #{@details['target_host']}: #{detail}</p><p>#{resolved}</p>"
- 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} <p>The #{@details['test_type']} test succeeded against #{@details['target_host']}</p><p>#{resolved}</p>"
- 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} <p>The #{@details['test_type']} test succeeded against #{@details['target_host']}</p><p>#{resolved}</p>"
- Mauve::Sender.new("alert.bytemark.co.uk").send(update)
+ #
+ # Return the alert to the caller.
+ #
+ alert
end
+
end