sensu 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.org CHANGED
@@ -35,6 +35,6 @@
35
35
  - [[http://twitter.com/amdprophet][Justin Kolberg]]
36
36
  - [[http://twitter.com/kartar][James Turnbull]]
37
37
  - [[http://twitter.com/joshpasqualetto][Josh Pasqualetto]]
38
- - Steve Lum
38
+ - [[http://github.com/lum][Steve Lum]]
39
39
  - [[http://twitter.com/miller_joe][Joe Miller]]
40
40
  - [[http://twitter.com/decklin][Decklin Foster]]
data/lib/sensu.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Sensu
2
- VERSION = "0.9.0"
2
+ VERSION = "0.9.1"
3
3
  end
data/lib/sensu/api.rb CHANGED
@@ -3,14 +3,14 @@ require File.join(File.dirname(__FILE__), 'config')
3
3
  require 'sinatra/async'
4
4
  require 'redis'
5
5
 
6
- require File.join(File.dirname(__FILE__), 'helpers', 'redis')
6
+ require File.join(File.dirname(__FILE__), 'patches', 'redis')
7
7
 
8
8
  module Sensu
9
9
  class API < Sinatra::Base
10
10
  register Sinatra::Async
11
11
 
12
12
  def self.run(options={})
13
- EM.run do
13
+ EM::run do
14
14
  self.setup(options)
15
15
  self.run!(:port => $settings.api.port)
16
16
 
@@ -39,13 +39,6 @@ module Sensu
39
39
  $amq = AMQP::Channel.new(rabbitmq)
40
40
  end
41
41
 
42
- def self.stop(signal)
43
- $logger.warn('[process] -- ' + signal + ' -- stopping sensu api')
44
- EM.add_timer(1) do
45
- EM.stop
46
- end
47
- end
48
-
49
42
  before do
50
43
  content_type 'application/json'
51
44
  end
@@ -90,7 +83,7 @@ module Sensu
90
83
  }
91
84
  $amq.queue('results').publish({:client => client, :check => check}.to_json)
92
85
  end
93
- EM.add_timer(8) do
86
+ EM::Timer.new(5) do
94
87
  $redis.srem('clients', client)
95
88
  $redis.del('events:' + client)
96
89
  $redis.del('client:' + client)
@@ -253,7 +246,7 @@ module Sensu
253
246
  end
254
247
  end
255
248
 
256
- def self.test(options={})
249
+ def self.create_test_scaffolding(options={})
257
250
  self.setup(options)
258
251
  $settings.client.timestamp = Time.now.to_i
259
252
  $redis.set('client:' + $settings.client.name, $settings.client.to_json).callback do
@@ -271,5 +264,12 @@ module Sensu
271
264
  end
272
265
  end
273
266
  end
267
+
268
+ def self.stop(signal)
269
+ $logger.warn('[stop] -- stopping sensu api -- ' + signal)
270
+ EM::Timer.new(1) do
271
+ EM::stop_event_loop
272
+ end
273
+ end
274
274
  end
275
275
  end
data/lib/sensu/client.rb CHANGED
@@ -10,7 +10,7 @@ module Sensu
10
10
  if options[:pid_file]
11
11
  Process.write_pid(options[:pid_file])
12
12
  end
13
- EM.run do
13
+ EM::run do
14
14
  client.setup_amqp
15
15
  client.setup_keepalives
16
16
  client.setup_subscriptions
@@ -31,13 +31,6 @@ module Sensu
31
31
  @logger = config.logger || config.open_log
32
32
  end
33
33
 
34
- def stop(signal)
35
- @logger.warn('[stop] -- stopping sensu client -- ' + signal)
36
- EM.add_timer(1) do
37
- EM.stop
38
- end
39
- end
40
-
41
34
  def setup_amqp
42
35
  @logger.debug('[amqp] -- connecting to rabbitmq')
43
36
  rabbitmq = AMQP.connect(@settings.rabbitmq.to_hash.symbolize_keys)
@@ -53,13 +46,13 @@ module Sensu
53
46
  def setup_keepalives
54
47
  @logger.debug('[keepalive] -- setup keepalives')
55
48
  publish_keepalive
56
- EM.add_periodic_timer(30) do
49
+ EM::PeriodicTimer.new(30) do
57
50
  publish_keepalive
58
51
  end
59
52
  end
60
53
 
61
54
  def publish_result(check)
62
- @logger.info('[result] -- publishing check result -- ' + check.status.to_s + ' -- ' + check.name)
55
+ @logger.info('[result] -- publishing check result -- ' + [check.name, check.status, check.output].join(' -- '))
63
56
  @amq.queue('results').publish({
64
57
  :client => @settings.client.name,
65
58
  :check => check.to_hash
@@ -95,7 +88,7 @@ module Sensu
95
88
  end
96
89
  @checks_in_progress.delete(check.name)
97
90
  end
98
- EM.defer(execute, publish)
91
+ EM::defer(execute, publish)
99
92
  else
100
93
  @logger.warn('[execute] -- missing client attributes -- ' + unmatched_tokens.join(', ') + ' -- ' + check.name)
101
94
  check.status = 3
@@ -160,11 +153,11 @@ module Sensu
160
153
 
161
154
  def setup_queue_monitor
162
155
  @logger.debug('[monitor] -- setup queue monitor')
163
- EM.add_periodic_timer(5) do
156
+ EM::PeriodicTimer.new(5) do
164
157
  unless @check_queue.subscribed?
165
158
  @logger.warn('[monitor] -- re-subscribing to subscriptions')
166
159
  @check_queue.delete
167
- EM.add_timer(1) do
160
+ EM::Timer.new(1) do
168
161
  setup_subscriptions
169
162
  end
170
163
  end
@@ -173,12 +166,19 @@ module Sensu
173
166
 
174
167
  def setup_socket
175
168
  @logger.debug('[socket] -- starting up socket')
176
- EM.start_server('127.0.0.1', 3030, ClientSocket) do |socket|
169
+ EM::start_server('127.0.0.1', 3030, ClientSocket) do |socket|
177
170
  socket.settings = @settings
178
171
  socket.logger = @logger
179
172
  socket.amq = @amq
180
173
  end
181
174
  end
175
+
176
+ def stop(signal)
177
+ @logger.warn('[stop] -- stopping sensu client -- ' + signal)
178
+ EM::Timer.new(1) do
179
+ EM::stop_event_loop
180
+ end
181
+ end
182
182
  end
183
183
 
184
184
  class ClientSocket < EM::Connection
@@ -198,7 +198,7 @@ module Sensu
198
198
  :check => check.to_hash
199
199
  }.to_json)
200
200
  else
201
- @logger.warn('[socket] -- a check name, exit status, and output are required -- e.g. {"name": "x", "status": 0, output: "y"}')
201
+ @logger.warn('[socket] -- a check name, exit status, and output are required -- e.g. {"name": "x", "status": 0, "output": "y"}')
202
202
  end
203
203
  rescue JSON::ParserError => error
204
204
  @logger.warn('[socket] -- check result must be valid JSON: ' + error.to_s)
data/lib/sensu/config.rb CHANGED
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), 'helpers', 'ruby')
1
+ require File.join(File.dirname(__FILE__), 'patches', 'ruby')
2
2
 
3
3
  require 'rubygems' if RUBY_VERSION < '1.9.0'
4
4
  require 'bundler'
@@ -38,6 +38,10 @@ module Sensu
38
38
  end
39
39
  end
40
40
 
41
+ def invalid_config(message)
42
+ raise 'configuration invalid, ' + message
43
+ end
44
+
41
45
  def open_log
42
46
  @logger = Cabin::Channel.new
43
47
  if File.writable?(@options[:log_file]) || !File.exist?(@options[:log_file]) && File.writable?(File.dirname(@options[:log_file]))
@@ -89,31 +93,6 @@ module Sensu
89
93
  end
90
94
  end
91
95
 
92
- def validate_config
93
- if @logger
94
- @logger.debug('[config] -- validating configuration')
95
- end
96
- has_keys(%w[rabbitmq checks])
97
- validate_common_config
98
- case @options[:service]
99
- when 'rake'
100
- has_keys(%w[redis api handlers client])
101
- validate_server_config
102
- validate_client_config
103
- when 'sensu-server'
104
- has_keys(%w[redis handlers])
105
- validate_server_config
106
- when 'sensu-api'
107
- has_keys(%w[redis api])
108
- when 'sensu-client'
109
- has_keys(%w[client])
110
- validate_client_config
111
- end
112
- if @logger
113
- @logger.debug('[config] -- configuration valid -- running ' + @options[:service])
114
- end
115
- end
116
-
117
96
  def validate_common_config
118
97
  @settings.checks.each do |name, details|
119
98
  unless details.interval.is_a?(Integer) && details.interval > 0
@@ -149,7 +128,7 @@ module Sensu
149
128
  unless details.key?('type')
150
129
  invalid_config('missing type for handler ' + name)
151
130
  end
152
- case details.type
131
+ case details['type']
153
132
  when 'pipe'
154
133
  unless details.key?('command')
155
134
  invalid_config('missing command for pipe handler ' + name)
@@ -187,8 +166,29 @@ module Sensu
187
166
  end
188
167
  end
189
168
 
190
- def invalid_config(message)
191
- raise 'configuration invalid, ' + message
169
+ def validate_config
170
+ if @logger
171
+ @logger.debug('[config] -- validating configuration')
172
+ end
173
+ has_keys(%w[rabbitmq checks])
174
+ validate_common_config
175
+ case @options[:service]
176
+ when 'rake'
177
+ has_keys(%w[redis api handlers client])
178
+ validate_server_config
179
+ validate_client_config
180
+ when 'sensu-server'
181
+ has_keys(%w[redis handlers])
182
+ validate_server_config
183
+ when 'sensu-api'
184
+ has_keys(%w[redis api])
185
+ when 'sensu-client'
186
+ has_keys(%w[client])
187
+ validate_client_config
188
+ end
189
+ if @logger
190
+ @logger.debug('[config] -- configuration valid -- running ' + @options[:service])
191
+ end
192
192
  end
193
193
 
194
194
  def self.read_arguments(arguments)
@@ -12,7 +12,7 @@ module Redis
12
12
 
13
13
  def unbind
14
14
  unless !@connected || @closing_connection
15
- EM.add_timer(1) do
15
+ EM::Timer.new(1) do
16
16
  reconnect(@host, @port)
17
17
  end
18
18
  else
@@ -26,6 +26,6 @@ module Redis
26
26
  def self.connect(options={})
27
27
  host = options[:host] || 'localhost'
28
28
  port = options[:port] || 6379
29
- EM.connect(host, port, Redis::Client)
29
+ EM::connect(host, port, Redis::Client)
30
30
  end
31
31
  end
File without changes
data/lib/sensu/server.rb CHANGED
@@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), 'config')
2
2
 
3
3
  require 'redis'
4
4
 
5
- require File.join(File.dirname(__FILE__), 'helpers', 'redis')
5
+ require File.join(File.dirname(__FILE__), 'patches', 'redis')
6
6
 
7
7
  module Sensu
8
8
  class Server
@@ -16,8 +16,8 @@ module Sensu
16
16
  if options[:pid_file]
17
17
  Process.write_pid(options[:pid_file])
18
18
  end
19
- EM.threadpool_size = 16
20
- EM.run do
19
+ EM::threadpool_size = 16
20
+ EM::run do
21
21
  server.setup_redis
22
22
  server.setup_amqp
23
23
  server.setup_keepalives
@@ -37,17 +37,7 @@ module Sensu
37
37
  config = Sensu::Config.new(options)
38
38
  @settings = config.settings
39
39
  @logger = config.logger || config.open_log
40
- @status = nil
41
- end
42
-
43
- def stop_reactor
44
- EM.add_timer(3) do
45
- EM.stop
46
- end
47
- end
48
-
49
- def stopping?
50
- @status == :stopping
40
+ @timers = Array.new
51
41
  end
52
42
 
53
43
  def setup_redis
@@ -91,8 +81,8 @@ module Sensu
91
81
  if @settings.handlers.key?(handler)
92
82
  @logger.debug('[event] -- handling event -- ' + [handler, event.client.name, event.check.name].join(' -- '))
93
83
  details = @settings.handlers[handler]
94
- case details.type
95
- when "pipe"
84
+ case details['type']
85
+ when 'pipe'
96
86
  handle = proc do
97
87
  output = ''
98
88
  Bundler.with_clean_env do
@@ -108,12 +98,12 @@ module Sensu
108
98
  end
109
99
  output
110
100
  end
111
- EM.defer(handle, report)
112
- when "amqp"
101
+ EM::defer(handle, report)
102
+ when 'amqp'
113
103
  exchange = details.exchange.name
114
- exchange_type = details.exchange.key?('type') ? details.exchange.type.to_sym : :direct
104
+ exchange_type = details.exchange.key?('type') ? details.exchange['type'].to_sym : :direct
115
105
  exchange_options = details.exchange.reject { |key, value| %w[name type].include?(key) }
116
- @logger.debug('[event] -- publishing event to amqp exchange -- ' + [exchange, event.client.name, event.check.name].join(' -- '))
106
+ @logger.debug('[event] -- publishing event to rabbitmq exchange -- ' + [exchange, event.client.name, event.check.name].join(' -- '))
117
107
  payload = details.send_only_check_output ? event.check.output : event.to_json
118
108
  @amq.method(exchange_type).call(exchange, exchange_options).publish(payload)
119
109
  end
@@ -134,7 +124,7 @@ module Sensu
134
124
  :check => check,
135
125
  :occurrences => 1
136
126
  })
137
- if check.type == 'metric'
127
+ if check['type'] == 'metric'
138
128
  handle_event(event)
139
129
  else
140
130
  history_key = 'history:' + client.name + ':' + check.name
@@ -214,7 +204,7 @@ module Sensu
214
204
  @result_queue = @amq.queue('results')
215
205
  @result_queue.subscribe do |result_json|
216
206
  result = Hashie::Mash.new(JSON.parse(result_json))
217
- @logger.info('[result] -- received result -- ' + [result.check.name, result.client, result.check.status, result.check.output].join(' -- '))
207
+ @logger.info('[result] -- received result -- ' + [result.client, result.check.name, result.check.status, result.check.output].join(' -- '))
218
208
  process_result(result)
219
209
  end
220
210
  end
@@ -225,7 +215,7 @@ module Sensu
225
215
  @settings.checks.each_with_index do |(name, details), index|
226
216
  check_request = Hashie::Mash.new({:name => name})
227
217
  unless details.publish == false
228
- EM.add_timer(stagger*index) do
218
+ @timers << EM::Timer.new(stagger*index) do
229
219
  details.subscribers.each do |target|
230
220
  if target.is_a?(Hash)
231
221
  @logger.debug('[publisher] -- check requires matching -- ' + target.to_hash.to_s + ' -- ' + name)
@@ -235,12 +225,10 @@ module Sensu
235
225
  exchange = target
236
226
  end
237
227
  interval = options[:test] ? 0.5 : details.interval
238
- EM.add_periodic_timer(interval) do
239
- unless stopping?
240
- check_request.issued = Time.now.to_i
241
- @logger.info('[publisher] -- publishing check -- ' + name + ' -- ' + exchange)
242
- @amq.fanout(exchange).publish(check_request.to_json)
243
- end
228
+ @timers << EM::PeriodicTimer.new(interval) do
229
+ check_request.issued = Time.now.to_i
230
+ @logger.info('[publisher] -- publishing check request -- ' + name + ' -- ' + exchange)
231
+ @amq.fanout(exchange).publish(check_request.to_json)
244
232
  end
245
233
  end
246
234
  end
@@ -250,37 +238,35 @@ module Sensu
250
238
 
251
239
  def setup_keepalive_monitor
252
240
  @logger.debug('[keepalive] -- setup keepalive monitor')
253
- EM.add_periodic_timer(30) do
254
- unless stopping?
255
- @logger.debug('[keepalive] -- checking for stale clients')
256
- @redis.smembers('clients').callback do |clients|
257
- clients.each do |client_id|
258
- @redis.get('client:' + client_id).callback do |client_json|
259
- client = Hashie::Mash.new(JSON.parse(client_json))
260
- time_since_last_keepalive = Time.now.to_i - client.timestamp
261
- result = Hashie::Mash.new({
262
- :client => client.name,
263
- :check => {
264
- :name => 'keepalive',
265
- :issued => Time.now.to_i
266
- }
267
- })
268
- case
269
- when time_since_last_keepalive >= 180
270
- result.check.status = 2
271
- result.check.output = 'No keep-alive sent from host in over 180 seconds'
272
- @amq.queue('results').publish(result.to_json)
273
- when time_since_last_keepalive >= 120
274
- result.check.status = 1
275
- result.check.output = 'No keep-alive sent from host in over 120 seconds'
276
- @amq.queue('results').publish(result.to_json)
277
- else
278
- @redis.hexists('events:' + client_id, 'keepalive').callback do |exists|
279
- if exists
280
- result.check.status = 0
281
- result.check.output = 'Keep-alive sent from host'
282
- @amq.queue('results').publish(result.to_json)
283
- end
241
+ @timers << EM::PeriodicTimer.new(30) do
242
+ @logger.debug('[keepalive] -- checking for stale clients')
243
+ @redis.smembers('clients').callback do |clients|
244
+ clients.each do |client_id|
245
+ @redis.get('client:' + client_id).callback do |client_json|
246
+ client = Hashie::Mash.new(JSON.parse(client_json))
247
+ time_since_last_keepalive = Time.now.to_i - client.timestamp
248
+ result = Hashie::Mash.new({
249
+ :client => client.name,
250
+ :check => {
251
+ :name => 'keepalive',
252
+ :issued => Time.now.to_i
253
+ }
254
+ })
255
+ case
256
+ when time_since_last_keepalive >= 180
257
+ result.check.status = 2
258
+ result.check.output = 'No keep-alive sent from host in over 180 seconds'
259
+ @amq.queue('results').publish(result.to_json)
260
+ when time_since_last_keepalive >= 120
261
+ result.check.status = 1
262
+ result.check.output = 'No keep-alive sent from host in over 120 seconds'
263
+ @amq.queue('results').publish(result.to_json)
264
+ else
265
+ @redis.hexists('events:' + client_id, 'keepalive').callback do |exists|
266
+ if exists
267
+ result.check.status = 0
268
+ result.check.output = 'Keep-alive sent from host'
269
+ @amq.queue('results').publish(result.to_json)
284
270
  end
285
271
  end
286
272
  end
@@ -320,39 +306,44 @@ module Sensu
320
306
 
321
307
  def setup_master_monitor
322
308
  request_master_election
323
- EM.add_periodic_timer(20) do
324
- unless stopping?
325
- if @is_master
326
- timestamp = Time.now.to_i
327
- @redis.set('lock:master', timestamp).callback do
328
- @logger.debug('[master] -- updated master lock timestamp -- ' + timestamp.to_s)
329
- end
330
- else
331
- request_master_election
309
+ @timers << EM::PeriodicTimer.new(20) do
310
+ if @is_master
311
+ timestamp = Time.now.to_i
312
+ @redis.set('lock:master', timestamp).callback do
313
+ @logger.debug('[master] -- updated master lock timestamp -- ' + timestamp.to_s)
332
314
  end
315
+ else
316
+ request_master_election
333
317
  end
334
318
  end
335
319
  end
336
320
 
337
321
  def setup_queue_monitor
338
322
  @logger.debug('[monitor] -- setup queue monitor')
339
- EM.add_periodic_timer(5) do
340
- unless stopping?
341
- unless @keepalive_queue.subscribed?
342
- @logger.warn('[monitor] -- re-subscribing to rabbitmq queue -- keepalives')
343
- setup_keepalives
344
- end
345
- unless @result_queue.subscribed?
346
- @logger.warn('[monitor] -- re-subscribing to rabbitmq queue -- results')
347
- setup_results
348
- end
323
+ @timers << EM::PeriodicTimer.new(5) do
324
+ unless @keepalive_queue.subscribed?
325
+ @logger.warn('[monitor] -- re-subscribing to rabbitmq queue -- keepalives')
326
+ setup_keepalives
327
+ end
328
+ unless @result_queue.subscribed?
329
+ @logger.warn('[monitor] -- re-subscribing to rabbitmq queue -- results')
330
+ setup_results
349
331
  end
350
332
  end
351
333
  end
352
334
 
335
+ def stop_reactor
336
+ @logger.warn('[stop] -- stopping reactor')
337
+ EM::Timer.new(3) do
338
+ EM::stop_event_loop
339
+ end
340
+ end
341
+
353
342
  def stop(signal)
354
343
  @logger.warn('[stop] -- stopping sensu server -- ' + signal)
355
- @status = :stopping
344
+ @timers.each do |timer|
345
+ timer.cancel
346
+ end
356
347
  @keepalive_queue.unsubscribe do
357
348
  @logger.warn('[stop] -- unsubscribed from rabbitmq queue -- keepalives')
358
349
  @result_queue.unsubscribe do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu
3
3
  version: !ruby/object:Gem::Version
4
- hash: 59
4
+ hash: 57
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 0
10
- version: 0.9.0
9
+ - 1
10
+ version: 0.9.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Sean Porter
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-12-23 00:00:00 -08:00
19
+ date: 2011-12-27 00:00:00 -08:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -230,8 +230,8 @@ files:
230
230
  - lib/sensu/api.rb
231
231
  - lib/sensu/client.rb
232
232
  - lib/sensu/config.rb
233
- - lib/sensu/helpers/redis.rb
234
- - lib/sensu/helpers/ruby.rb
233
+ - lib/sensu/patches/redis.rb
234
+ - lib/sensu/patches/ruby.rb
235
235
  - lib/sensu/server.rb
236
236
  - lib/sensu.rb
237
237
  - sensu.gemspec