sensu 0.16.0-java → 0.17.0.beta.1-java

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sensu/client.rb DELETED
@@ -1,292 +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 schedule_checks(checks)
188
- check_count = 0
189
- stagger = testing? ? 0 : 2
190
- checks.each do |check|
191
- check_count += 1
192
- scheduling_delay = stagger * check_count % 30
193
- @timers[:run] << EM::Timer.new(scheduling_delay) do
194
- interval = testing? ? 0.5 : check[:interval]
195
- @timers[:run] << EM::PeriodicTimer.new(interval) do
196
- check[:issued] = Time.now.to_i
197
- process_check(check.dup)
198
- end
199
- end
200
- end
201
- end
202
-
203
- def setup_standalone
204
- @logger.debug('scheduling standalone checks')
205
- standard_checks = @settings.checks.select do |check|
206
- check[:standalone]
207
- end
208
- extension_checks = @extensions.checks.select do |check|
209
- check[:standalone] && check[:interval].is_a?(Integer)
210
- end
211
- schedule_checks(standard_checks + extension_checks)
212
- end
213
-
214
- def setup_sockets
215
- options = @settings[:client][:socket] || Hash.new
216
- options[:bind] ||= '127.0.0.1'
217
- options[:port] ||= 3030
218
- @logger.debug('binding client tcp and udp sockets', {
219
- :options => options
220
- })
221
- EM::start_server(options[:bind], options[:port], Socket) do |socket|
222
- socket.logger = @logger
223
- socket.settings = @settings
224
- socket.transport = @transport
225
- end
226
- EM::open_datagram_socket(options[:bind], options[:port], Socket) do |socket|
227
- socket.logger = @logger
228
- socket.settings = @settings
229
- socket.transport = @transport
230
- socket.protocol = :udp
231
- end
232
- end
233
-
234
- def complete_checks_in_progress(&block)
235
- @logger.info('completing checks in progress', {
236
- :checks_in_progress => @checks_in_progress
237
- })
238
- retry_until_true do
239
- if @checks_in_progress.empty?
240
- block.call
241
- true
242
- end
243
- end
244
- end
245
-
246
- def bootstrap
247
- setup_keepalives
248
- setup_subscriptions
249
- setup_standalone
250
- @state = :running
251
- end
252
-
253
- def start
254
- setup_transport
255
- setup_sockets
256
- bootstrap
257
- end
258
-
259
- def pause
260
- unless @state == :pausing || @state == :paused
261
- @state = :pausing
262
- @timers[:run].each do |timer|
263
- timer.cancel
264
- end
265
- @timers[:run].clear
266
- @transport.unsubscribe
267
- @state = :paused
268
- end
269
- end
270
-
271
- def resume
272
- retry_until_true(1) do
273
- if @state == :paused
274
- if @transport.connected?
275
- bootstrap
276
- true
277
- end
278
- end
279
- end
280
- end
281
-
282
- def stop
283
- @logger.warn('stopping')
284
- pause
285
- @state = :stopping
286
- complete_checks_in_progress do
287
- @transport.close
288
- super
289
- end
290
- end
291
- end
292
- 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