Last active
August 29, 2015 13:57
-
-
Save canoedf/9467811 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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> | |
<html> | |
<head> | |
</head> | |
<body> | |
<?php | |
# This will process incidents coming from Icinga storing in a table | |
$string = file_get_contents("php://input"); | |
$result = (json_decode($string,true)); | |
# Log some details | |
$tracefile=fopen("/tmp/phpJSONlogDB.log","a"); | |
echo fputs($tracefile,"name: " . $result["name"] ."\n\n"); | |
# 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["incident"]; | |
# update the icinga table here | |
# ---------------------------- | |
$dbhost = 'FQDN'; | |
$dbuser = 'USER'; | |
$dbpass = 'PASS'; | |
$dbbase = '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 icinga_alerts(Time, Name, Host, Monitor, Status, Event, Incident)" . | |
"VALUES('$time', '$name', '$host', '$monitor', '$status', '$event', '$incident')"; | |
mysql_select_db($dbbase); | |
$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> | |
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
################################################ | |
#!/bin/sh | |
# send_icinga_alerts | |
#/usr/local/bin/send_icinga_alerts.sh | |
# | |
# Icinga calls the script with an argument list from the configured command: | |
#define command{ | |
# command_name send_icinga_alerts | |
# command_line /usr/local/bin/send_icinga_alerts.sh '$SHORTDATETIME$' '$HOSTALIAS$' '$SERVICECHECKCOMMAND$' '$SERVICEOUTPUT$' '$SERVICESTATE$' '$HOSTALIAS$/$SERVICEDESC$'} | |
# To avoid setting event handlers in your objects just set a GLOBAL HOST AND SERVICE EVENT HANDLER in icinga.cfg like this: | |
# global_service_event_handler=send_icinga_alerts | |
# ################################################ | |
# | |
TIMESTAMP=$1 | |
NAME="Icinga" | |
HOST=$2 | |
MONITOR=$3 | |
STATUS=$4 | |
EVENT=$5 | |
INCIDENT=$6 | |
# | |
# Use the arguments to build and send the JSON payload | |
#------------------------------------------------------ | |
RAW="$(curl -d '{"timestamp":"'"$1"'", "name":"'"$NAME"'", "host":"'"$2"'", "monitor":"'"$3"'", "status":"'"$4"'", "event":"'"$5"'", "incident":"'"$6"'"}' -i http://FQDN/icinga/)" | |
echo -e "CURL RESULT: $RAW" >>/tmp/dbargs.txt | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment