Created
January 15, 2015 16:12
-
-
Save gjuric/4b313a26fb572284900a to your computer and use it in GitHub Desktop.
Integrate Icinga with Slack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
$slack_botname = "Icinga"; | |
$slack_webhook_url = "https://WEB-HOOK-URL"; | |
$icinga_hostname = "http://IP-OR-HOSTNAME-OF-YOUR-ICINGA-INSTALLATION"; | |
$type = $argv[1]; | |
$state = ("host" === $type) ? getenv('ICINGA_HOSTSTATE') : getenv('ICINGA_SERVICESTATE'); | |
/** | |
* Hosts can be in these states: UP, DOWN, UNREACHABLE | |
* Services can be in these states: OK, WARNING, UNKNOWN, CRITICAL | |
*/ | |
switch ($state) { | |
case 'UP': | |
case 'OK': | |
$icon = ":white_check_mark:"; | |
break; | |
case 'DOWN': | |
case 'CRITICAL': | |
$icon = ":exclamation:"; | |
break; | |
case 'WARNING': | |
$icon = ":warning:"; | |
break; | |
case 'UNREACHABLE': | |
case 'UNKNOWN': | |
$icon = ":question:"; | |
break; | |
default: | |
$icon = ":white_medium_square:"; | |
break; | |
} | |
$hostname = getenv('ICINGA_HOSTNAME'); | |
if ("host" === $type) { | |
$msg = "${icon} Host: $hostname\n"; | |
$msg .= "Message: " . getenv('ICINGA_HOSTOUTPUT') . "\n"; | |
if (!is_null(getenv('ICINGA_HOSTPERFDATA'))) { | |
$msg .= "Perfdata: " . getenv('ICINGA_HOSTPERFDATA') . "\n"; | |
} | |
$msg .= "<${icinga_hostname}/cgi-bin/icinga/extinfo.cgi?type=1&host=${hostname}|View host details>"; | |
} else { | |
$msg = "Host: $hostname\n"; | |
$msg .= "${icon} Service: " . getenv('ICINGA_SERVICEDISPLAYNAME') . "\n"; | |
$msg .= "Message: " . getenv('ICINGA_SERVICEOUTPUT') . "\n"; | |
if (!is_null(getenv('ICINGA_SERVICEPERFDATA'))) { | |
$msg .= "Perfdata: " . getenv('ICINGA_SERVICEPERFDATA') . "\n"; | |
} | |
$msg .= "<${icinga_hostname}/cgi-bin/icinga/extinfo.cgi?type=1&host=${hostname}|View host details>"; | |
} | |
$payload = array( | |
"username" => $slack_botname, | |
// slacks icon for Nagios, will do for now | |
"icon_url" => "https://slack.global.ssl.fastly.net/20653/img/services/nagios_128.png", | |
"text" => $msg, | |
); | |
$data_string = json_encode($payload); | |
$ch = curl_init($slack_webhook_url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($data_string)) | |
); | |
$result = curl_exec($ch); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Place the file in /usr/local/bin/slack.php, chmod it to 755 and setup Icinga:
You will also need to install php5 and php5-cli