Skip to content

Commit b43b64c

Browse files
author
Brian Czapiga
committed
initial commit
0 parents  commit b43b64c

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
gem 'redis'
3+
gem 'oj'
4+
gem 'json'

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Sensu-Flapjack Pipe Handler
2+
3+
## Usage
4+
5+
```
6+
{
7+
"handlers": {
8+
"flapjack": {
9+
"type": "pipe",
10+
# "command": "/etc/sensu/handlers/flapjack.rb -h 127.0.0.1 -p 6380 -c events -d 0"
11+
"command": "/etc/sensu/handlers/flapjack.rb"
12+
}
13+
}
14+
}
15+
```
16+
17+
Note that a check must be either defined as type _metric_ or in a state other than OK in order to be passed to a standard file (pipe) handler.
18+
19+
## Sources
20+
21+
Uses source and fragments from:
22+
23+
- https://github.com/sensu/sensu
24+
- https://github.com/sensu/sensu-community-plugins/commits/e58c1f3ce39c6c0ca271fadd470c04362e92b190/extensions/handlers/flapjack.rb
25+
- https://github.com/flapjack/flapjack
26+
27+
## License
28+
29+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
30+
31+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
32+
33+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

flapjack.rb

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'json'
4+
require 'optparse'
5+
require 'redis'
6+
require 'oj'
7+
8+
options = {
9+
host: '127.0.0.1',
10+
port: 6380,
11+
channel: 'events',
12+
db: 0
13+
}
14+
15+
optparse = OptionParser.new do |opts|
16+
opts.banner = <<eos
17+
Usage: flapjack.rb [-h host] [-p port] [-d database] [-c channel]
18+
19+
Relay handler will immediately read Sensu Event Handler JSON and connect
20+
to default server unless overridden by command line options.
21+
22+
eos
23+
24+
opts.on("-h", "--host redisHost", "Redis Host") do |h|
25+
options[:host] = h
26+
end
27+
28+
opts.on("-p", "--port redisPort", "Redis Port") do |p|
29+
options[:port] = p.to_i
30+
end
31+
32+
opts.on("-d", "--database redisDatabase", "Redis Database") do |d|
33+
options[:db] = d.to_i
34+
end
35+
36+
opts.on("-c", "--channel redisChannel", "Redis Channel") do |c|
37+
options[:channel] = c
38+
end
39+
end
40+
41+
optparse.parse(ARGV)
42+
43+
if options[:host].nil? || options[:host] == ''
44+
puts "FATAL: You must specify a redis host (-h)."
45+
puts optparse.help
46+
exit
47+
end
48+
49+
jsonInput = $stdin.read
50+
event = JSON.parse(jsonInput)
51+
52+
redis = Redis.new(options)
53+
54+
SEVERITIES = ['ok', 'warning', 'critical', 'unknown']
55+
client = event['client']
56+
check = event['check']
57+
tags = []
58+
59+
tags.concat(client['tags']) if client['tags'].is_a?(Array)
60+
tags.concat(check['tags']) if check['tags'].is_a?(Array)
61+
tags << client['environment'] unless client['environment'].nil?
62+
unless check['subscribers'].nil? || check['subscribers'].empty? # rubocop:disable UnlessElse
63+
tags.concat(client['subscriptions'] - (client['subscriptions'] - check['subscribers']))
64+
else
65+
tags.concat(client['subscriptions'])
66+
end
67+
details = ['Address:' + client['address']]
68+
details << 'Tags:' + tags.join(',')
69+
details << "Raw Output: #{check[:output]}" if check[:notification]
70+
71+
flapjack_event = {
72+
entity: client['name'],
73+
check: check['name'],
74+
type: 'service',
75+
state: SEVERITIES[check['status']] || 'unknown',
76+
summary: check['notification'] || check['output'],
77+
details: details.join(' '),
78+
time: check['executed'],
79+
tags: tags
80+
}
81+
82+
redis.lpush(options[:channel], Oj.dump(flapjack_event, :mode => :compat, :time_format => :ruby, :indent => 0))

0 commit comments

Comments
 (0)