Skip to content

Instantly share code, notes, and snippets.

@gjuric
Created January 15, 2015 16:12
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 gjuric/4b313a26fb572284900a to your computer and use it in GitHub Desktop.
Save gjuric/4b313a26fb572284900a to your computer and use it in GitHub Desktop.
Integrate Icinga with Slack
#!/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);
@gjuric
Copy link
Author

gjuric commented Jan 15, 2015

Place the file in /usr/local/bin/slack.php, chmod it to 755 and setup Icinga:

  define contact {
        contact_name                             slack
        alias                                    Slack
        service_notification_period              24x7
        host_notification_period                 24x7
        service_notification_options             w,u,c,r
        host_notification_options                d,r
        service_notification_commands            notify-service-by-slack
        host_notification_commands               notify-host-by-slack
  }

  define command {
        command_name     notify-service-by-slack
        command_line     /usr/local/bin/slack.php service
  }

  define command {
        command_name     notify-host-by-slack
        command_line     /usr/local/bin/slack.php host
  }

You will also need to install php5 and php5-cli

apt-get install php5 php5-cli

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