1 | <?php |
||
2 | /* |
||
3 | * This program is free software: you can redistribute it and/or modify it |
||
4 | * under the terms of the GNU General Public License as published by the |
||
5 | * Free Software Foundation, either version 3 of the License, or (at your |
||
6 | * option) any later version. Please see LICENSE.txt at the top level of |
||
7 | * the source code distribution for details. |
||
8 | * |
||
9 | * @package LibreNMS |
||
10 | * @subpackage graphs |
||
11 | * @link https://www.librenms.org |
||
12 | * @copyright 2017 LibreNMS |
||
13 | * @author LibreNMS Contributors |
||
14 | */ |
||
15 | |||
16 | /* FIXME: is there a central place we can put this? */ |
||
17 | |||
18 | $alert_states = [ |
||
19 | // divined from librenms/alerts.php |
||
20 | 'recovered' => 0, |
||
21 | 'alerted' => 1, |
||
22 | 'acknowledged' => 2, |
||
23 | 'worse' => 3, |
||
24 | 'better' => 4, |
||
25 | ]; |
||
26 | |||
27 | $alert_severities = [ |
||
28 | // alert_rules.status is enum('ok','warning','critical') |
||
29 | 'ok' => 1, |
||
30 | 'warning' => 2, |
||
31 | 'critical' => 3, |
||
32 | 'ok only' => 4, |
||
33 | 'warning only' => 5, |
||
34 | 'critical only' => 6, |
||
35 | ]; |
||
36 | if (Auth::user()->hasGlobalAdmin()) { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
37 | $admin_verbose_details = '<th data-column-id="verbose_details" data-sortable="false">Details</th>'; |
||
38 | } |
||
39 | |||
40 | //if( defined('SHOW_SETTINGS') || empty($widget_settings) ) { |
||
41 | if (defined('SHOW_SETTINGS')) { |
||
42 | $current_acknowledged = isset($widget_settings['acknowledged']) ? $widget_settings['acknowledged'] : ''; |
||
43 | $current_fired = isset($widget_settings['fired']) ? $widget_settings['fired'] : ''; |
||
44 | $current_severity = isset($widget_settings['severity']) ? $widget_settings['severity'] : ''; |
||
45 | $current_state = isset($widget_settings['state']) ? $widget_settings['state'] : ''; |
||
46 | $current_group = isset($widget_settings['group']) ? $widget_settings['group'] : ''; |
||
47 | $current_proc = isset($widget_settings['proc']) ? $widget_settings['proc'] : ''; |
||
48 | $current_sorting = isset($widget_settings['sort']) ? $widget_settings['sort'] : ''; |
||
49 | |||
50 | $common_output[] = ' |
||
51 | <form class="form" onsubmit="widget_settings(this); return false;"> |
||
52 | ' . csrf_field() . ' |
||
53 | <div class="form-group row"> |
||
54 | <div class="col-sm-4"> |
||
55 | <label for="acknowledged" class="control-label">Show acknowledged alerts: </label> |
||
56 | </div> |
||
57 | <div class="col-sm-8"> |
||
58 | <select class="form-control" name="acknowledged">'; |
||
59 | |||
60 | $common_output[] = '<option value=""' . ($current_acknowledged == '' ? ' selected' : ' ') . '>not filtered</option>'; |
||
61 | $common_output[] = '<option value="1"' . ($current_acknowledged == '1' ? ' selected' : ' ') . '>show only acknowledged</option>'; |
||
62 | $common_output[] = '<option value="0"' . ($current_acknowledged == '0' ? ' selected' : ' ') . '>hide acknowledged</option>'; |
||
63 | |||
64 | $common_output[] = ' |
||
65 | </select> |
||
66 | </div> |
||
67 | </div> |
||
68 | <div class="form-group row"> |
||
69 | <div class="col-sm-4"> |
||
70 | <label for="fired" class="control-label">Show only Fired alerts: </label> |
||
71 | </div> |
||
72 | <div class="col-sm-8"> |
||
73 | <select class="form-control" name="fired">'; |
||
74 | |||
75 | $common_output[] = '<option value=""' . ($current_fired == '' ? ' selected' : ' ') . '>not filtered</option>'; |
||
76 | $common_output[] = '<option value="1"' . ($current_fired == '1' ? ' selected' : ' ') . '>show only Fired alerts</option>'; |
||
77 | |||
78 | $common_output[] = ' |
||
79 | </select> |
||
80 | </div> |
||
81 | </div> |
||
82 | <div class="form-group row"> |
||
83 | <div class="col-sm-4"> |
||
84 | <label for="min_severity" class="control-label">Displayed severity:</label> |
||
85 | </div> |
||
86 | <div class="col-sm-8"> |
||
87 | <select class="form-control" name="min_severity"> |
||
88 | <option value="">any severity</option>'; |
||
89 | |||
90 | foreach ($alert_severities as $name => $val) { |
||
91 | $common_output[] = "<option value=\"$val\"" . ($current_severity == $name || $current_severity == $val ? ' selected' : '') . ">$name" . ($val > 3 ? '' : ' or higher') . '</option>'; |
||
92 | } |
||
93 | |||
94 | $common_output[] = ' |
||
95 | </select> |
||
96 | </div> |
||
97 | </div> |
||
98 | <div class="form-group row"> |
||
99 | <div class="col-sm-4"> |
||
100 | <label for="state" class="control-label">State:</label> |
||
101 | </div> |
||
102 | <div class="col-sm-8"> |
||
103 | <select class="form-control" name="state">'; |
||
104 | $common_output[] = '<option value=""' . ($current_state == '' ? ' selected' : '') . '>any state</option>'; |
||
105 | |||
106 | foreach ($alert_states as $name => $val) { |
||
107 | $common_output[] = "<option value=\"$val\"" . ($current_state == $name || (is_numeric($current_state) && $current_state == $val) ? ' selected' : '') . ">$name</option>"; |
||
108 | } |
||
109 | |||
110 | $common_output[] = ' |
||
111 | </select> |
||
112 | </div> |
||
113 | </div> |
||
114 | <div class="form-group row"> |
||
115 | <div class="col-sm-4"> |
||
116 | <label for="group" class="control-label">Device Group:</label> |
||
117 | </div> |
||
118 | <div class="col-sm-8"> |
||
119 | <select class="form-control" name="group">'; |
||
120 | $common_output[] = '<option value=""' . ($current_group == '' ? ' selected' : '') . '>any group</option>'; |
||
121 | |||
122 | foreach (\App\Models\DeviceGroup::orderBy('name')->get(['id', 'name', 'desc']) as $group) { |
||
123 | $common_output[] = "<option value=\"$group->id\"" . (is_numeric($current_group) && $current_group == $group->id ? ' selected' : '') . '>' . $group->name . ' - ' . $group->desc . '</option>'; |
||
124 | } |
||
125 | $common_output[] = ' |
||
126 | </select> |
||
127 | </div> |
||
128 | </div> |
||
129 | <div class="form-group row"> |
||
130 | <div class="col-sm-4"> |
||
131 | <label for="proc" class="control-label">Show Procedure field: </label> |
||
132 | </div> |
||
133 | <div class="col-sm-8"> |
||
134 | <select class="form-control" name="proc">'; |
||
135 | |||
136 | $common_output[] = '<option value="1"' . ($current_proc == '1' ? ' selected' : ' ') . '>show</option>'; |
||
137 | $common_output[] = '<option value="0"' . ($current_proc == '0' ? ' selected' : ' ') . '>hide</option>'; |
||
138 | |||
139 | $common_output[] = ' |
||
140 | </select> |
||
141 | </div> |
||
142 | </div> |
||
143 | <div class="form-group row"> |
||
144 | <div class="col-sm-4"> |
||
145 | <label for="sort" class="control-label">Sort alerts by: </label> |
||
146 | </div> |
||
147 | <div class="col-sm-8"> |
||
148 | <select class="form-control" name="sort">'; |
||
149 | $common_output[] = '<option value=""' . ($current_sorting == '' ? ' selected' : '') |
||
150 | . '>timestamp, descending</option>'; |
||
151 | $common_output[] = '<option value="severity"' . ($current_sorting == 'severity' ? ' selected' : ' ') |
||
152 | . '>severity, descending</option>'; |
||
153 | |||
154 | $common_output[] = ' |
||
155 | </select> |
||
156 | </div> |
||
157 | </div> |
||
158 | |||
159 | <div class="form-group"> |
||
160 | <div class="col-sm-12"> |
||
161 | <button type="submit" class="btn btn-default">Set</button> |
||
162 | </div> |
||
163 | </div> |
||
164 | </form> |
||
165 | '; |
||
166 | } else { |
||
167 | $device_id = $device['device_id']; |
||
168 | $acknowledged = $widget_settings['acknowledged']; |
||
169 | $fired = $widget_settings['fired']; |
||
170 | $state = $widget_settings['state']; |
||
171 | $min_severity = $widget_settings['min_severity']; |
||
172 | $group = $widget_settings['group']; |
||
173 | $proc = $widget_settings['proc']; |
||
174 | $sort = $widget_settings['sort']; |
||
175 | |||
176 | $title = 'Alerts'; |
||
177 | |||
178 | // state can be 0 or '', be sure they are treated differently |
||
179 | if (is_numeric($state)) { |
||
180 | $state_name = array_search($state, $alert_states); |
||
181 | $title = "$title ($state_name)"; |
||
182 | } elseif ($state) { |
||
183 | $title = "$title ($state)"; |
||
184 | } |
||
185 | |||
186 | if (is_numeric($acknowledged)) { |
||
187 | if ($acknowledged == '0') { |
||
188 | $title = "Unacknowledged $title"; |
||
189 | } elseif ($acknowledged == '1') { |
||
190 | $title = "Acknowledged $title"; |
||
191 | } |
||
192 | } |
||
193 | |||
194 | if (is_numeric($fired)) { |
||
195 | $title = "Fired $title"; |
||
196 | } |
||
197 | |||
198 | if (is_numeric($group)) { |
||
199 | $group_row = dbFetchRow('SELECT * FROM device_groups WHERE id = ?', [$group]); |
||
200 | if ($group_row) { |
||
201 | $title = "$title for " . $group_row['name']; |
||
202 | } |
||
203 | } |
||
204 | |||
205 | if ($min_severity) { |
||
206 | $sev_name = $min_severity; |
||
207 | if (is_numeric($min_severity)) { |
||
208 | $sev_name = array_search($min_severity, $alert_severities); |
||
209 | $title = "$title " . ($min_severity > 3 ? '' : '>') . "=$sev_name"; |
||
210 | } |
||
211 | } |
||
212 | |||
213 | if (! empty($sort)) { |
||
214 | $title = "$title " . 'sorted by severity (higher first)'; |
||
215 | } |
||
216 | |||
217 | $widget_settings['title'] = $title; |
||
218 | |||
219 | $group = $widget_settings['group']; |
||
220 | |||
221 | $common_output[] = ' |
||
222 | <div class="row"> |
||
223 | <div class="col-sm-12"> |
||
224 | <span id="message"></span> |
||
225 | </div> |
||
226 | </div> |
||
227 | <div class="table-responsive"> |
||
228 | <table id="alerts_' . $unique_id . '" class="table table-hover table-condensed alerts"> |
||
229 | <thead> |
||
230 | <tr> |
||
231 | <th data-column-id="severity"></th> |
||
232 | <th data-column-id="timestamp">Timestamp</th> |
||
233 | <th data-column-id="rule">Rule</th> |
||
234 | <th data-column-id="details" data-sortable="false"></th> |
||
235 | <th data-column-id="hostname">Hostname</th> |
||
236 | <th data-column-id="location">Location</th> |
||
237 | <th data-column-id="ack_ico" data-sortable="false">ACK</th> |
||
238 | <th data-column-id="notes" data-sortable="false">Notes</th> |
||
239 | ' . $admin_verbose_details . ''; |
||
240 | |||
241 | if ($proc == '1') { |
||
242 | $common_output[] = '<th data-column-id="proc" data-sortable="false">URL</th>'; |
||
243 | } |
||
244 | |||
245 | $common_output[] = ' |
||
246 | </tr> |
||
247 | </thead> |
||
248 | </table> |
||
249 | </div> |
||
250 | <script> |
||
251 | var alerts_grid = $("#alerts_' . $unique_id . '").bootgrid({ |
||
252 | ajax: true, |
||
253 | post: function () |
||
254 | { |
||
255 | return { |
||
256 | id: "alerts", |
||
257 | '; |
||
258 | |||
259 | if (is_numeric($acknowledged)) { |
||
260 | $common_output[] = "acknowledged: '$acknowledged',\n"; |
||
261 | } |
||
262 | if (is_numeric($fired)) { |
||
263 | $common_output[] = "fired: '$fired',\n"; |
||
264 | } |
||
265 | if (isset($state) && $state != '') { |
||
266 | $common_output[] = "state: '$state',\n"; |
||
267 | } |
||
268 | if (isset($min_severity) && $min_severity != '') { |
||
269 | $common_output[] = "min_severity: '$min_severity',\n"; |
||
270 | } |
||
271 | |||
272 | if (is_numeric($group)) { |
||
273 | $common_output[] = "group: '$group',\n"; |
||
274 | } |
||
275 | if (is_numeric($proc)) { |
||
276 | $common_output[] = "proc: '$proc',\n"; |
||
277 | } |
||
278 | |||
279 | if (isset($sort) && $sort != '') { |
||
280 | $common_output[] = "sort: '$sort',\n"; |
||
281 | } |
||
282 | |||
283 | $common_output[] = ' |
||
284 | device_id: \'' . $device['device_id'] . '\' |
||
285 | } |
||
286 | }, |
||
287 | url: "ajax_table.php", |
||
288 | rowCount: [50, 100, 250, -1], |
||
289 | |||
290 | }).on("loaded.rs.jquery.bootgrid", function() { |
||
291 | alerts_grid = $(this); |
||
292 | alerts_grid.find(".incident-toggle").each( function() { |
||
293 | $(this).parent().addClass(\'incident-toggle-td\'); |
||
294 | }).on("click", function(e) { |
||
295 | var target = $(this).data("target"); |
||
296 | $(target).collapse(\'toggle\'); |
||
297 | $(this).toggleClass(\'fa-plus fa-minus\'); |
||
298 | }); |
||
299 | alerts_grid.find(".incident").each( function() { |
||
300 | $(this).parent().addClass(\'col-lg-4 col-md-4 col-sm-4 col-xs-4\'); |
||
301 | $(this).parent().parent().on("mouseenter", function() { |
||
302 | $(this).find(".incident-toggle").fadeIn(200); |
||
303 | }).on("mouseleave", function() { |
||
304 | $(this).find(".incident-toggle").fadeOut(200); |
||
305 | }); |
||
306 | }); |
||
307 | alerts_grid.find(".command-ack-alert").on("click", function(e) { |
||
308 | e.preventDefault(); |
||
309 | var alert_state = $(this).data("alert_state"); |
||
310 | var alert_id = $(this).data(\'alert_id\'); |
||
311 | $(\'#ack_alert_id\').val(alert_id); |
||
312 | $(\'#ack_alert_state\').val(alert_state); |
||
313 | $(\'#ack_msg\').val(\'\'); |
||
314 | $("#alert_ack_modal").modal(\'show\'); |
||
315 | }); |
||
316 | alerts_grid.find(".command-alert-note").on("click", function(e) { |
||
317 | e.preventDefault(); |
||
318 | var alert_id = $(this).data(\'alert_id\'); |
||
319 | $(\'#alert_id\').val(alert_id); |
||
320 | $("#alert_notes_modal").modal(\'show\'); |
||
321 | }); |
||
322 | alerts_grid.find(".command-alert-details").on("click", function(e) { |
||
323 | e.preventDefault(); |
||
324 | var alert_log_id = $(this).data(\'alert_log_id\'); |
||
325 | $(\'#alert_log_id\').val(alert_log_id); |
||
326 | $("#alert_details_modal").modal(\'show\'); |
||
327 | }); |
||
328 | }); |
||
329 | </script>'; |
||
330 | } |
||
331 |