Skip to content

Instantly share code, notes, and snippets.

@canoedf
Created March 10, 2014 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save canoedf/9473676 to your computer and use it in GitHub Desktop.
Save canoedf/9473676 to your computer and use it in GitHub Desktop.
check_cron.json
{
"checks": {
"cron_check": {
"handlers": ["osticket","pagerduty","smarts"],
"command": "/etc/sensu/plugins/check-procs.rb -p cron -C 1 ",
"interval": 60,
"incident_key": "cron_check",
"subscribers": [ "webservers" ]
}
}
}
handler_smarts.json
{
"handlers": {
"smarts": {
"type": "pipe",
"command": "/etc/sensu/handlers/smarts.rb"
}
}
}
smarts.json
{
"smarts": {
"api_key": "CFA4DThisISaFAKEAPIkeyFB3B53",
"url": "http://FQDN/sensu/",
"Name": "Sensu",
"Host": "hostnamefield",
"Monitor": "monitorfield",
"Status": "statusfield",
"Event": "eventfield",
"subject": "Test API message",
"message": "This is a test of the SensuMon GRID",
"ip": "111.222.33.44"
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
</head>
<body>
<?php
# Run this on your webserver - this is the JSON payload receiver
# This will process incidents coming from Sensu storing in a table
$string = file_get_contents("php://input");
$result = (json_decode($string,true));
# Log some details in /tmp/phplogDB.log
$tracefile=fopen("/tmp/phplogDB.log","a");
# these fields get stored in the database
$time = $result["timestamp"];
$name = $result["name"];
$host = $result["host"];
$monitor = $result["monitor"];
$status = $result["status"];
$event = $result["event"];
$incident = $result["host"] ."_" . $result["monitor"];
# ----------------------------------------------------------------------------------------
# update the sensu table here
$dbhost = 'FQDN';
$dbuser = 'USER';
$dbpass = 'PASS';
$dbdata = 'DATABASE';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
echo fputs($tracefile,date(DATE_W3C) . " Failed to connect: " . $dbhost . "\n");
fclose($tracefile);
die('Could not connect: ' . $dbhost . "\n");
}
echo fputs($tracefile,date(DATE_W3C) . " Connected to " . $dbhost . "\n");
$sql="INSERT INTO sensu_alerts(Time, Name, Host, Monitor, Status, Event, Incident)" .
"VALUES('$time', '$name', '$host', '$monitor', '$status', '$event', '$incident')";
mysql_select_db($dbdata);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
echo fputs($tracefile,date(DATE_W3C) . " Failed to enter data successfully" . "\n");
fclose($tracefile);
mysql_close($conn);
die('Could not enter data: ' . "\n");
}
echo fputs($tracefile,date(DATE_W3C) . " Entered data successfully" . "\n");
mysql_close($conn);
# ----------------------------------------------------------------------------------------
fclose($tracefile);
?>
</body>
</html>
#!/usr/bin/php -q
<?php
# data is being sent from the Ruby Handler to smarts.php
# get the pieces array
$pieces = explode(",", $argv[1]);
# Use to debug if necessary
/*
#-------------------------------------------------------
print "this is from SMARTS.RB argv1 " . $argv[1] . "\n";
print "this is from api_test timestamp: " . $pieces[0] . "\n";
print "this is from api_test name: " . $pieces[1] . "\n";
print "this is from api_test host: " . $pieces[2] . "\n";
print "this is from api_test monitor: " . $pieces[3] . "\n";
print "this is from api_test status: " . $pieces[4] . "\n";
print "this is from api_test event: " . $pieces[5] . "\n";
print "this is from api_test ip: " . $pieces[6] . "\n";
print "this is from api_test api_key: " . $pieces[7] . "\n";
print "this is from api_test url: " . $pieces[8] . "\n";
#-------------------------------------------------------
*/
$data = array(
'timestamp' => $pieces[0],
'name' => $pieces[1],
'host' => $pieces[2],
'monitor' => $pieces[3],
'status' => $pieces[4],
'event' => $pieces[5],
'ip' => $pieces[6],
'api_key' => $pieces[7],
'url' => $pieces[8],
'attachments' => array()
);
$config = array(
'url'=>$pieces[8],
);
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, 'Sensu JSON');
curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
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 smarts.rb script for logging to sensu-server.log
if ($code != 200)
die($code);
print $code;
?>
#!/usr/bin/env ruby
#
# This handler will feed the JSON sender that updates the sensu database table
# 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 SMARTS < Sensu::Handler
def handle
begin
timeout(30) do
timestamp = (@event['check']['issued']).to_s
name = settings['smarts']['Name']
host = @event['client']['name']
monitor = @event['check']['name']
status = @event['check']['output']
event = (@event['check']['status']).to_s
ip = settings['smarts']['ip']
api_key = settings['smarts']['api_key']
url = settings['smarts']['url']
phpargs = timestamp + "," + name + "," + host + "," + monitor + "," + status + "," + event + "," + ip + "," + api_key + "," + url
response = case @event['action']
when 'create'
puts 'SMARTS -- create phase '
result = %x[php /opt/sensu/smarts.php "#{phpargs}"]
puts 'SMARTS result -- ' + result
when 'resolve'
puts 'SMARTS -- resolve phase '
result = %x[php /opt/sensu/smarts.php "#{phpargs}"]
puts 'SMARTS result -- ' + result
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment