Skip to content

Instantly share code, notes, and snippets.

@eurica
Last active November 22, 2019 02:12
Show Gist options
  • Save eurica/6034108 to your computer and use it in GitHub Desktop.
Save eurica/6034108 to your computer and use it in GitHub Desktop.
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.
?>
@eurica
Copy link
Author

eurica commented Dec 13, 2013

@ryanhoskin
Copy link

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