aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/heartbeat.rb
blob: 0f51f80a553efb03b594933c212df2e74328089b (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
require 'mauve/sender'
require 'mauve/proto'
require 'mauve/mauve_thread'
require 'log4r'

#
# This class is responsible for sending a heartbeat to another mauve instance elsewhere.
#
module Mauve

  class Heartbeat < MauveThread

    include Singleton

    attr_accessor :destination, :summary, :detail
    attr_reader   :sleep_interval, :raise_at 

    def initialize
      super

      @destination    = nil
      @summary        = "Mauve alert server down."
      @detail         = "The Mauve server at #{Server.instance.hostname} has failed to send a heartbeat."
      self.raise_at   = 600
    end

    def raise_at=(i)
      @raise_at = i
      @sleep_interval = ((i.to_f)/2.5).round.to_i
    end

    def logger
      @logger ||= Log4r::Logger.new(self.class.to_s)
    end

    def main_loop
      #
      # Don't send if no destination set.
      #
      return if @destination.nil?

      update = Mauve::Proto::AlertUpdate.new
      update.replace = false
      update.alert = []
      update.source = Server.instance.hostname
      update.transmission_id = rand(2**63)

      message = Mauve::Proto::Alert.new
      message.id = "mauve-heartbeat"
      message.summary = self.summary
      message.detail = self.detail
      message.raise_time = (MauveTime.now.to_f+self.raise_at).to_i
      message.clear_time = MauveTime.now.to_i

      update.alert << message

      Mauve::Sender.new(self.destination).send(update)
    end

  end

end