Created
May 9, 2014 21:51
-
-
Save steveberryman/00803c8e271b561ff359 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' if RUBY_VERSION < '1.9.0' | |
require 'sensu-handler' | |
require 'hipchat' | |
require 'timeout' | |
module Sensu::Extension | |
class Hipchat < Handler # Sub-class the appropriate extension type | |
def event_name | |
@event['client']['name'] + '/' + @event['check']['name'] | |
end | |
# The post_init hook is called after the main event loop has started | |
# At this time EventMachine is available for interaction. | |
def post_init | |
end | |
# Must at a minimum define type and name. These are used by | |
# Sensu during extension initialization. | |
def definition | |
{ | |
type: 'extension', # Always. | |
name: 'hipchat', # Usually just class name lower case. | |
} | |
end | |
# Simple accessor for the extension's name. You could put a different | |
# value here that is slightly more descriptive, or you could simply | |
# refer to the definition name. | |
def name | |
definition[:name] | |
end | |
# A simple, brief description of the extension's function. | |
def description | |
'Hipchat extension. Because otherwise the sensu server will forking die.' | |
end | |
def send_hipchat(room, from, message, color, notify) | |
apiversion = @settings["hipchat"]["apiversion"] || 'v1' | |
apikey = @settings["hipchat"]["apikey"] | |
hipchatmsg = HipChat::Client.new(apikey, :api_version => apiversion) | |
begin | |
timeout(3) do | |
hipchatmsg[room].send(from, "#{message}.", :color => 'gray', :notify => false) | |
end | |
rescue Timeout::Error | |
"Timed out while attempting to message #{room} [#{message}]" | |
end | |
end | |
# run() is passed a copy of the event_data hash | |
# for more information, see links below. | |
def run(event_data) | |
event = Oj.load(event_data) | |
resolved = true if event['action'].eql?("resolve") | |
if resolved or [1,2,3].include?(@event['status']) | |
room = event['check']['hipchat_room'] || @settings["hipchat"]["room"] | |
from = event['check']['hipchat_from'] || @settings["hipchat"]["from"] || 'Sensu' | |
client = event[:client] | |
client_name = event['source'] || client[:name] | |
check = event[:check] | |
check_name = check[:name] | |
state = check[:status] | |
output = check[:notification] || check[:output] | |
status_msg = resolved ? "[RESOLVED]" : "[PROBLEM]" | |
if state == 0 | |
state_msg = 'OK' | |
color = 'green' | |
notify = true | |
elsif state == 1 | |
state_msg = 'WARNING' | |
color = 'yellow' | |
notify = true | |
elsif state == 2 | |
state_msg = 'CRITICAL' | |
color = 'red' | |
notify = true | |
else | |
state_msg = 'UNKNOWN' | |
color = 'gray' | |
notify = false | |
end | |
# If the playbook attribute exists and is a URL, "[<a href='url'>playbook</a>]" will be output. | |
# To control the link name, set the playbook value to the HTML output you would like. | |
if check['playbook'] | |
begin | |
uri = URI.parse(check['playbook']) | |
if %w( http https ).include?(uri.scheme) | |
playbook = "[<a href='#{@event['check']['playbook']}'>Playbook</a>]" | |
else | |
playbook = "Playbook: #{@event['check']['playbook']}" | |
end | |
rescue | |
playbook = "Playbook: #{@event['check']['playbook']}" | |
end | |
end | |
message = "#{status_msg} #{client_name}/#{check_name} - #{state_msg}: #{output} [#{playbook}]" | |
operation = proc { | |
send_hipchat(room, from, message, color, notify) | |
} | |
callback = proc {|result| | |
"Hipchat message: #{result}" | |
} | |
EM::defer(operation, callback) | |
yield 'Sent an event to Hipchat', 0 | |
end | |
end | |
# Called when Sensu begins to shutdown. | |
def stop | |
yield | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment