Last active
August 29, 2015 13:56
-
-
Save canoedf/9001274 to your computer and use it in GitHub Desktop.
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
/etc/sensu/handlers/osticket.rb | |
#!/usr/bin/env ruby | |
# | |
# This handler will create tickets in osTicket | |
# | |
# Note :- Adjust the API key and url in osTicket.json to match your osTicket API | |
# | |
# Author: Dan Fischer <noSPAM4me@yahoo.com> | |
# | |
# Released under the same terms as Sensu (the MIT license); see LICENSE | |
# for details. | |
require 'rubygems' if RUBY_VERSION < '1.9.0' | |
require 'sensu-handler' | |
require 'timeout' | |
class OSTicket < Sensu::Handler | |
def handle | |
description = @event['check']['notification'] | |
description ||= [@event['client']['name'], @event['check']['name'], @event['check']['output']].join(' : ') | |
# puts settings.inspect | |
timeout(30) do | |
timestamp = (@event['check']['issued']).to_s | |
api_key = settings['osticket']['api_key'] | |
url = settings['osticket']['url'] | |
name = settings['osticket']['name'] | |
email = settings['osticket']['email'] | |
subject = @event['check']['name'] | |
message = timestamp + " " + @event['check']['command'] | |
ip = settings['osticket']['ip'] | |
# | |
# The next line builds the "payload" for PHP | |
phpargs = name + "," + email + ","+ subject + "," + message + "," + ip + "," + api_key + "," + url | |
response = case @event['action'] | |
when 'create' | |
puts 'osTicket -- create phase ' | |
result = %x[php /opt/sensu/api_ticket_create.php "#{phpargs}"] | |
puts 'osTicket RESULT FROM PHP CALL -- ' + result | |
when 'resolve' | |
# The osTicket API does not support resolves/updates yet | |
puts 'osTicket -- resolve phase ' | |
end | |
end | |
end | |
end | |
/opt/sensu/api_ticket_create.php | |
#!/usr/bin/php -q | |
<?php | |
# data is being sent from the Ruby Handler to api_ticket_create.php | |
# get the pieces array | |
$pieces = explode(",", $argv[1]); | |
/* | |
# Use to debug if necessary | |
#------------------------------------------------------- | |
print "this is from api argv1 " . $argv[1] . "\n"; | |
print "this is from api name: " . $pieces[0] . "\n"; | |
print "this is from api email: " . $pieces[1] . "\n"; | |
print "this is from api subject: " . $pieces[2] . "\n"; | |
print "this is from api message: " . $pieces[3] . "\n"; | |
print "this is from api ip: " . $pieces[4] . "\n"; | |
print "this is from api api_key: " . $pieces[5] . "\n"; | |
print "this is from api url: " . $pieces[6] . "\n"; | |
#------------------------------------------------------- | |
*/ | |
$data = array( | |
'name' => $pieces[0], | |
'email' => $pieces[1], | |
'subject' => $pieces[2], | |
'message' => $pieces[3], | |
'ip' => $pieces[4], | |
'attachments' => array() | |
); | |
$config = array( | |
'url'=>$pieces[6], | |
'key'=>$pieces[5] | |
); | |
/* | |
* If you have a proper PHP installation there is no need | |
* to check for these two functions so I remark them | |
#pre-checks | |
function_exists('curl_version') or die('CURL support required'); | |
function_exists('json_encode') or die('JSON support required'); | |
*/ | |
set_time_limit(30); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $config['url']); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.8'); | |
curl_setopt($ch, CURLOPT_VERBOSE, FALSE); | |
curl_setopt($ch, CURLOPT_HEADER, FALSE); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$config['key'])); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
$result=curl_exec($ch); | |
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
# $result is sent back to the osticket.rb script | |
if ($code != 201) | |
die('Unable to create ticket: '.$result); | |
$ticket_id = (int) $result; | |
print $ticket_id; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment