Page MenuHomeMiraheze

Cachet eventhandler
ArchivedPublic

Authored by Samtar on Aug 8 2016, 13:41.
Referenced Files
F7501: Cachet eventhandler
Aug 8 2016, 17:15
F7477: Cachet eventhandler
Aug 8 2016, 13:41
Subscribers
#!/usr/bin/php5
<?php if ($argc != 6) {
echo 'Usage: ' . basename(__FILE__) . ' cachet_component service_name service_state service_state_type service_output' . "\n";
exit(1);
}
$cachet_url = 'https://status.miraheze.org/api/v1/';
$api_key = hiera('passwords::cachet::apitoken');
$incident_prefix = '[Nagios]';
$cachet_notify_subscribers = true; // Enable subscribers notifcation for incidents creation and updates
$cachet_component = $argv[1];
$service_name = $argv[2];
$service_status = $argv[3];
$service_status_type = $argv[4];
$service_output = $argv[5];
define('CACHET_STATUS_INVESTIGATING', 1);
define('CACHET_STATUS_IDENTIFIED', 2);
define('CACHET_STATUS_WATCHING', 3);
define('CACHET_STATUS_FIXED', 4);
define('CACHET_COMPONENTSTATUS_OK', 1);
define('CACHET_COMPONENTSTATUS_INVESTIGATING', 2);
define('CACHET_COMPONENTSTATUS_PARTIALOUTAGE', 3);
define('CACHET_COMPONENTSTATUS_MAJOROUTAGE', 4);
function cachet_query($api_part, $action = 'GET', $data = null) {
global $api_key, $cachet_url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $cachet_url . $api_part);
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (in_array($action, array('GET', 'POST', 'PUT'))) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $action);
}
if ($data !== null && is_array($data)) {
$ch_data = http_build_query($data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ch_data);
}
$ch_headers = array(
'X-Cachet-Token: ' . $api_key
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $ch_headers);
curl_setopt($ch, CURLOPT_HEADER, false); // Don't return headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return body
$http_body = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array('code' => $http_code, 'body' => json_decode($http_body));
}
/* Find Cachet component ID */
$result = cachet_query('components'); if ($result['code'] != 200) {
echo 'Can\'t query components' . "\n";
exit(1);
}
$cachet_component_id = false;
foreach ($result['body']->data as $component) {
if ($cachet_component == $component->name) { // We nailed it
$cachet_component_id = $component->id;
break; // Yes, bad.
}
}
if ($cachet_component_id === false) {
echo 'Can\'t find component "' . $cachet_component . '"' . "\n";
exit(1);
}
/* Determine what to to:
- if PROBLEM and SOFT then don't cry just yet
- if PROBLEM and HARD then create incident
- if RECOVERY and SOFT then update incident
- if RECOVERY and HARD then update incident PROBLEM = !OK = (WARNING | CRITICAL | UNKONWN) RECOVERY = OK */
if ($service_status != 'OK' && $service_status_type == 'SOFT') { // Hope it will be back soon
echo 'KO SOFT: not doing anything' . "\n";
exit(0);
} elseif ($service_status != 'OK' && $service_status_type == 'HARD') { // Something went wrong, let's notify
echo 'KO HARD: creating incident' . "\n";
$query = array(
'name' => $incident_prefix . ' ' . $service_name,
'message' => $service_output,
'status' => CACHET_STATUS_INVESTIGATING,
'component_id' => $cachet_component_id,
'notify' => $cachet_notify_subscribers,
'component_status' => 4,
);
$result = cachet_query('incidents', 'POST', $query);
if ($result['code'] != 200) {
echo 'Can\'t create incident' . "\n";
exit(1);
}
} elseif ($service_status == 'OK' && $service_status_type == 'SOFT') { // Recovery underway
echo 'OK SOFT: updating incident' . "\n";
/* Get the incident ID */
$results = cachet_query('incidents');
if ($result['code'] != 200) {
echo 'Can\'t get incidents' . "\n";
exit(1);
}
$cachet_incident_id = false;
foreach ($results['body']->data as $incident) {
if ($incident->name == $incident_prefix . ' ' . $service_name) {
$cachet_incident_id = $incident->id;
break; // Yes, bad.
}
}
if ($cachet_incident_id === false) {
echo 'Can\'t find incident "' . $incident_prefix . ' ' . $service_name . '"' . "\n";
exit(1);
}
/* Update the incident */
$query = array(
'name' => $incident_prefix . ' ' . $service_name,
'message' => $service_output,
'status' => CACHET_STATUS_WATCHING,
'component_id' => $cachet_component_id,
'notify' => $cachet_notify_subscribers,
'component_status' => 3,
);
/* Use a POST query as that will keep an incident history */
$result = cachet_query('incidents', 'POST', $query);
if ($result['code'] != 200) {
echo 'Can\'t update incident' . "\n";
exit(1);
}
} elseif ($service_status == 'OK' && $service_status_type == 'HARD') { // Recovery completed
echo 'OK HARD: updating incident' . "\n";
/* Get the incident ID */
$results = cachet_query('incidents');
if ($result['code'] != 200) {
echo 'Can\'t get incidents' . "\n";
exit(1);
}
$cachet_incident_id = false;
foreach ($results['body']->data as $incident) {
if ($incident->name == $incident_prefix . ' ' . $service_name) {
$cachet_incident_id = $incident->id;
break; // Yes, bad.
}
}
if ($cachet_incident_id === false) {
echo 'Can\'t find incident "' . $incident_prefix . ' ' . $service_name . '"' . "\n";
exit(1);
}
/* Update the incident */
$query = array(
'name' => $incident_prefix . ' ' . $service_name,
'message' => $service_output,
'status' => CACHET_STATUS_FIXED,
'component_id' => $cachet_component_id,
'notify' => $cachet_notify_subscribers,
'component_status' => 1,
);
/* Use a POST query as that will keep an incident history */
$result = cachet_query('incidents', 'POST', $query);
if ($result['code'] != 200) {
echo 'Can\'t update incident' . "\n";
exit(1);
}
} else {
echo 'Bad arguments' . "\n";
exit(1);
}
exit(0);

Event Timeline

Samtar created this object in space S1 Public.
Samtar created this object with visibility "All Users".
NDKilla changed the visibility from "All Users" to "Public (No Login Required)".
NDKilla subscribed.

Updated per commit 086ed60a