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
158
159
160
161
162
163
164
165
166
|
# encoding: UTF-8
require 'mauve/proto'
require 'mauve/sender'
module Mauve
#
# This class can be used in simple cases where all the program needs to do is
# send an update about a single alert.
#
# In its simplest form, this could be something like
#
# Mauve::QuickUpdate.new("foo").raise!
#
# sends a "raise" to the default mauve destination about alert ID "foo".
#
# It can be used to do set more details about the alert.
#
# update = Mauve::QuickUpdate.new("foo")
# update.summary = "Foo backups failed"
# update.detail = cmd_output
# update.raise!
#
# Another example might be a heartbeat.
#
# update = Mauve::QuickUpdate.new("heartbeat")
# update.summary = "Heartbeat for this.host.name not received"
# update.detail = "Maybe this host is down, or if not, cron has stopped running."
# update.raise_at = Time.now + 600
# update.clear_at = now
# update.suppress_until = Time.now + 900
# update.send
#
class QuickUpdate
def initialize(alert_id)
raise ArgumentError, "alert_id must be a String, or respond to to_s" unless alert_id.is_a?(String) or alert_id.respond_to?("to_s")
@verbose = false
@update = Mauve::Proto::AlertUpdate.new
@update.replace = false
@update.alert = []
@alert = Mauve::Proto::Alert.new
@alert.id = alert_id.to_s
@update << @alert
end
#
# Sets the replace flag for the whole update. Defaults to false.
#
def replace=(bool)
raise ArgumentError, "replace must either be true or false" unless bool.is_a?(TrueClass) or bool.is_a?(FalseClass)
@update.replace = bool
end
#
# Sets the verbose flag for the update process. Defaults to false.
#
def verbose=(bool)
raise ArgumentError, "verbose must either be true or false" unless bool.is_a?(TrueClass) or bool.is_a?(FalseClass)
@verbose = bool
end
#
# Sets the source of the alert. Defaults to the machine's hostname.
#
def source=(s)
raise ArgumentError, "source must be a String, or respond to to_s" unless s.is_a?(String) or s.respond_to?("to_s")
@update.source = s.to_s
end
#
# Sets the alert summary. Must be a string or something that can convert to a string.
#
def summary=(s)
raise ArgumentError, "summary must be a String, or respond to to_s" unless s.is_a?(String) or s.respond_to?("to_s")
@alert.summary = s.to_s
end
#
# Sets the alert detail. Must be a string or something that can convert to a string.
#
def detail=(s)
raise ArgumentError, "detail must be a String, or respond to to_s" unless s.is_a?(String) or s.respond_to?("to_s")
@alert.detail = s
end
#
# Sets the alert summary. Must be a string or something that can convert to a string.
#
def subject=(s)
raise ArgumentError, "subject must be a String, or respond to to_s" unless s.is_a?(String) or s.respond_to?("to_s")
@alert.subject = s
end
#
# Sets the raise time. Must be an Integer (epoch time) or a Time.
#
def raise_time=(t)
raise ArgumentError, "raise_time must be a Time or an Integer" unless t.is_a?(Time) or t.is_a?(Integer)
t = t.to_i if t.is_a?(Time)
@alert.raise_time = t
end
alias raise_at= raise_time=
#
# Sets the clear time. Must be an Integer (epoch time) or a Time.
#
def clear_time=(t)
clear ArgumentError, "clear_time must be a Time or an Integer" unless t.is_a?(Time) or t.is_a?(Integer)
t = t.to_i if t.is_a?(Time)
@alert.clear_time = t
end
alias clear_at= clear_time=
#
# Sets the time after which alerts will get sent. Must be an Integer (epoch time) or a Time.
#
def suppress_until=(t)
clear ArgumentError, "suppress_until must be a Time or an Integer" unless t.is_a?(Time) or t.is_a?(Integer)
t = t.to_i if t.is_a?(Time)
@alert.suppress_until = t
end
#
# Immediately send a raise message. The raise_time defaults to Time#now.
#
def raise!(t = Time.now)
self.raise_time = t
self.send
end
#
# Immediately send a clear message. The clear_time defaults to Time#now.
#
def clear!(t = Time.now)
self.clear_time = t
self.send
end
#
# This sends the alert. If destinations are left as nil, then the default
# as per Mauve::Sender are used.
#
def send(destinations = nil)
Mauve::Sender.new(destinations).send(@update, @verbose)
end
end
end
|