Skip to content
This repository has been archived by the owner on Jul 11, 2020. It is now read-only.

Commit

Permalink
Send sensu notifications to slack
Browse files Browse the repository at this point in the history
Sam Kottler committed Jun 19, 2014
1 parent eed20b3 commit ecdf5cc
Showing 6 changed files with 153 additions and 94 deletions.
6 changes: 0 additions & 6 deletions cookbooks/rubygems-sensu/files/default/irc.json

This file was deleted.

67 changes: 0 additions & 67 deletions cookbooks/rubygems-sensu/files/default/irc.rb

This file was deleted.

118 changes: 118 additions & 0 deletions cookbooks/rubygems-sensu/files/default/slack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/usr/bin/env ruby

# Copyright 2014 Dan Shultz and contributors.
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
#
# In order to use this plugin, you must first configure an incoming webhook
# integration in slack. You can create the required webhook by visiting
# https://{your team}.slack.com/services/new/incoming-webhook
#
# After you configure your webhook, you'll need to token from the integration.
# The default channel and bot name entered can be overridden by this handlers
# configuration.
#
# Minimum configuration required is the 'token' and 'team_name'

require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-handler'
require 'json'

class Slack < Sensu::Handler

def slack_token
get_setting('token')
end

def slack_channel
get_setting('channel')
end

def slack_message_prefix
get_setting('message_prefix')
end

def slack_team_name
get_setting('team_name')
end

def slack_bot_name
get_setting('bot_name')
end

def incident_key
@event['client']['name'] + '/' + @event['check']['name']
end

def get_setting(name)
settings["slack"][name]
end

def handle
description = @event['notification'] || build_description
post_data("#{incident_key}: #{description}")
end

def build_description
[
@event['client']['name'],
@event['check']['name'],
@event['check']['output'],
@event['client']['address'],
@event['client']['subscriptions'].join(',')
].join(' : ')
end

def post_data(notice)
uri = slack_uri(slack_token)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

req = Net::HTTP::Post.new("#{uri.path}?#{uri.query}")
req.body = "payload=#{payload(notice).to_json}"

response = http.request(req)
verify_response(response)
end

def verify_response(response)
case response
when Net::HTTPSuccess
true
else
raise response.error!
end
end

def payload(notice)
{
:link_names => 1,
:text => [slack_message_prefix, notice].compact.join(' '),
:icon_emoji => icon_emoji
}.tap do |payload|
payload[:channel] = slack_channel if slack_channel
payload[:username] = slack_bot_name if slack_bot_name
end
end

def icon_emoji
default = ":feelsgood:"
emoji = {
0 => ':godmode:',
1 => ':hurtrealbad:',
2 => ':feelsgood:'
}
emoji.fetch(check_status.to_i, default)
end

def check_status
@event['check']['status']
end

def slack_uri(token)
url = "https://#{slack_team_name}.slack.com/services/hooks/incoming-webhook?token=#{token}"
URI(url)
end

end
23 changes: 2 additions & 21 deletions cookbooks/rubygems-sensu/recipes/server.rb
Original file line number Diff line number Diff line change
@@ -12,27 +12,8 @@
node.default['sensu']['dashboard']['user'] = sensu_creds['user']
node.default['sensu']['dashboard']['password'] = sensu_creds['password']

%w{ irc }.each do |handler|
sensu_handler handler do
type 'pipe'
command 'irc.rb'
severities ['critical']
end

cookbook_file "/etc/sensu/handlers/#{handler}.rb" do
path "/etc/sensu/handlers/#{handler}.rb"
source "#{handler}.rb"
action :create
end

cookbook_file "/etc/sensu/#{handler}.json" do
path "/etc/sensu/#{handler}.json"
source "#{handler}.json"
action :create
end
end

include_recipe "rubygems-sensu::librato"
include_recipe 'rubygems-sensu::librato'
include_recipe 'rubygems-sensu::slack'

include_recipe 'sensu::rabbitmq'
include_recipe 'sensu::redis'
26 changes: 26 additions & 0 deletions cookbooks/rubygems-sensu/recipes/slack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# These have to be different than the handler credentials because Slack
# provides a separate token for each integration.
slack_creds = ChefVault::Item.load('sensu', 'credentials')['slack']

template '/etc/sensu/slack.json' do
source 'slack.json.erb'
path '/etc/sensu/slack.json'
owner 'sensu'
group 'sensu'
variables(
token: slack_creds['token'],
team_name: slack_creds['team_name']
)
end

cookbook_file '/etc/sensu/handlers/sensu.rb' do
path '/etc/sensu/handlers/sensu.rb'
source 'sensu.rb'
action :create
end

sensu_handler 'slack' do
type 'pipe'
command 'slack.rb'
severities ['ok', 'critical']
end
7 changes: 7 additions & 0 deletions cookbooks/rubygems-sensu/templates/default/slack.json.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"slack": {
"token": "<%= @token %>",
"team_name": "<%= @team_name %>",
"channel": "#sensu",
}
}

0 comments on commit ecdf5cc

Please sign in to comment.