Create a gist now

Instantly share code, notes, and snippets.

Simple example of using PagerDuty webhooks and PHP to forward all incident state changes to an email address.
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 = "CHANGEME@example.com";
$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 = "CHANGEME@example.com";
$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.
?>
@ryanhoskin

Here's a script for parsing Nagios incident webhooks and including information in the subject line:
https://gist.github.com/ryanhoskin/8445196

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment