From f10340acb74156812d414d7f9fce86157bc9c42b Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Mon, 19 Nov 2012 14:17:24 +0000 Subject: Show hostname in alerts. --- lib/custodian/alerter.rb | 58 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/custodian/alerter.rb b/lib/custodian/alerter.rb index f78d299..202b372 100644 --- a/lib/custodian/alerter.rb +++ b/lib/custodian/alerter.rb @@ -27,6 +27,28 @@ class Alerter end + # + # Resolve an IP address + # + def resolve_ip( target ) + begin + timeout( 4 ) do + begin + Socket.getaddrinfo(target, 'echo').each do |a| + resolved = a[3] if ( a ) + end + rescue SocketError + resolved = nil + end + end + rescue Timeout::Error => e + resolved = nil + end + end + + + + # # Expand to a message indicating whether a hostname is inside bytemark. # @@ -139,6 +161,23 @@ class Alerter inside = expand_inside_bytemark( @details["target_host"] ) + # + # Subject of the alert. + # + # If it is purely numeric then resolve it + # + subject = @details['target_host'] + if ( ( subject =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/ ) || + ( subject =~ /^([0-9a-f:]+)$/ ) ) + res = resolve_ip( subject ) + + if ( res ) + subject = "#{subject} [#{res}]" + end + end + + + # # Document the hostname if the alert relates to an IP address. # @@ -168,7 +207,7 @@ class Alerter # e.g. http-http://example.com/page1 alert.id = "#{@details['test_type']}-#{@details['target_host']}" - alert.subject = @details['target_host'] + 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 @@ -188,6 +227,21 @@ class Alerter # inside = expand_inside_bytemark( @details["target_host"] ) + # + # Subject of the alert. + # + # If it is purely numeric then resolve it + # + subject = @details['target_host'] + if ( ( subject =~ /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/ ) || + ( subject =~ /^([0-9a-f:]+)$/ ) ) + res = resolve_ip( subject ) + + if ( res ) + subject = "#{subject} [#{res}]" + end + end + # # Document the hostname if the alert relates to an IP address. @@ -219,7 +273,7 @@ class Alerter # e.g. http-http://example.com/page1 alert.id = "#{@details['test_type']}-#{@details['target_host']}" - alert.subject = @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 -- cgit v1.2.1