Skip to content

Instantly share code, notes, and snippets.

@rickheil
Last active August 29, 2015 14:28
Show Gist options
  • Save rickheil/26fc8e4b178c79c09213 to your computer and use it in GitHub Desktop.
Save rickheil/26fc8e4b178c79c09213 to your computer and use it in GitHub Desktop.
<?php
/**
* Observium
* @package observium
* @subpackage webui
* @author Rick Heil ([email protected])
*/
echo ("SENDING SLACK NOTIFICATION\n");
$slack_webhook = $config['alerts']['slack']['webhook_url'];
// Get a plain text link of the URL to the entity. The value in the array has <a> tags which we don't want here.
$entity_link = substr($message_tags['ENTITY_LINK'], 9);
$entity_link = substr($entity_link, 0, -65);
$pretext = $message_tags['ALERT_STATE'] . " - " . $message_tags['DEVICE_HOSTNAME'] . " [" . $message_tags['ENTITY_NAME'] . "] ";
$title = $message_tags['ALERT_STATE'] . " - " . $message_tags['DEVICE_HOSTNAME'];
// Set color based on alert state. Use blue if there's a doubt.
if ($message_tags['ALERT_STATE'] == "ALERT") {
$color = "warning";
}
else if ($message_tags['ALERT_STATE'] == "RECOVERY") {
$color = "good";
}
else {
$color = "#439FE0";
}
// Construct the message itself
$slack_attachment = array(
'fallback' => $title,
'title' => "Go to Observium page",
'title_link' => $entity_link,
'text' => $pretext,
'color' => "$color"
);
$slack_message = array(
'text' => $pretext,
'attachments' => array($slack_attachment)
);
$slack_json = json_encode($slack_message);
$curl_req = curl_init("$slack_webhook");
curl_setopt($curl_req, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl_req, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl_req, CURLOPT_POSTFIELDS, $slack_json);
curl_setopt($curl_req, CURLOPT_RETURNTRANSFER, true);
$request_result = curl_exec($curl_req);
curl_close($curl_req);
echo "Slack POST result: $request_result\n";
// EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment