Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Modification of the sensu-plugins-hipchat handler that sends the Uchiwa URL and silences notifications -- written as an ERB template for Puppet
#!/opt/sensu/embedded/bin/ruby
#
# Original: https://github.com/sensu-plugins/sensu-plugins-hipchat/blob/master/bin/handler-hipchat.rb
# Modified by: https://github.com/elocnatsirt
# Sensu Handler: hipchat
#
# This handler script is used to send notifications to Hipchat rooms.
#
# Input:
# @event - Event attributes.
# @event['action'] - Property to figure out the event type i.e. whether it is create or resolve.
# @event['check'] - Map of attributes from the check config which is calling this handler
# @event['client'] - Map of attributes from the client config for the clients from which this event is generated.
# option: json_config - By default, assumes the hipchat config parameters are in the "hipchat" top-level json key.
# This command line option allows to specify a custom json key.
#
# Output:
# Green coloured notification on the Hipchat room if a resolve event is seen.
# Yellow coloured notification used to notify warning if a create event is seen with a status of 1
# Red coloured notification used to notify critical if a create event is seen with a status other than 1
#
# Note: The handler config is fetched and merged from all json config files. The "hipchat" json key is used by default which can
# be overridden with the "json_config" command line option. The hipchat room could also be configured on a per client basis
# by defining the "hipchat_room" attribute in the client config file. This will override the default hipchat room where the
# alerts are being routed to for that particular client.
require 'sensu-handler'
require 'hipchat'
require 'timeout'
class HipChatNotif < Sensu::Handler
option :json_config,
description: 'JSON config key name',
short: '-j JsonKeyName',
long: '--json_config JsonKeyName',
required: false,
default: 'hipchat'
def event_name
@event['client']['name'] + '/' + @event['check']['name']
end
def check_url
'http://<%= @master_address %>:3000/#/client/sensu/' + @event['client']['name'] + '?check=' + @event['check']['name']
end
def handle
json_config = config[:json_config]
server_url = settings[json_config]['server_url'] || 'https://api.hipchat.com'
apiversion = settings[json_config]['apiversion'] || 'v1'
proxy_url = settings[json_config]['proxy_url']
hipchatmsg = HipChat::Client.new(settings[json_config]['apikey'], api_version: apiversion, http_proxy: proxy_url, server_url: server_url)
room = @event['client']['hipchat_room'] || @event['check']['hipchat_room'] || settings[json_config]['room']
from = settings[json_config]['from'] || 'Sensu'
message = @event['check']['notification'] || @event['check']['output']
# 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 @event['check']['playbook']
begin
uri = URI.parse(@event['check']['playbook'])
message << if %w( http https ).include?(uri.scheme)
" [<a href='#{@event['check']['playbook']}'>Playbook</a>]"
else
" Playbook: #{@event['check']['playbook']}"
end
rescue
message << " Playbook: #{@event['check']['playbook']}"
end
end
begin
timeout(3) do
if @event['action'].eql?('resolve')
hipchatmsg[room].send(from, "RESOLVED - [#{event_name}] - #{message} [<a href='#{check_url}'>View on Uchiwa</a>]", color: 'green')
else
hipchatmsg[room].send(from, "ALERT - [#{event_name}] - #{message} [<a href='#{check_url}'>View on Uchiwa</a>]", color: @event['check']['status'] == 1 ? 'yellow' : 'red', notify: false)
end
end
rescue Timeout::Error
puts "hipchat -- timed out while attempting to message #{room}"
end
end
end
@elocnatsirt

This comment has been minimized.

Copy link
Owner Author

@elocnatsirt elocnatsirt commented Sep 17, 2016

I modified this handler because I wanted to be able to click on the Hipchat notification and get taken straight to the alerting check on the Sensu dashboard. Since we have a lot of hosts and anything that is critical alerts us via PagerDuty, I did not want every single alert on Hipchat to show up as a message, and silenced them all by default. There is one variable in this template, and that is the URL to your Uchiwa dashboard (<%= @master_address %>). It expects port 3000 by default, but you can manually change these values to be hard coded and change the extension if you just want to place with file without Puppet.

This could easily be updated to accept the Uchiwa URL and port in the json_config, as well as a toggle option for silenced alerts.

@giorgian

This comment has been minimized.

Copy link

@giorgian giorgian commented Dec 2, 2016

This only works if Uchiwa is configured with one server (that must be called 'sensu'), doesn't it?

I'm struggling to understand how to find out the right url if there is more than a server, without luck.

@elocnatsirt

This comment has been minimized.

Copy link
Owner Author

@elocnatsirt elocnatsirt commented Dec 8, 2016

@giorgian If you mean configured with one data center, then possibly; I have not tested that. We have two Uchiwa dashboards/Sensu masters so there is only one datacenter per host. We feed in the correct dashboard url with Puppet/Hiera.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment