blob: 9c0b133bd1fe4bcba08d17a4a25141e85ebfe085 (
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
|
require 'mauve/sender'
require 'mauve/proto'
require 'mauve/mauve_thread'
require 'log4r'
module Mauve
#
# This class is responsible for sending a heartbeat to another mauve instance elsewhere.
#
class Heartbeat < MauveThread
include Singleton
#
# Allow access to some basics.
#
attr_reader :raise_after, :destination, :summary, :detail, :send_every
#
# This sets up the Heartbeat singleton
#
def initialize
super
@destination = nil
@summary = "Mauve alert server heartbeat failed"
@detail = "The Mauve server at #{Server.instance.hostname} has failed to send a heartbeat."
@raise_after = 310
@send_every = 60
end
#
# This is the time period after which an alert is raised by the remote Mauve instance.
# @param [Integer] i Seconds
# @return [Integer] Seconds
def raise_after=(i)
raise ArgumentError, "raise_after must be an integer" unless i.is_a?(Integer)
@raise_after = i
end
#
# This is the time period after which an alert is raised by the remote Mauve instance.
# @param [Integer] i Seconds
# @return [Integer] Seconds
def send_every=(i)
raise ArgumentError, "send_every must be an integer" unless i.is_a?(Integer)
@send_every = i
end
alias poll_every= send_every=
# Sets the summary of the heartbeat
#
# @param [String] s Summary
def summary=(s)
raise ArgumentError, "summary must be a string" unless s.is_a?(String)
@summary = s
end
# Sets the detail of the heartbeat
#
# @param [String] d Detail
def detail=(d)
raise ArgumentError, "detail must be a string" unless d.is_a?(String)
@detail = d
end
# Sets the destinantion Mauve instance
#
# @param [String] d Destination
#
def destination=(d)
raise ArgumentError, "destination must be a string" unless d.is_a?(String)
@destination = d
end
# @return [Log4r::Logger]
def logger
@logger ||= Log4r::Logger.new(self.class.to_s)
end
private
# @private This is the main heartbeat loop.
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 = (Time.now.to_f+self.raise_after).to_i
message.clear_time = Time.now.to_i
update.alert << message
begin
Mauve::Sender.new(self.destination).send(update)
logger.debug "Sent to #{self.destination}"
rescue => e
logger.error "Caught #{e.class}: #{e.message}"
logger.debug e.backtrace.join("\n")
raise
end
sleep @send_every
end
end
end
|