From 4f48346d568556be1d7101b0f6dc566d6766f255 Mon Sep 17 00:00:00 2001 From: laf Date: Mon, 23 Feb 2015 23:25:57 +0000 Subject: [PATCH 1/5] Added slack transport agent --- includes/alerts/transport.slack.php | 47 +++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 includes/alerts/transport.slack.php diff --git a/includes/alerts/transport.slack.php b/includes/alerts/transport.slack.php new file mode 100644 index 000000000..77ea3ed96 --- /dev/null +++ b/includes/alerts/transport.slack.php @@ -0,0 +1,47 @@ +/* Copyright (C) 2014 Daniel Preussker + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + +/** + * API Transport + * @author f0o + * @copyright 2014 f0o, LibreNMS + * @license GPL + * @package LibreNMS + * @subpackage Alerts + */ + +foreach( $opts as $method=>$apis ) { + foreach( $apis as $api ) { + list($host, $api) = explode("?",$api,2); + foreach( $obj as $k=>$v ) { + $api = str_replace("%".$k,$method == "get" ? urlencode($v) : $v, $api); + } + $curl = curl_init(); + $data = array('text' => $obj['msg']); + $alert_message = "payload=" . json_encode($data); + curl_setopt($curl, CURLOPT_URL, ($method == "get" ? $host."?".$api : $host) ); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_POST,true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $alert_message ); + $ret = curl_exec($curl); + $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); + if( $code != 200 ) { + var_dump("API '$host' returnd Error"); //FIXME: propper debuging + var_dump("Params: ".$api); //FIXME: propper debuging + var_dump("Return: ".$ret); //FIXME: propper debuging + return false; + } + } +} +return true; From 92b069bff15d11dfddcf1555adbc3790f41c06e7 Mon Sep 17 00:00:00 2001 From: laf Date: Fri, 27 Feb 2015 15:23:14 +0000 Subject: [PATCH 2/5] Final updates (hopefully) for slack transport --- includes/alerts/transport.slack.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/includes/alerts/transport.slack.php b/includes/alerts/transport.slack.php index 77ea3ed96..0fbd26cd2 100644 --- a/includes/alerts/transport.slack.php +++ b/includes/alerts/transport.slack.php @@ -22,14 +22,21 @@ */ foreach( $opts as $method=>$apis ) { - foreach( $apis as $api ) { - list($host, $api) = explode("?",$api,2); + foreach( $apis as $tmp_api ) { + list($host, $api) = explode("?",$tmp_api['url'],2); foreach( $obj as $k=>$v ) { $api = str_replace("%".$k,$method == "get" ? urlencode($v) : $v, $api); } $curl = curl_init(); - $data = array('text' => $obj['msg']); + $data = array( + 'text' => $obj['msg'], + 'channel' => $tmp_api['channel'], + 'username' => $tmp_api['username'], + 'icon_url' => $tmp_api['icon_url'], + 'icon_emoji' => $tmp_api['icon_emoji'], + ); $alert_message = "payload=" . json_encode($data); +print_r($alert_message); curl_setopt($curl, CURLOPT_URL, ($method == "get" ? $host."?".$api : $host) ); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST,true); From 06bf9cd95e1eb724deeae4345d8629a3247e3b15 Mon Sep 17 00:00:00 2001 From: laf Date: Fri, 27 Feb 2015 15:26:13 +0000 Subject: [PATCH 3/5] Removed debug line! --- includes/alerts/transport.slack.php | 1 - 1 file changed, 1 deletion(-) diff --git a/includes/alerts/transport.slack.php b/includes/alerts/transport.slack.php index 0fbd26cd2..a623402e4 100644 --- a/includes/alerts/transport.slack.php +++ b/includes/alerts/transport.slack.php @@ -36,7 +36,6 @@ foreach( $opts as $method=>$apis ) { 'icon_emoji' => $tmp_api['icon_emoji'], ); $alert_message = "payload=" . json_encode($data); -print_r($alert_message); curl_setopt($curl, CURLOPT_URL, ($method == "get" ? $host."?".$api : $host) ); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POST,true); From 54cff79acbedb60280f85d30f806e7e050406359 Mon Sep 17 00:00:00 2001 From: laf Date: Fri, 27 Feb 2015 15:36:12 +0000 Subject: [PATCH 4/5] Added docs for slack transport --- doc/Extensions/Alerting.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/doc/Extensions/Alerting.md b/doc/Extensions/Alerting.md index 02e9ea6c3..ddeddb34f 100644 --- a/doc/Extensions/Alerting.md +++ b/doc/Extensions/Alerting.md @@ -11,6 +11,7 @@ Table of Content: - [API](#transports-api) - [Nagios-Compatible](#transports-nagios) - [IRC](#transports-irc) + - [Slack](#transports-slack) # About @@ -170,3 +171,13 @@ Configuration of the LibreNMS IRC-Bot is described [here](https://github.com/lib $config['alert']['transports']['irc'] = true; ``` +## Slack + +The Slack transport will POST the alert message to your Slack Incoming WebHook, you are able to specify multiple webhooks along with the relevant options to go with it. All options are optional, the only required value is for url, without this then no call to Slack will be made. Below is an example of how to send alerts to two channels with different customised options: + +```php +$config['alert']['transports']['slack']['post'][] = array('url' => "https://hooks.slack.com/services/A12B34CDE/F56GH78JK/L901LmNopqrSTUVw2w3XYZAB4C", 'channel' => '#Alerting'); + +$config['alert']['transports']['slack']['post'][] = array('url' => "https://hooks.slack.com/services/A12B34CDE/F56GH78JK/L901LmNopqrSTUVw2w3XYZAB4C", 'channel' => '@john', 'username' => 'LibreNMS', 'icon_emoji' => ':ghost:'); + +``` From c1c520d781a9d4bd3254b48ee4077a11a26379a4 Mon Sep 17 00:00:00 2001 From: laf Date: Sat, 28 Feb 2015 11:45:30 +0000 Subject: [PATCH 5/5] Removing POST / GET code as slack only uses POST --- doc/Extensions/Alerting.md | 4 +-- includes/alerts/transport.slack.php | 52 ++++++++++++++--------------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/doc/Extensions/Alerting.md b/doc/Extensions/Alerting.md index ddeddb34f..d1b93e3d4 100644 --- a/doc/Extensions/Alerting.md +++ b/doc/Extensions/Alerting.md @@ -176,8 +176,8 @@ $config['alert']['transports']['irc'] = true; The Slack transport will POST the alert message to your Slack Incoming WebHook, you are able to specify multiple webhooks along with the relevant options to go with it. All options are optional, the only required value is for url, without this then no call to Slack will be made. Below is an example of how to send alerts to two channels with different customised options: ```php -$config['alert']['transports']['slack']['post'][] = array('url' => "https://hooks.slack.com/services/A12B34CDE/F56GH78JK/L901LmNopqrSTUVw2w3XYZAB4C", 'channel' => '#Alerting'); +$config['alert']['transports']['slack'][] = array('url' => "https://hooks.slack.com/services/A12B34CDE/F56GH78JK/L901LmNopqrSTUVw2w3XYZAB4C", 'channel' => '#Alerting'); -$config['alert']['transports']['slack']['post'][] = array('url' => "https://hooks.slack.com/services/A12B34CDE/F56GH78JK/L901LmNopqrSTUVw2w3XYZAB4C", 'channel' => '@john', 'username' => 'LibreNMS', 'icon_emoji' => ':ghost:'); +$config['alert']['transports']['slack'][] = array('url' => "https://hooks.slack.com/services/A12B34CDE/F56GH78JK/L901LmNopqrSTUVw2w3XYZAB4C", 'channel' => '@john', 'username' => 'LibreNMS', 'icon_emoji' => ':ghost:'); ``` diff --git a/includes/alerts/transport.slack.php b/includes/alerts/transport.slack.php index a623402e4..15ba3ea19 100644 --- a/includes/alerts/transport.slack.php +++ b/includes/alerts/transport.slack.php @@ -21,33 +21,31 @@ * @subpackage Alerts */ -foreach( $opts as $method=>$apis ) { - foreach( $apis as $tmp_api ) { - list($host, $api) = explode("?",$tmp_api['url'],2); - foreach( $obj as $k=>$v ) { - $api = str_replace("%".$k,$method == "get" ? urlencode($v) : $v, $api); - } - $curl = curl_init(); - $data = array( - 'text' => $obj['msg'], - 'channel' => $tmp_api['channel'], - 'username' => $tmp_api['username'], - 'icon_url' => $tmp_api['icon_url'], - 'icon_emoji' => $tmp_api['icon_emoji'], - ); - $alert_message = "payload=" . json_encode($data); - curl_setopt($curl, CURLOPT_URL, ($method == "get" ? $host."?".$api : $host) ); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($curl, CURLOPT_POST,true); - curl_setopt($curl, CURLOPT_POSTFIELDS, $alert_message ); - $ret = curl_exec($curl); - $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); - if( $code != 200 ) { - var_dump("API '$host' returnd Error"); //FIXME: propper debuging - var_dump("Params: ".$api); //FIXME: propper debuging - var_dump("Return: ".$ret); //FIXME: propper debuging - return false; - } +foreach( $opts as $tmp_api ) { + $host = $tmp_api['url']; + foreach( $obj as $k=>$v ) { + $api = str_replace("%".$k,$method == "get" ? urlencode($v) : $v, $api); + } + $curl = curl_init(); + $data = array( + 'text' => $obj['msg'], + 'channel' => $tmp_api['channel'], + 'username' => $tmp_api['username'], + 'icon_url' => $tmp_api['icon_url'], + 'icon_emoji' => $tmp_api['icon_emoji'], + ); + $alert_message = "payload=" . json_encode($data); + curl_setopt($curl, CURLOPT_URL, $host); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_POST,true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $alert_message ); + $ret = curl_exec($curl); + $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); + if( $code != 200 ) { + var_dump("API '$host' returnd Error"); //FIXME: propper debuging + var_dump("Params: ".$alert_message); //FIXME: propper debuging + var_dump("Return: ".$ret); //FIXME: propper debuging + return false; } } return true;