forked from sensu-plugins/sensu-plugins-zendesk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler-zendesk.rb
executable file
·88 lines (80 loc) · 2.43 KB
/
handler-zendesk.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
#!/usr/bin/env ruby
#
# Sensu Zendesk Handler
#
# DESCRIPTION:
# Handler to automatic create new tickets in Zendesk for alarms.
#
# subscriptions_to_tags - transforms your subscriptions in tags
# status_to_use - determine if Critical (2), Warning (1) or Unknown (3) alerts will create tickets
#
# OUTPUT:
# Create a new Zendesk ticket
#
# PLATFORMS:
# all
#
# DEPENDENCIES:
# gem zendesk_api
#
# 2014, Diogo Gomes <[email protected]> @_diogo
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
#
require 'sensu-handler'
require 'zendesk_api'
class Zendesk < Sensu::Handler
def ticket_description
"Sensu Alert\r\n" \
'Client: ' + @event['client']['name'] + "\r\n" \
'Address: ' + @event['client']['address'] + "\r\n" \
'Subscriptions: ' + @event['client']['subscriptions'].join(', ') + "\r\n" \
'Check: ' + @event['check']['name'] + "\r\n" \
'Output: ' + @event['check']['output'] + "\r\n"
end
def ticket_tags
tags = []
unless settings['zendesk']['tags'].nil?
tags << settings['zendesk']['tags']
end
if settings['zendesk']['subscriptions_to_tags']
tags << @event['client']['subscriptions']
end
tags
end
def ticket_subject
'Alert - ' + @event['client']['name'] + ' - ' + @event['check']['name']
end
def handle
client = ZendeskAPI::Client.new do |config|
config.url = settings['zendesk']['url']
# Basic / Token Authentication
config.username = settings['zendesk']['username']
# Choose one of the following depending on your authentication choice
if settings['zendesk']['token'].nil?
config.password = settings['zendesk']['password']
else
config.token = settings['zendesk']['token']
end
config.retry = true
end
begin
Timeout.timeout(60) do
if settings['zendesk']['status_to_use'].include?(@event['check']['status'])
ZendeskAPI::Ticket.create(
client,
subject: ticket_subject,
comment: { value: ticket_description },
submitter_id: client.current_user.id,
priority: settings['zendesk']['priority'] || 'urgent',
type: settings['zendesk']['type'] || 'incident',
tags: ticket_tags
)
end
end
rescue Timeout::Error
puts "zendesk -- timed out while attempting to create a ticket for #{ticket_subject} -"
end
end
end