-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflapjack.rb
84 lines (68 loc) · 2.1 KB
/
flapjack.rb
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
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env ruby
require 'json'
require 'optparse'
require 'redis'
require 'flapjack'
require 'flapjack/configuration'
require 'flapjack/data/event'
options = {
host: '127.0.0.1',
port: 6380,
channel: 'events',
db: 0
}
optparse = OptionParser.new do |opts|
opts.banner = <<eos
Usage: flapjack.rb [-h host] [-p port] [-d database] [-c channel]
Relay handler will immediately read Sensu Event Handler JSON and connect
to default server unless overridden by command line options.
eos
opts.on("-h", "--host redisHost", "Redis Host") do |h|
options[:host] = h
end
opts.on("-p", "--port redisPort", "Redis Port") do |p|
options[:port] = p.to_i
end
opts.on("-d", "--database redisDatabase", "Redis Database") do |d|
options[:db] = d.to_i
end
opts.on("-c", "--channel redisChannel", "Redis Channel") do |c|
options[:channel] = c
end
end
optparse.parse(ARGV)
if options[:host].nil? || options[:host] == ''
puts "FATAL: You must specify a redis host (-h)."
puts optparse.help
exit
end
jsonInput = $stdin.read
event = JSON.parse(jsonInput)
SEVERITIES = ['ok', 'warning', 'critical', 'unknown']
client = event['client']
check = event['check']
tags = []
tags.concat(client['tags']) if client['tags'].is_a?(Array)
tags.concat(check['tags']) if check['tags'].is_a?(Array)
tags << client['environment'] unless client['environment'].nil?
unless check['subscribers'].nil? || check['subscribers'].empty? # rubocop:disable UnlessElse
tags.concat(client['subscriptions'] - (client['subscriptions'] - check['subscribers']))
else
tags.concat(client['subscriptions'])
end
details = ['Address:' + client['address']]
details << 'Tags:' + tags.join(',')
details << "Raw Output: #{check[:output]}" if check[:notification]
flapjack_event = {
entity: client['name'],
check: check['name'],
type: 'service',
state: SEVERITIES[check['status']] || 'unknown',
summary: check['notification'] || check['output'],
details: details.join(' '),
time: check['executed'],
tags: tags
}
Flapjack::RedisProxy.config = options
Flapjack::Data::Event.push(options[:channel], flapjack_event)
Flapjack.redis.quit