Class: Sensu::Client
- Inherits:
-
Object
show all
- Includes:
- Daemon
- Defined in:
- lib/sensu/client.rb
Instance Attribute Summary collapse
Attributes included from Daemon
#state
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Daemon
#load_extensions, #load_settings, #log_concerns, #pause, #resume, #setup_logger, #setup_process, #setup_redis, #setup_signal_traps, #setup_transport
Methods included from Utilities
#deep_merge, #random_uuid, #redact_sensitive, #retry_until_true, #testing?
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
18
19
20
21
22
|
# File 'lib/sensu/client.rb', line 18
def initialize(options={})
super
@safe_mode = @settings[:client][:safe_mode] || false
@checks_in_progress = Array.new
end
|
Instance Attribute Details
#safe_mode ⇒ Object
Returns the value of attribute safe_mode.
8
9
10
|
# File 'lib/sensu/client.rb', line 8
def safe_mode
@safe_mode
end
|
Class Method Details
.run(options = {}) ⇒ Object
10
11
12
13
14
15
16
|
# File 'lib/sensu/client.rb', line 10
def self.run(options={})
client = self.new(options)
EM::run do
client.start
client.setup_signal_traps
end
end
|
Instance Method Details
#complete_checks_in_progress(&block) ⇒ Object
231
232
233
234
235
236
237
238
239
240
241
|
# File 'lib/sensu/client.rb', line 231
def complete_checks_in_progress(&block)
@logger.info('completing checks in progress', {
:checks_in_progress => @checks_in_progress
})
retry_until_true do
if @checks_in_progress.empty?
block.call
true
end
end
end
|
#execute_check_command(check) ⇒ Object
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
|
# File 'lib/sensu/client.rb', line 93
def execute_check_command(check)
@logger.debug('attempting to execute check command', {
:check => check
})
unless @checks_in_progress.include?(check[:name])
@checks_in_progress << check[:name]
command, unmatched_tokens = substitute_command_tokens(check)
if unmatched_tokens.empty?
check[:executed] = Time.now.to_i
started = Time.now.to_f
Spawn.process(command, :timeout => check[:timeout]) do |output, status|
check[:duration] = ('%.3f' % (Time.now.to_f - started)).to_f
check[:output] = output
check[:status] = status
publish_result(check)
@checks_in_progress.delete(check[:name])
end
else
check[:output] = 'Unmatched command tokens: ' + unmatched_tokens.join(', ')
check[:status] = 3
check[:handle] = false
publish_result(check)
@checks_in_progress.delete(check[:name])
end
else
@logger.warn('previous check command execution in progress', {
:check => check
})
end
end
|
#find_client_attribute(tree, path, default) ⇒ Object
71
72
73
74
75
76
77
78
|
# File 'lib/sensu/client.rb', line 71
def find_client_attribute(tree, path, default)
attribute = tree[path.shift]
if attribute.is_a?(Hash)
find_client_attribute(attribute, path, default)
else
attribute.nil? ? default : attribute
end
end
|
#process_check(check) ⇒ Object
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
|
# File 'lib/sensu/client.rb', line 137
def process_check(check)
@logger.debug('processing check', {
:check => check
})
if check.has_key?(:command)
if @settings.check_exists?(check[:name])
check.merge!(@settings[:checks][check[:name]])
execute_check_command(check)
elsif @safe_mode
check[:output] = 'Check is not locally defined (safe mode)'
check[:status] = 3
check[:handle] = false
check[:executed] = Time.now.to_i
publish_result(check)
else
execute_check_command(check)
end
else
if @extensions.check_exists?(check[:name])
run_check_extension(check)
else
@logger.warn('unknown check extension', {
:check => check
})
end
end
end
|
#publish_keepalive ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/sensu/client.rb', line 24
def publish_keepalive
keepalive = @settings[:client].merge({
:version => VERSION,
:timestamp => Time.now.to_i
})
payload = redact_sensitive(keepalive, @settings[:client][:redact])
@logger.debug('publishing keepalive', {
:payload => payload
})
@transport.publish(:direct, 'keepalives', MultiJson.dump(payload)) do |info|
if info[:error]
@logger.error('failed to publish keepalive', {
:payload => payload,
:error => info[:error].to_s
})
end
end
end
|
#publish_result(check) ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/sensu/client.rb', line 53
def publish_result(check)
payload = {
:client => @settings[:client][:name],
:check => check
}
@logger.info('publishing check result', {
:payload => payload
})
@transport.publish(:direct, 'results', MultiJson.dump(payload)) do |info|
if info[:error]
@logger.error('failed to publish check result', {
:payload => payload,
:error => info[:error].to_s
})
end
end
end
|
#run_check_extension(check) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/sensu/client.rb', line 124
def run_check_extension(check)
@logger.debug('attempting to run check extension', {
:check => check
})
check[:executed] = Time.now.to_i
extension = @extensions[:checks][check[:name]]
extension.safe_run do |output, status|
check[:output] = output
check[:status] = status
publish_result(check)
end
end
|
#schedule_checks(checks) ⇒ Object
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/sensu/client.rb', line 182
def schedule_checks(checks)
check_count = 0
stagger = testing? ? 0 : 2
checks.each do |check|
check_count += 1
scheduling_delay = stagger * check_count % 30
@timers[:run] << EM::Timer.new(scheduling_delay) do
interval = testing? ? 0.5 : check[:interval]
@timers[:run] << EM::PeriodicTimer.new(interval) do
unless @state == :paused
check[:issued] = Time.now.to_i
process_check(check.dup)
end
end
end
end
end
|
#setup_keepalives ⇒ Object
43
44
45
46
47
48
49
50
51
|
# File 'lib/sensu/client.rb', line 43
def setup_keepalives
@logger.debug('scheduling keepalives')
publish_keepalive
@timers[:run] << EM::PeriodicTimer.new(20) do
unless @state == :paused
publish_keepalive
end
end
end
|
#setup_sockets ⇒ Object
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
# File 'lib/sensu/client.rb', line 211
def setup_sockets
options = @settings[:client][:socket] || Hash.new
options[:bind] ||= '127.0.0.1'
options[:port] ||= 3030
@logger.debug('binding client tcp and udp sockets', {
:options => options
})
EM::start_server(options[:bind], options[:port], Socket) do |socket|
socket.logger = @logger
socket.settings = @settings
socket.transport = @transport
end
EM::open_datagram_socket(options[:bind], options[:port], Socket) do |socket|
socket.logger = @logger
socket.settings = @settings
socket.transport = @transport
socket.reply = false
end
end
|
#setup_standalone ⇒ Object
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/sensu/client.rb', line 200
def setup_standalone
@logger.debug('scheduling standalone checks')
standard_checks = @settings.checks.select do |check|
check[:standalone]
end
extension_checks = @extensions.checks.select do |check|
check[:standalone] && check[:interval].is_a?(Integer)
end
schedule_checks(standard_checks + extension_checks)
end
|
#setup_subscriptions ⇒ Object
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/sensu/client.rb', line 165
def setup_subscriptions
@logger.debug('subscribing to client subscriptions')
@settings[:client][:subscriptions].each do |subscription|
@logger.debug('subscribing to a subscription', {
:subscription => subscription
})
funnel = [@settings[:client][:name], VERSION, Time.now.to_i].join('-')
@transport.subscribe(:fanout, subscription, funnel) do |message_info, message|
check = MultiJson.load(message)
@logger.info('received check request', {
:check => check
})
process_check(check)
end
end
end
|
#start ⇒ Object
243
244
245
246
247
248
249
250
|
# File 'lib/sensu/client.rb', line 243
def start
setup_transport
setup_keepalives
setup_subscriptions
setup_standalone
setup_sockets
super
end
|
#stop ⇒ Object
252
253
254
255
256
257
258
259
260
261
262
|
# File 'lib/sensu/client.rb', line 252
def stop
@logger.warn('stopping')
@timers[:run].each do |timer|
timer.cancel
end
@transport.unsubscribe
complete_checks_in_progress do
@transport.close
super
end
end
|
#substitute_command_tokens(check) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/sensu/client.rb', line 80
def substitute_command_tokens(check)
unmatched_tokens = Array.new
substituted = check[:command].gsub(/:::([^:].*?):::/) do
token, default = $1.to_s.split('|', -1)
matched = find_client_attribute(@settings[:client], token.split('.'), default)
if matched.nil?
unmatched_tokens << token
end
matched
end
[substituted, unmatched_tokens]
end
|