Created
February 3, 2015 05:49
-
-
Save lippytak/c5978e631c173c188eb6 to your computer and use it in GitHub Desktop.
Create StatusPage incidents from Pingometer events
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
<?php | |
// connection settings | |
define('PINGOMETER_USERNAME', $_ENV["PINGOMETER_USERNAME"]); | |
define('PINGOMETER_PASSWORD', $_ENV["PINGOMETER_PASSWORD"]); | |
define('PINGOMETER_BASE_URI', 'https://app.pingometer.com/api/v1.0'); | |
define('STATUSPAGE_API_KEY', $_ENV["STATUSPAGE_API_KEY"]); | |
define('STATUSPAGE_PAGE_ID', $_ENV["STATUSPAGE_PAGE_ID"]); | |
define('STATUSPAGE_BASE_URI', 'https://api.statuspage.io/v1'); | |
// validations | |
$result = false; | |
if (isset($_POST['monitor_id']) && !empty($_POST['monitor_id']) && isset($_POST['monitor_status'])) { | |
if ($_POST['monitor_status'] == 0 || $_POST['monitor_status'] == 1) { | |
$pingometer_last_event_status = $_POST['monitor_status']; | |
$pingometer_monitor_id = $_POST['monitor_id']; | |
// pingometer api | |
$pingometer_ch = curl_init(sprintf("%s/monitor/%s/", PINGOMETER_BASE_URI, $pingometer_monitor_id)); | |
curl_setopt($pingometer_ch, CURLOPT_HTTPHEADER, array("Authorization: Basic " . base64_encode(PINGOMETER_USERNAME . ":" . PINGOMETER_PASSWORD))); | |
curl_setopt($pingometer_ch, CURLOPT_POST, false); | |
curl_setopt($pingometer_ch, CURLOPT_RETURNTRANSFER, 1); | |
$pingometer_result = json_decode(curl_exec($pingometer_ch), false); | |
$pingometer_monitor_name = $pingometer_result->monitor[0]->name; | |
$statuspage_component_id = $pingometer_result->monitor[0]->integrations->statuspage->component_id; | |
$result = true; | |
} | |
} | |
// process webhook | |
if ($result) { | |
// statuspage api | |
$statuspage_ch = curl_init(sprintf("%s/pages/%s/incidents.json", STATUSPAGE_BASE_URI, STATUSPAGE_PAGE_ID)); | |
curl_setopt($statuspage_ch, CURLOPT_HTTPHEADER, array("Authorization: OAuth " . STATUSPAGE_API_KEY, "Expect: 100-continue")); | |
curl_setopt($statuspage_ch, CURLOPT_POST, true); | |
curl_setopt($statuspage_ch, CURLOPT_RETURNTRANSFER, 1); | |
if ($pingometer_last_event_status == 0) { | |
$statuspage_incident_status = 'identified'; | |
} else { | |
$statuspage_incident_status = 'resolved'; | |
} | |
$postparams = array("incident[name]" => "Issue with " . $pingometer_monitor_name, "incident[status]" => $statuspage_incident_status, "incident[component_ids][]" => $statuspage_component_id); | |
curl_setopt($statuspage_ch, CURLOPT_POSTFIELDS, $postparams); | |
$statuspage_result = curl_exec($statuspage_ch); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment