Skip to content

Commit

Permalink
Merge pull request sensu#206 from deepakmdass88/master
Browse files Browse the repository at this point in the history
Added RT Handler which will create Tickets for the events in Request Tracker
  • Loading branch information
nstielau committed Jun 5, 2013
2 parents a95c789 + 035e5e0 commit 008a93b
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions handlers/notification/rt.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env ruby
# Handler RT
# ===
#
# This is a simple Handler script for Sensu which will create
# tickets in Request Tracker for each event
#
# Note :- Replace user, pass, requestor, server, queue variables
# to suit to your RT
#
# Author Deepak Mohan Das <[email protected]>
#
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.


require 'sensu-handler'
require 'net/http'
require 'timeout'

class RT < Sensu::Handler
def short_name
@event['client']['name'] + '/' + @event['check']['name']
end

def action_to_string
@event['action'].eql?('resolve') ? "RESOLVED" : "ALERT"
end

def handle
user = 'rt_user'
pass = 'rt_pass'
requestor = '[email protected]'
server = 'http://localhost/'
queue = 'queue'
uri = URI("#{server}/REST/1.0/ticket/new")
stat = "#{@event['check']['output']}".chomp
body = <<-BODY.gsub(/^ {14}/, '')
#{stat}
Host: #{@event['client']['name']}
Timestamp: #{Time.at(@event['check']['issued'])}
Address: #{@event['client']['address']}
Check Name: #{@event['check']['name']}
Command: #{@event['check']['command']}
Status: #{@event['check']['status']}
Occurrences: #{@event['occurrences']}
BODY
subject = "#{action_to_string} - #{short_name}: #{@event['check']['notification']}"
content = "id: ticket/new\nRequestor: #{requestor}\nSubject: #{subject}\nStatus: new\nText: #{body} ticket\nQueue: #{queue}"
begin
timeout 10 do
puts "Connecting to Request tracker"
response = Net::HTTP.post_form(uri, {'user' => user, 'pass' => pass, 'content' => content})
puts "Response - #{response}"
end
rescue Timeout::Error
puts "CRITICAL --- Timed out while attempting to create ticket in RT"
rescue Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
puts "Critical --- HTTP Connection error #{e.message}"
end
end
end

0 comments on commit 008a93b

Please sign in to comment.