Skip to content

Instantly share code, notes, and snippets.

@ryanhoskin
Created January 15, 2014 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanhoskin/8445196 to your computer and use it in GitHub Desktop.
Save ryanhoskin/8445196 to your computer and use it in GitHub Desktop.
PagerDuty webhooks email script for Nagios.
<?php
//Nagios alerts with a custom subject line that includes the HOSTNAME, SERVICESTATE, SERVICEDESC, HOSTSTATE and PagerDuty incident status fields.
$messages = json_decode($HTTP_RAW_POST_DATA);
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;
$link = $webhook->data->incident->html_url;
$emailAddress = ($webhook->type == "incident.resolve" ? $webhook->data->incident->last_status_change_by->email : $webhook->data->incident->assigned_to_user->email);
$body = "PagerDuty Update\n\nAn incident has been $status.\n\nDetails: $description on service $service\n\nClick here for more details: $link\n";
$subject = "[" . $webhook->data->incident->trigger_summary_data->HOSTNAME . "/"
. $webhook->data->incident->trigger_summary_data->SERVICESTATE . "/"
. $webhook->data->incident->trigger_summary_data->SERVICEDESC . "/"
. $webhook->data->incident->trigger_summary_data->HOSTSTATE . "] "
. $webhook->data->incident->status;
$success = mail($emailAddress, $subject, $body);
if (!$success) {
mail($emailAddress, "Mail failed", "Mail failed");
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment