Skip to content

Instantly share code, notes, and snippets.

@jeanfrancis
Forked from eurica/PagerDutyWebhookToEmail.php
Last active August 29, 2015 14:14
Show Gist options
  • Save jeanfrancis/f7f6b235dddd50ea7581 to your computer and use it in GitHub Desktop.
Save jeanfrancis/f7f6b235dddd50ea7581 to your computer and use it in GitHub Desktop.
Sample PHP code to accept PagerDuty webhooks and send out notifications by email on state changes.
For more information, see http://developer.pagerduty.com/documentation/rest/webhooks
This example threads emails based on "$status: $description on $service" so each update to each incident would start a new thread.
This code is unsupported by PagerDuty.
<?php
$emailAddress = "[email protected]";
$messages = json_decode($HTTP_RAW_POST_DATA); // alternately, try file_get_contents('php://input');
if ($messages) foreach ($messages->messages as $webhook) {
$service = $webhook->data->incident->service->name;
$description = $webhook->data->incident->trigger_summary_data->subject." ".$webhook->data->incident->trigger_summary_data->description;
$status = $webhook->data->incident->status;
$subject = "$status: $description on $service";
$link = $webhook->data->incident->html_url;
$body = "$subject\n$link\n\n$HTTP_RAW_POST_DATA";
mail($emailAddress, $subject, $body);
}
//PagerDuty doesn't keep the output of this script.
?>
Sample PHP code to accept PagerDuty webhooks and send out notifications by email on state changes.
For more information, see http://developer.pagerduty.com/documentation/rest/webhooks
This example threads emails based on "$status on $service" so each service will have its own thread.
It also only sends emails for incidents that have been resolved.
This code is unsupported by PagerDuty.
<?php
$emailAddress = "[email protected]";
$messages = json_decode($HTTP_RAW_POST_DATA); // alternately, try file_get_contents('php://input');
if ($messages) foreach ($messages->messages as $webhook) {
$service = $webhook->data->incident->service->name;
$description = $webhook->data->incident->trigger_summary_data->subject." ".$webhook->data->incident->trigger_summary_data->description;
$status = $webhook->data->incident->status;
$subject = "$status on $service";
$link = $webhook->data->incident->html_url;
$body = "$subject\n$link\n\n$HTTP_RAW_POST_DATA";
if($status == "Resolved") {
mail($emailAddress, $subject, $body);
}
}
//PagerDuty doesn't keep the output of this script.
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment