aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/sender.rb
blob: 0a481d56bdc954bd32ed5c1f93fdb3187b336790 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# encoding: UTF-8
require 'ipaddr'
require 'resolv'
require 'socket'
require 'mauve/mauve_time'
require 'pp'

module Mauve 
  class Sender
    DEFAULT_PORT = 32741

    include Resolv::DNS::Resource::IN
    
    def initialize(*destinations)
      destinations = destinations.flatten
      
      destinations = begin
        File.read("/etc/mauvealert/mauvesend.destination").split(/\s+/)
      rescue Errno::ENOENT => notfound
        []
      end if destinations.empty?
      
      if !destinations || destinations.empty?
        raise ArgumentError.new("No destinations specified, and could not read any destinations from /etc/mauvealert/mauvesend.destination")
      end
    
      #
      # Resolv results
      #
      results = []
      destinations.each do |spec|
        case spec
          when /^((?:\d{1,3}\.){3}\d{1,3})(?::(\d+))?$/
            #
            # IPv4 with a port
            #
            results << [$1, $2 || DEFAULT_PORT]

          when /^\[?([0-9a-f:]{2,39})\]??$/i
            #
            # IPv6 without a port
            #
            results << [$1, $2 || DEFAULT_PORT]

          when /^\[([0-9a-f:]{2,39})\](?::(\d+))?$/i
            #
            # IPv6 with a port
            #
            results << [$1, $2 || DEFAULT_PORT]

          when /^([^: ]+)(?::(\d+))?/
            domain = $1
            port   = $2 || DEFAULT_PORT

            Resolv::DNS.open do |dns|
              #
              # Search for SRV records first.  If the first character of the
              # domain is an underscore, assume that it is a SRV record
              #
              srv_domain = (domain[0] == ?_ ? domain : "_mauvealert._udp.#{domain}")

              list = dns.getresources(srv_domain, SRV).map do |srv|
                [srv.target.to_s, srv.port]
              end

              #
              # If nothing found, just use the domain and port
              #
              list = [[domain, port]] if list.empty?

              list.each do |d,p|
                r = []
                #
                # Try IPv6 first.
                #  
                dns.getresources(d, AAAA).map do |a|
                   r << [a.address.to_s, p]
                end 

                #
                # Try IPv4 too.
                #
                dns.getresources(d, A).each do |a|
                  r << [a.address.to_s, p]
                end

                results += r unless r.empty?
              end
           end
        end
      end

      #
      # Validate results.
      #
      @destinations = []

      results.each do |ip, port|
        ip = IPAddr.new(ip)
        @destinations << [ip, port.to_i]
      end

    end

    #
    # Returns the number of packets sent.
    #
    def send(update, verbose=0)

      #
      # Must have a source, so default to hostname if user doesn't care 
      update.source ||= `hostname -f`.chomp
      
      #
      # Make sure all alerts default to "-r now"
      #
      update.alert.each do |alert|
        next if alert.raise_time || alert.clear_time
        alert.raise_time = MauveTime.now.to_i
      end
     
      #
      # Make sure we set the transmission time
      #
      update.transmission_time = MauveTime.now.to_i 

      data = update.serialize_to_string


      if verbose == 1
        print "#{update.transmission_id}\n"
      elsif verbose >= 2
        print "Sending #{update.inspect.chomp} to #{@destinations.join(", ")}\n"
      end

      #
      # Keep a count of the number of alerts sent.
      #
      sent = 0

      @destinations.each do |ip, port|
        begin
          UDPSocket.open(ip.family).send(data, 0, ip.to_s, port)
          sent += 1
        rescue Errno::ENETUNREACH => ex
          # Catch and ignore unreachable network errors.
          warn "Got #{ex.to_s} whilst trying to send to #{ip} #{port}" if verbose > 0
        end
      end

      raise "Failed to send any packets to any destinations!" unless sent > 0

      sent
    end
  end
end