summaryrefslogtreecommitdiff
path: root/lib/custodian/alerter.rb
blob: 69de28eab87130e72dd97bfba8a654e4c3ae12f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require 'mauve/sender'
require 'mauve/proto'

require 'ipaddr'
require 'socket'



#
#  This class encapsulates the raising and clearing of alerts via Mauve.
#
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? )

    if ( host =~ /^http/ )
        host = "<a href=\"#{host}\">#{host}</a>"
    end

    #
    #  Test trange
    #
    if ( BYTEMARK_RANGES.any?{|range| range.include?(IPAddr.new(resolved.to_s))} )
      return "<p>#{host} resolves to <tt>#{resolved}</tt> which is inside the Bytemark network.</p>"
    else
      return "<p>#{host} resolves to <tt>#{resolved}</tt> 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"

    # 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    = @details['target_host']
    alert.summary    = @details['test_alert']
    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

    Mauve::Sender.new("alert.bytemark.co.uk").send(update)

  end

  #
  #  Clear the alert.
  #
  def clear

    inside = expand_inside_bytemark( @details["target_host"] )

    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    = @details['target_host']
    alert.summary    = @details['test_alert']
    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

    Mauve::Sender.new("alert.bytemark.co.uk").send(update)
  end

end