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 2018 LibreNMS |
||
13 | * @author LibreNMS Contributors |
||
14 | */ |
||
15 | |||
16 | $where = 1; |
||
17 | |||
18 | if (is_numeric($vars['device_id'])) { |
||
19 | $where .= ' AND E.device_id = ?'; |
||
20 | $param[] = $vars['device_id']; |
||
21 | } |
||
22 | |||
23 | $where .= ' AND `E`.`state` = 1'; // state 1 => alert |
||
24 | |||
25 | if (is_numeric($vars['time_interval'])) { |
||
26 | $where .= ' AND E.`time_logged` > DATE_SUB(NOW(),INTERVAL ? DAY)'; |
||
27 | $param[] = $vars['time_interval']; |
||
28 | } |
||
29 | |||
30 | if (isset($vars['min_severity'])) { |
||
31 | $where .= get_sql_filter_min_severity($vars['min_severity'], 'R'); |
||
32 | } |
||
33 | |||
34 | if (Auth::user()->hasGlobalRead()) { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
35 | $sql = " FROM `alert_log` AS E LEFT JOIN devices AS D ON E.device_id=D.device_id RIGHT JOIN alert_rules AS R ON E.rule_id=R.id WHERE $where"; |
||
36 | } else { |
||
37 | $sql = " FROM `alert_log` AS E LEFT JOIN devices AS D ON E.device_id=D.device_id RIGHT JOIN alert_rules AS R ON E.rule_id=R.id RIGHT JOIN devices_perms AS P ON E.device_id = P.device_id WHERE $where AND P.user_id = ?"; |
||
38 | $param[] = [Auth::id()]; |
||
39 | } |
||
40 | |||
41 | if (isset($searchPhrase) && ! empty($searchPhrase)) { |
||
42 | $sql .= ' AND (`D`.`hostname` LIKE ? OR `D`.`sysName` LIKE ? OR `E`.`time_logged` LIKE ? OR `name` LIKE ?)'; |
||
43 | $param[] = "%$searchPhrase%"; |
||
44 | $param[] = "%$searchPhrase%"; |
||
45 | $param[] = "%$searchPhrase%"; |
||
46 | $param[] = "%$searchPhrase%"; |
||
47 | } |
||
48 | |||
49 | $count_sql = "SELECT COUNT(DISTINCT D.sysname, R.name) $sql"; |
||
50 | $total = dbFetchCell($count_sql, $param); |
||
51 | if (empty($total)) { |
||
52 | $total = 0; |
||
53 | } |
||
54 | |||
55 | $sql .= ' GROUP BY D.device_id, R.name ORDER BY COUNT(*) DESC'; |
||
56 | |||
57 | if (isset($current)) { |
||
58 | $limit_low = (($current * $rowCount) - ($rowCount)); |
||
59 | $limit_high = $rowCount; |
||
60 | } |
||
61 | |||
62 | if ($rowCount != -1) { |
||
63 | $sql .= " LIMIT $limit_low,$limit_high"; |
||
64 | } |
||
65 | |||
66 | $sql = "SELECT COUNT(*), D.device_id, R.name $sql"; |
||
67 | |||
68 | $rulei = 0; |
||
69 | foreach (dbFetchRows($sql, $param) as $alertlog) { |
||
70 | $dev = device_by_id_cache($alertlog['device_id']); |
||
71 | |||
72 | $response[] = [ |
||
73 | 'id' => $rulei++, |
||
74 | 'count' => $alertlog['COUNT(*)'], |
||
75 | 'hostname' => '<div class="incident">' . generate_device_link($dev), |
||
76 | 'alert_rule' => $alertlog['name'], |
||
77 | ]; |
||
78 | }//end foreach |
||
79 | |||
80 | $output = [ |
||
81 | 'current' => $current, |
||
82 | 'rowCount' => $rowCount, |
||
83 | 'rows' => $response, |
||
84 | 'total' => $total, |
||
85 | ]; |
||
86 | echo json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
||
87 |