forked from sensu/sensu-community-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sensu#206 from deepakmdass88/master
Added RT Handler which will create Tickets for the events in Request Tracker
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |