1 | <?php |
||
2 | /* Copyright (C) 2015 James Campbell <[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 | /* Copyright (C) 2015 Daniel Preussker <[email protected]> |
||
17 | * This program is free software: you can redistribute it and/or modify |
||
18 | * it under the terms of the GNU General Public License as published by |
||
19 | * the Free Software Foundation, either version 3 of the License, or |
||
20 | * (at your option) any later version. |
||
21 | * |
||
22 | * This program is distributed in the hope that it will be useful, |
||
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||
25 | * GNU General Public License for more details. |
||
26 | * |
||
27 | * You should have received a copy of the GNU General Public License |
||
28 | * along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
||
29 | |||
30 | /** |
||
31 | * Boxcar API Transport |
||
32 | * |
||
33 | * @author trick77 <[email protected]> |
||
34 | * @copyright 2015 trick77, neokjames, f0o, LibreNMS |
||
35 | * @license GPL |
||
36 | */ |
||
37 | |||
38 | namespace LibreNMS\Alert\Transport; |
||
39 | |||
40 | use LibreNMS\Alert\Transport; |
||
41 | use LibreNMS\Config; |
||
42 | use LibreNMS\Enum\AlertState; |
||
43 | |||
44 | class Boxcar extends Transport |
||
45 | { |
||
46 | public function deliverAlert($obj, $opts) |
||
47 | { |
||
48 | $boxcar_opts = $this->parseUserOptions($this->config['options']); |
||
49 | $boxcar_opts['access_token'] = $this->config['boxcar-token']; |
||
50 | |||
51 | return $this->contactBoxcar($obj, $boxcar_opts); |
||
52 | } |
||
53 | |||
54 | public static function contactBoxcar($obj, $api) |
||
55 | { |
||
56 | $data = []; |
||
57 | $data['user_credentials'] = $api['access_token']; |
||
58 | $data['notification[source_name]'] = Config::get('project_id', 'librenms'); |
||
59 | switch ($obj['severity']) { |
||
60 | case 'critical': |
||
61 | $severity = 'Critical'; |
||
62 | if (! empty($api['sound_critical'])) { |
||
63 | $data['notification[sound]'] = $api['sound_critical']; |
||
64 | } |
||
65 | break; |
||
66 | case 'warning': |
||
67 | $severity = 'Warning'; |
||
68 | if (! empty($api['sound_warning'])) { |
||
69 | $data['notification[sound]'] = $api['sound_warning']; |
||
70 | } |
||
71 | break; |
||
72 | default: |
||
73 | $severity = 'Unknown'; |
||
74 | break; |
||
75 | } |
||
76 | switch ($obj['state']) { |
||
77 | case AlertState::RECOVERED: |
||
78 | $title_text = 'OK'; |
||
79 | if (! empty($api['sound_ok'])) { |
||
80 | $data['notification[sound]'] = $api['sound_ok']; |
||
81 | } |
||
82 | break; |
||
83 | case AlertState::ACTIVE: |
||
84 | $title_text = $severity; |
||
85 | break; |
||
86 | case AlertState::ACKNOWLEDGED: |
||
87 | $title_text = 'Acknowledged'; |
||
88 | break; |
||
89 | default: |
||
90 | $title_text = $severity; |
||
91 | break; |
||
92 | |||
93 | } |
||
94 | $data['notification[title]'] = $title_text . ' - ' . $obj['hostname'] . ' - ' . $obj['name']; |
||
95 | $message_text = 'Timestamp: ' . $obj['timestamp']; |
||
96 | if (! empty($obj['faults'])) { |
||
97 | $message_text .= "\n\nFaults:\n"; |
||
98 | foreach ($obj['faults'] as $k => $faults) { |
||
99 | $message_text .= '#' . $k . ' ' . $faults['string'] . "\n"; |
||
100 | } |
||
101 | } |
||
102 | $data['notification[long_message]'] = $message_text; |
||
103 | $curl = curl_init(); |
||
104 | set_curl_proxy($curl); |
||
105 | curl_setopt($curl, CURLOPT_URL, 'https://new.boxcar.io/api/notifications'); |
||
106 | curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true); |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
107 | curl_setopt($curl, CURLOPT_POSTFIELDS, $data); |
||
108 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
||
109 | curl_exec($curl); |
||
110 | $code = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||
111 | if ($code != 201) { |
||
112 | var_dump('Boxcar returned error'); //FIXME: proper debugging |
||
113 | |||
114 | return false; |
||
115 | } |
||
116 | |||
117 | return true; |
||
118 | } |
||
119 | |||
120 | public static function configTemplate() |
||
121 | { |
||
122 | return [ |
||
123 | 'config' => [ |
||
124 | [ |
||
125 | 'title' => 'Access Token', |
||
126 | 'name' => 'boxcar-token', |
||
127 | 'descr' => 'Boxcar Access Token', |
||
128 | 'type' => 'text', |
||
129 | ], |
||
130 | [ |
||
131 | 'title' => 'Boxcar Options', |
||
132 | 'name' => 'boxcar-options', |
||
133 | 'descr' => 'Boxcar Options', |
||
134 | 'type' => 'textarea', |
||
135 | ], |
||
136 | ], |
||
137 | 'validation' => [ |
||
138 | 'boxcar-token' => 'required', |
||
139 | ], |
||
140 | ]; |
||
141 | } |
||
142 | } |
||
143 |