1 | <?php |
||
2 | /* Copyright (C) 2014 Daniel Preussker <[email protected]>, Tyler Christiansen <[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 | * @author Tyler Christiansen <[email protected]> |
||
19 | * @copyright 2014 Daniel Preussker, Tyler Christiansen, LibreNMS |
||
20 | * @license GPL |
||
21 | * @package LibreNMS |
||
22 | * @subpackage Alerts |
||
23 | */ |
||
24 | |||
25 | namespace LibreNMS\Alert\Transport; |
||
26 | |||
27 | use LibreNMS\Alert\Transport; |
||
28 | |||
29 | class Hipchat extends Transport |
||
30 | { |
||
31 | public function deliverAlert($obj, $opts) |
||
32 | { |
||
33 | $hipchat_opts = $this->parseUserOptions($this->config['hipchat-options']); |
||
34 | $hipchat_opts['url'] = $this->config['hipchat-url']; |
||
35 | $hipchat_opts['room_id'] = $this->config['hipchat-room-id']; |
||
36 | $hipchat_opts['from'] = $this->config['hipchat-from-name']; |
||
37 | |||
38 | return $this->contactHipchat($obj, $hipchat_opts); |
||
39 | } |
||
40 | |||
41 | public function contactHipchat($obj, $option) |
||
42 | { |
||
43 | $version = 1; |
||
44 | if (stripos($option['url'], 'v2')) { |
||
45 | $version = 2; |
||
46 | } |
||
47 | |||
48 | // Generate our URL from the base URL + room_id and the auth token if the version is 2. |
||
49 | $url = $option['url']; |
||
50 | if ($version == 2) { |
||
51 | $url .= '/' . urlencode($option['room_id']) . '/notification?auth_token=' . urlencode($option['auth_token']); |
||
52 | } |
||
53 | |||
54 | $curl = curl_init(); |
||
55 | |||
56 | if (empty($obj['msg'])) { |
||
57 | return 'Empty Message'; |
||
58 | } |
||
59 | |||
60 | if (empty($option['message_format'])) { |
||
61 | $option['message_format'] = 'text'; |
||
62 | } |
||
63 | |||
64 | // Sane default of making the message color green if the message indicates |
||
65 | // that the alert recovered. If it rebooted, make it yellow. |
||
66 | if (stripos($obj['msg'], 'recovered')) { |
||
67 | $color = 'green'; |
||
68 | } elseif (stripos($obj['msg'], 'rebooted')) { |
||
69 | $color = 'yellow'; |
||
70 | } else { |
||
71 | if (empty($option['color']) || $option['color'] == 'u') { |
||
72 | $color = 'red'; |
||
73 | } else { |
||
74 | $color = $option['color']; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | $data[] = 'message=' . urlencode($obj['msg']); |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
79 | if ($version == 1) { |
||
80 | $data[] = 'room_id=' . urlencode($option['room_id']); |
||
81 | } |
||
82 | $data[] = 'from=' . urlencode($option['from']); |
||
83 | $data[] = 'color=' . urlencode($color); |
||
84 | $data[] = 'notify=' . urlencode($option['notify']); |
||
85 | $data[] = 'message_format=' . urlencode($option['message_format']); |
||
86 | |||
87 | $data = implode('&', $data); |
||
88 | set_curl_proxy($curl); |
||
89 | curl_setopt($curl, CURLOPT_URL, $url); |
||
90 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
||
91 | curl_setopt($curl, CURLOPT_POST, true); |
||
92 | curl_setopt($curl, CURLOPT_POSTFIELDS, $data); |
||
93 | curl_setopt($curl, CURLOPT_HTTPHEADER, [ |
||
94 | 'Content-Type: application/x-www-form-urlencoded', |
||
95 | ]); |
||
96 | $ret = curl_exec($curl); |
||
97 | |||
98 | $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
99 | if ($code != 200 && $code != 204) { |
||
100 | var_dump("API '$url' returned Error"); |
||
101 | //var_dump('Params: ' . $message); |
||
102 | var_dump('Return: ' . $ret); |
||
103 | |||
104 | return 'HTTP Status code ' . $code; |
||
105 | } |
||
106 | |||
107 | return true; |
||
108 | } |
||
109 | |||
110 | public static function configTemplate() |
||
111 | { |
||
112 | return [ |
||
113 | 'config' => [ |
||
114 | [ |
||
115 | 'title' => 'API URL', |
||
116 | 'name' => 'hipchat-url', |
||
117 | 'descr' => 'Hipchat API URL', |
||
118 | 'type' => 'text', |
||
119 | ], |
||
120 | [ |
||
121 | 'title' => 'Room ID', |
||
122 | 'name' => 'hipchat-room-id', |
||
123 | 'descr' => 'Hipchat Room ID', |
||
124 | 'type' => 'text', |
||
125 | ], |
||
126 | [ |
||
127 | 'title' => 'From Name', |
||
128 | 'name' => 'hipchat-from-name', |
||
129 | 'descr' => 'From Name', |
||
130 | 'type' => 'text', |
||
131 | ], |
||
132 | [ |
||
133 | 'title' => 'Hipchat Options', |
||
134 | 'name' => 'hipchat-options', |
||
135 | 'descr' => 'Hipchat Options', |
||
136 | 'type' => 'textarea', |
||
137 | ], |
||
138 | ], |
||
139 | 'validation' => [ |
||
140 | 'hipchat-url' => 'required|url', |
||
141 | 'hipchat-room-id' => 'required|numeric', |
||
142 | ], |
||
143 | ]; |
||
144 | } |
||
145 | } |
||
146 |