forked from sensu/sensu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sa-monitoring-server
executable file
·73 lines (55 loc) · 1.41 KB
/
sa-monitoring-server
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
#!/usr/bin/env ruby
require 'sa-monitoring/config'
require 'uuidtools'
require 'em-redis'
#
# Connect to RabbitMQ
#
AMQP.start(:host => CONFIG['rabbitmq']['server']) do
redis = EM::Protocols::Redis.connect(:host => CONFIG['redis']['server'])
amq = MQ.new
#
# Publish critical/warning check results
#
amq.queue('results').subscribe do |msg|
puts msg
end
#
# Send checks out to subscribed clients
#
exchanges = Hash.new
amq.queue('checks').subscribe do |check|
check = JSON.parse(check)
check_id = UUIDTools::UUID.random_create.to_s
redis.set(check_id, check['name'])
check_msg = {
:name => check['name'],
:id => check_id
}.to_json
check['subscribers'].each do |exchange|
if exchanges[exchange].nil?
exchanges[exchange] = amq.fanout(exchange)
end
exchanges[exchange].publish(check_msg)
end
end
#
# Populate the work queue with checks defined in the JSON config file
#
work = amq.direct('')
CONFIG['checks'].each do |name, info|
work.publish({'name' => name, 'subscribers' => info['subscribers']}.to_json, :routing_key => 'checks')
end
#
# Accept client keep-alives
#
class OhaiServer < EM::Connection
attr_accessor :redis
def receive_data(data)
@redis.set("client1", data)
end
end
EM::start_server('0.0.0.0', 9000, OhaiServer) do |ohaiserver|
ohaiserver.redis = redis
end
end