Skip to content

Instantly share code, notes, and snippets.

@Buzer
Created February 13, 2016 15:04
Show Gist options
  • Save Buzer/262da357c24907a1d05d to your computer and use it in GitHub Desktop.
Save Buzer/262da357c24907a1d05d to your computer and use it in GitHub Desktop.
Sensu bridge extension to Flapjack
# Sends events to Flapjack for notification routing. See http://flapjack.io/
#
# This extension requires Flapjack >= 0.8.7 and Sensu >= 0.13.1
#
# In order for Flapjack to keep its entities up to date, it is necssary to set
# metric to "true" for each check that is using the flapjack handler extension.
#
# Here is an example of what the Sensu configuration for flapjack should
# look like, assuming your Flapjack's redis service is running on the
# same machine as the Sensu server:
#
# {
# "flapjack": {
# "host": "localhost",
# "port": 6379,
# "db": "0"
# }
# }
#
# Copyright 2014 Jive Software and contributors.
#
# Released under the same terms as Sensu (the MIT license); see LICENSE for details.
require 'sensu/redis'
module Sensu
module Extension
class Flapjack < Bridge
def name
'flapjack'
end
def description
'sends sensu events to the flapjack redis queue'
end
def options
return @options if @options
@options = {
host: '127.0.0.1',
port: 6379,
channel: 'events',
db: 0,
default_send: true,
default_send_metric: true,
default_send_check: true,
flapjack_v2: false
}
if @settings[:flapjack].is_a?(Hash)
@options.merge!(@settings[:flapjack])
end
@options
end
def definition
{
type: 'extension',
name: name,
mutator: 'ruby_hash'
}
end
def post_init
@redis = Sensu::Redis.connect(options)
@redis.on_error do |_error|
@logger.warn('Flapjack Redis instance not available on ' + options[:host])
end
end
def run(event)
client = event[:client]
check = event[:check]
# Merge order:
# @options[:default_send] < @options[:default_send_[:type]] < check[:flapjack]
send_event = @options[:default_send]
if check.has_key? :type
if check[:type] == 'metric'
send_event = @options[:default_send_metric]
elsif check[:type] == 'check'
send_event = @options[:default_send_check]
end
end
if check.has_key? :flapjack
send_event = check[:flapjack]
end
if not send_event
yield 'did not send event to flapjack', 0
return
end
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?
# #YELLOW
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: Sensu::SEVERITIES[check[:status]] || 'unknown',
summary: check[:notification] || check[:output],
details: details.join(' '),
time: check[:executed],
tags: tags
}
@redis.lpush(options[:channel], MultiJson.dump(flapjack_event))
if @options[:flapjack_v2]
@redis.lpush("events_actions", "+")
end
yield 'sent an event to the flapjack redis queue', 0
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment