sensu 0.17.0.beta → 0.17.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sensu/client.rb DELETED
@@ -1,298 +0,0 @@
1
- require 'sensu/daemon'
2
- require 'sensu/socket'
3
-
4
- module Sensu
5
- class Client
6
- include Daemon
7
-
8
- attr_accessor :safe_mode
9
-
10
- def self.run(options={})
11
- client = self.new(options)
12
- EM::run do
13
- client.start
14
- client.setup_signal_traps
15
- end
16
- end
17
-
18
- def initialize(options={})
19
- super
20
- @safe_mode = @settings[:client][:safe_mode] || false
21
- @checks_in_progress = Array.new
22
- end
23
-
24
- def publish_keepalive
25
- keepalive = @settings[:client].merge({
26
- :version => VERSION,
27
- :timestamp => Time.now.to_i
28
- })
29
- payload = redact_sensitive(keepalive, @settings[:client][:redact])
30
- @logger.debug('publishing keepalive', {
31
- :payload => payload
32
- })
33
- @transport.publish(:direct, 'keepalives', MultiJson.dump(payload)) do |info|
34
- if info[:error]
35
- @logger.error('failed to publish keepalive', {
36
- :payload => payload,
37
- :error => info[:error].to_s
38
- })
39
- end
40
- end
41
- end
42
-
43
- def setup_keepalives
44
- @logger.debug('scheduling keepalives')
45
- publish_keepalive
46
- @timers[:run] << EM::PeriodicTimer.new(20) do
47
- publish_keepalive
48
- end
49
- end
50
-
51
- def publish_result(check)
52
- payload = {
53
- :client => @settings[:client][:name],
54
- :check => check
55
- }
56
- @logger.info('publishing check result', {
57
- :payload => payload
58
- })
59
- @transport.publish(:direct, 'results', MultiJson.dump(payload)) do |info|
60
- if info[:error]
61
- @logger.error('failed to publish check result', {
62
- :payload => payload,
63
- :error => info[:error].to_s
64
- })
65
- end
66
- end
67
- end
68
-
69
- def find_client_attribute(tree, path, default)
70
- attribute = tree[path.shift]
71
- if attribute.is_a?(Hash)
72
- find_client_attribute(attribute, path, default)
73
- else
74
- attribute.nil? ? default : attribute
75
- end
76
- end
77
-
78
- def substitute_command_tokens(check)
79
- unmatched_tokens = Array.new
80
- substituted = check[:command].gsub(/:::([^:].*?):::/) do
81
- token, default = $1.to_s.split('|', -1)
82
- matched = find_client_attribute(@settings[:client], token.split('.'), default)
83
- if matched.nil?
84
- unmatched_tokens << token
85
- end
86
- matched
87
- end
88
- [substituted, unmatched_tokens]
89
- end
90
-
91
- def execute_check_command(check)
92
- @logger.debug('attempting to execute check command', {
93
- :check => check
94
- })
95
- unless @checks_in_progress.include?(check[:name])
96
- @checks_in_progress << check[:name]
97
- command, unmatched_tokens = substitute_command_tokens(check)
98
- if unmatched_tokens.empty?
99
- check[:executed] = Time.now.to_i
100
- started = Time.now.to_f
101
- Spawn.process(command, :timeout => check[:timeout]) do |output, status|
102
- check[:duration] = ('%.3f' % (Time.now.to_f - started)).to_f
103
- check[:output] = output
104
- check[:status] = status
105
- publish_result(check)
106
- @checks_in_progress.delete(check[:name])
107
- end
108
- else
109
- check[:output] = 'Unmatched command tokens: ' + unmatched_tokens.join(', ')
110
- check[:status] = 3
111
- check[:handle] = false
112
- publish_result(check)
113
- @checks_in_progress.delete(check[:name])
114
- end
115
- else
116
- @logger.warn('previous check command execution in progress', {
117
- :check => check
118
- })
119
- end
120
- end
121
-
122
- def run_check_extension(check)
123
- @logger.debug('attempting to run check extension', {
124
- :check => check
125
- })
126
- check[:executed] = Time.now.to_i
127
- extension = @extensions[:checks][check[:name]]
128
- extension.safe_run do |output, status|
129
- check[:output] = output
130
- check[:status] = status
131
- publish_result(check)
132
- end
133
- end
134
-
135
- def process_check(check)
136
- @logger.debug('processing check', {
137
- :check => check
138
- })
139
- if check.has_key?(:command)
140
- if @settings.check_exists?(check[:name])
141
- check.merge!(@settings[:checks][check[:name]])
142
- execute_check_command(check)
143
- elsif @safe_mode
144
- check[:output] = 'Check is not locally defined (safe mode)'
145
- check[:status] = 3
146
- check[:handle] = false
147
- check[:executed] = Time.now.to_i
148
- publish_result(check)
149
- else
150
- execute_check_command(check)
151
- end
152
- else
153
- if @extensions.check_exists?(check[:name])
154
- run_check_extension(check)
155
- else
156
- @logger.warn('unknown check extension', {
157
- :check => check
158
- })
159
- end
160
- end
161
- end
162
-
163
- def setup_subscriptions
164
- @logger.debug('subscribing to client subscriptions')
165
- @settings[:client][:subscriptions].each do |subscription|
166
- @logger.debug('subscribing to a subscription', {
167
- :subscription => subscription
168
- })
169
- funnel = [@settings[:client][:name], VERSION, Time.now.to_i].join('-')
170
- @transport.subscribe(:fanout, subscription, funnel) do |message_info, message|
171
- begin
172
- check = MultiJson.load(message)
173
- @logger.info('received check request', {
174
- :check => check
175
- })
176
- process_check(check)
177
- rescue MultiJson::ParseError => error
178
- @logger.error('failed to parse the check request payload', {
179
- :message => message,
180
- :error => error.to_s
181
- })
182
- end
183
- end
184
- end
185
- end
186
-
187
- def calculate_execution_splay(check)
188
- key = [@settings[:client][:name], check[:name]].join(':')
189
- splay_hash = Digest::MD5.digest(key).unpack('Q<').first
190
- current_time = (Time.now.to_f * 1000).to_i
191
- (splay_hash - current_time) % (check[:interval] * 1000) / 1000.0
192
- end
193
-
194
- def schedule_checks(checks)
195
- checks.each do |check|
196
- execute_check = Proc.new do
197
- check[:issued] = Time.now.to_i
198
- process_check(check.dup)
199
- end
200
- execution_splay = testing? ? 0 : calculate_execution_splay(check)
201
- interval = testing? ? 0.5 : check[:interval]
202
- @timers[:run] << EM::Timer.new(execution_splay) do
203
- execute_check.call
204
- @timers[:run] << EM::PeriodicTimer.new(interval, &execute_check)
205
- end
206
- end
207
- end
208
-
209
- def setup_standalone
210
- @logger.debug('scheduling standalone checks')
211
- standard_checks = @settings.checks.select do |check|
212
- check[:standalone]
213
- end
214
- extension_checks = @extensions.checks.select do |check|
215
- check[:standalone] && check[:interval].is_a?(Integer)
216
- end
217
- schedule_checks(standard_checks + extension_checks)
218
- end
219
-
220
- def setup_sockets
221
- options = @settings[:client][:socket] || Hash.new
222
- options[:bind] ||= '127.0.0.1'
223
- options[:port] ||= 3030
224
- @logger.debug('binding client tcp and udp sockets', {
225
- :options => options
226
- })
227
- EM::start_server(options[:bind], options[:port], Socket) do |socket|
228
- socket.logger = @logger
229
- socket.settings = @settings
230
- socket.transport = @transport
231
- end
232
- EM::open_datagram_socket(options[:bind], options[:port], Socket) do |socket|
233
- socket.logger = @logger
234
- socket.settings = @settings
235
- socket.transport = @transport
236
- socket.protocol = :udp
237
- end
238
- end
239
-
240
- def complete_checks_in_progress(&block)
241
- @logger.info('completing checks in progress', {
242
- :checks_in_progress => @checks_in_progress
243
- })
244
- retry_until_true do
245
- if @checks_in_progress.empty?
246
- block.call
247
- true
248
- end
249
- end
250
- end
251
-
252
- def bootstrap
253
- setup_keepalives
254
- setup_subscriptions
255
- setup_standalone
256
- @state = :running
257
- end
258
-
259
- def start
260
- setup_transport
261
- setup_sockets
262
- bootstrap
263
- end
264
-
265
- def pause
266
- unless @state == :pausing || @state == :paused
267
- @state = :pausing
268
- @timers[:run].each do |timer|
269
- timer.cancel
270
- end
271
- @timers[:run].clear
272
- @transport.unsubscribe
273
- @state = :paused
274
- end
275
- end
276
-
277
- def resume
278
- retry_until_true(1) do
279
- if @state == :paused
280
- if @transport.connected?
281
- bootstrap
282
- true
283
- end
284
- end
285
- end
286
- end
287
-
288
- def stop
289
- @logger.warn('stopping')
290
- pause
291
- @state = :stopping
292
- complete_checks_in_progress do
293
- @transport.close
294
- super
295
- end
296
- end
297
- end
298
- end
data/lib/sensu/sandbox.rb DELETED
@@ -1,11 +0,0 @@
1
- module Sensu
2
- module Sandbox
3
- def self.eval(expression, value=nil)
4
- result = Proc.new do
5
- $SAFE = (RUBY_VERSION < '2.1.0' ? 4 : 3)
6
- Kernel.eval(expression)
7
- end
8
- result.call
9
- end
10
- end
11
- end