1 | <?php |
||
2 | /* Copyright (C) 2014 Daniel Preussker <[email protected]> |
||
3 | * This program is free software: you can redistribute it and/or modify |
||
4 | * it under the terms of the GNU General Public License as published by |
||
5 | * the Free Software Foundation, either version 3 of the License, or |
||
6 | * (at your option) any later version. |
||
7 | * |
||
8 | * This program is distributed in the hope that it will be useful, |
||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||
11 | * GNU General Public License for more details. |
||
12 | * |
||
13 | * You should have received a copy of the GNU General Public License |
||
14 | * along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
||
15 | |||
16 | /** |
||
17 | * API Transport |
||
18 | * |
||
19 | * @author f0o <[email protected]> |
||
20 | * @copyright 2014 f0o, LibreNMS |
||
21 | * @license GPL |
||
22 | */ |
||
23 | |||
24 | namespace LibreNMS\Alert\Transport; |
||
25 | |||
26 | use LibreNMS\Alert\Transport; |
||
27 | |||
28 | class Slack extends Transport |
||
29 | { |
||
30 | public function deliverAlert($obj, $opts) |
||
31 | { |
||
32 | $slack_opts = $this->parseUserOptions($this->config['slack-options']); |
||
33 | $slack_opts['url'] = $this->config['slack-url']; |
||
34 | |||
35 | return $this->contactSlack($obj, $slack_opts); |
||
36 | } |
||
37 | |||
38 | public static function contactSlack($obj, $api) |
||
39 | { |
||
40 | $host = $api['url']; |
||
41 | $curl = curl_init(); |
||
42 | $slack_msg = html_entity_decode(strip_tags($obj['msg']), ENT_QUOTES); |
||
43 | $color = self::getColorForState($obj['state']); |
||
44 | $data = [ |
||
45 | 'attachments' => [ |
||
46 | 0 => [ |
||
47 | 'fallback' => $slack_msg, |
||
48 | 'color' => $color, |
||
49 | 'title' => $obj['title'], |
||
50 | 'text' => $slack_msg, |
||
51 | 'mrkdwn_in' => ['text', 'fallback'], |
||
52 | 'author_name' => $api['author_name'], |
||
53 | ], |
||
54 | ], |
||
55 | 'channel' => $api['channel'], |
||
56 | 'username' => $api['username'], |
||
57 | 'icon_emoji' => ':' . $api['icon_emoji'] . ':', |
||
58 | ]; |
||
59 | $alert_message = json_encode($data); |
||
60 | curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); |
||
61 | set_curl_proxy($curl); |
||
62 | curl_setopt($curl, CURLOPT_URL, $host); |
||
63 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
||
64 | curl_setopt($curl, CURLOPT_POST, true); |
||
65 | curl_setopt($curl, CURLOPT_POSTFIELDS, $alert_message); |
||
66 | |||
67 | $ret = curl_exec($curl); |
||
68 | $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
69 | if ($code != 200) { |
||
70 | var_dump("API '$host' returned Error"); //FIXME: propper debuging |
||
0 ignored issues
–
show
Security
Debugging Code
introduced
by
Loading history...
|
|||
71 | var_dump('Params: ' . $alert_message); //FIXME: propper debuging |
||
72 | var_dump('Return: ' . $ret); //FIXME: propper debuging |
||
73 | |||
74 | return 'HTTP Status code ' . $code; |
||
75 | } |
||
76 | |||
77 | return true; |
||
78 | } |
||
79 | |||
80 | public static function configTemplate() |
||
81 | { |
||
82 | return [ |
||
83 | 'config' => [ |
||
84 | [ |
||
85 | 'title' => 'Webhook URL', |
||
86 | 'name' => 'slack-url', |
||
87 | 'descr' => 'Slack Webhook URL', |
||
88 | 'type' => 'text', |
||
89 | ], |
||
90 | [ |
||
91 | 'title' => 'Slack Options', |
||
92 | 'name' => 'slack-options', |
||
93 | 'descr' => 'Slack Options', |
||
94 | 'type' => 'textarea', |
||
95 | ], |
||
96 | ], |
||
97 | 'validation' => [ |
||
98 | 'slack-url' => 'required|url', |
||
99 | ], |
||
100 | ]; |
||
101 | } |
||
102 | } |
||
103 |