-
Notifications
You must be signed in to change notification settings - Fork 54
/
handler-slack.rb
executable file
·248 lines (213 loc) · 6.32 KB
/
handler-slack.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/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 the webhook URL from the integration.
require 'sensu-handler'
require 'json'
require 'erubis'
class Slack < Sensu::Handler
option :json_config,
description: 'Configuration name',
short: '-j JSONCONFIG',
long: '--json JSONCONFIG',
default: 'slack'
def payload_template
get_setting('payload_template')
end
def slack_webhook_url
get_setting('webhook_url')
end
def slack_icon_emoji
get_setting('icon_emoji')
end
def slack_icon_url
get_setting('icon_url')
end
def slack_channel
@event['client']['slack_channel'] || @event['check']['slack_channel'] || get_setting('channel')
end
def slack_message_prefix
get_setting('message_prefix')
end
def slack_bot_name
get_setting('bot_name')
end
def slack_surround
get_setting('surround')
end
def slack_link_names
get_setting('link_names')
end
def message_template
get_setting('template') || get_setting('message_template')
end
def fields
get_setting('fields')
end
def proxy_address
get_setting('proxy_address')
end
def proxy_port
get_setting('proxy_port')
end
def proxy_username
get_setting('proxy_username')
end
def proxy_password
get_setting('proxy_password')
end
def dashboard_uri
get_setting('dashboard')
end
def incident_key
if dashboard_uri.nil?
@event['client']['name'] + '/' + @event['check']['name']
else
"<#{dashboard_uri}#{@event['client']['name']}?check=#{@event['check']['name']}|#{@event['client']['name']}/#{@event['check']['name']}>"
end
end
def get_setting(name)
settings[config[:json_config]][name]
rescue TypeError, NoMethodError => e
puts "settings: #{settings}"
puts "slack key: #{config[:json_config]}. This should not be a file name/path."
puts <<-EOS
key name: #{name}. This is the key in config that broke.
Check the slack key to make sure it's parent key exists"
EOS
puts "error: #{e.message}"
exit 3 # unknown
end
def handle
if payload_template.nil?
description = @event['check']['notification'] || build_description
post_data("#{incident_key}: #{description}")
else
post_data(render_payload_template(slack_channel))
end
end
def render_payload_template(channel)
return unless payload_template && File.readable?(payload_template)
template = File.read(payload_template)
eruby = Erubis::Eruby.new(template)
eruby.result(binding)
end
def build_description
template = if message_template && File.readable?(message_template)
File.read(message_template)
else
'''<%=
[
@event["check"]["output"].gsub(\'"\', \'\\"\'),
@event["client"]["address"],
@event["client"]["subscriptions"].join(",")
].join(" : ")
%>
'''
end
eruby = Erubis::Eruby.new(template)
eruby.result(binding)
end
def post_data(body)
uri = URI(slack_webhook_url)
http = if proxy_address.nil?
Net::HTTP.new(uri.host, uri.port)
else
Net::HTTP::Proxy(proxy_address, proxy_port, proxy_username, proxy_password).new(uri.host, uri.port)
end
http.use_ssl = true
req = Net::HTTP::Post.new("#{uri.path}?#{uri.query}", 'Content-Type' => 'application/json')
if payload_template.nil?
text = slack_surround ? slack_surround + body + slack_surround : body
req.body = payload(text).to_json
else
req.body = body
end
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)
client_fields = []
unless fields.nil?
fields.each do |field|
# arbritary based on what I feel like
# -vjanelle
is_short = true unless @event['client'].key?(field) && @event['client'][field].length > 50
client_fields << {
title: field,
value: @event['client'][field],
short: is_short
}
end
end
{
icon_url: slack_icon_url ? slack_icon_url : 'https://raw.githubusercontent.com/sensu/sensu-logo/master/sensu1_flat%20white%20bg_png.png',
attachments: [{
title: "#{@event['client']['address']} - #{translate_status}",
text: [slack_message_prefix, notice].compact.join(' '),
color: color,
fields: client_fields
}]
}.tap do |payload|
payload[:channel] = slack_channel if slack_channel
payload[:username] = slack_bot_name if slack_bot_name
payload[:icon_emoji] = slack_icon_emoji if slack_icon_emoji
payload[:link_names] = slack_link_names if slack_link_names
end
end
def color
color = {
0 => '#36a64f',
1 => '#FFCC00',
2 => '#FF0000',
3 => '#6600CC'
}
begin
color.fetch(check_status.to_i)
# a script can return any error code it feels like we should not assume
# that it will always be 0,1,2,3 even if that is the sensu (nagions)
# specification. A couple common examples:
# 1. A sensu server schedules a check on the instance but the command
# executed does not exist in your `$PATH`. Shells will return a `127` status
# code.
# 2. Similarly a `126` is a permission denied or the command is not
# executable.
# Rather than adding every possible value we should just treat any non spec
# designated status code as `unknown`s.
rescue KeyError
color.fetch(3)
end
end
def check_status
@event['check']['status']
end
def translate_status
status = {
0 => :OK,
1 => :WARNING,
2 => :CRITICAL,
3 => :UNKNOWN
}
begin
status.fetch(check_status.to_i)
# handle any non standard check status as `unknown`
rescue KeyError
status.fetch(3)
end
end
end