|
| 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