2020-01-23 15:16:30 +01:00
< ? php
/*
* This program is free software : you can redistribute it and / or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation , either version 3 of the License , or ( at your
* option ) any later version . Please see LICENSE . txt at the top level of
* the source code distribution for details .
*
* @ package LibreNMS
* @ subpackage graphs
2021-02-09 00:29:04 +01:00
* @ link https :// www . librenms . org
2020-01-23 15:16:30 +01:00
* @ copyright 2018 LibreNMS
* @ author LibreNMS Contributors
*/
$where = 1 ;
if ( is_numeric ( $vars [ 'device_id' ])) {
$where .= ' AND E.device_id = ?' ;
$param [] = $vars [ 'device_id' ];
}
$where .= ' AND `E`.`state` = 1' ; // state 1 => alert
if ( is_numeric ( $vars [ 'time_interval' ])) {
$where .= ' AND E.`time_logged` > DATE_SUB(NOW(),INTERVAL ? DAY)' ;
$param [] = $vars [ 'time_interval' ];
}
if ( isset ( $vars [ 'min_severity' ])) {
$where .= get_sql_filter_min_severity ( $vars [ 'min_severity' ], 'R' );
}
if ( Auth :: user () -> hasGlobalRead ()) {
$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 " ;
} else {
2024-09-24 22:18:55 +01:00
$device_ids = Permissions :: devicesForUser () -> toArray () ? : [ 0 ];
// @phpstan-ignore-next-line
$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 AND E.device_id IN " . dbGenPlaceholders ( count ( $device_ids ));
$param = array_merge ( $param , $device_ids );
2020-01-23 15:16:30 +01:00
}
if ( isset ( $searchPhrase ) && ! empty ( $searchPhrase )) {
2020-07-10 16:17:09 +02:00
$sql .= ' AND (`D`.`hostname` LIKE ? OR `D`.`sysName` LIKE ? OR `E`.`time_logged` LIKE ? OR `name` LIKE ?)' ;
$param [] = " % $searchPhrase % " ;
$param [] = " % $searchPhrase % " ;
$param [] = " % $searchPhrase % " ;
$param [] = " % $searchPhrase % " ;
2020-01-23 15:16:30 +01:00
}
$count_sql = " SELECT COUNT(DISTINCT D.sysname, R.name) $sql " ;
$total = dbFetchCell ( $count_sql , $param );
if ( empty ( $total )) {
$total = 0 ;
}
$sql .= ' GROUP BY D.device_id, R.name ORDER BY COUNT(*) DESC' ;
if ( isset ( $current )) {
2023-03-13 22:32:22 +01:00
$limit_low = (( $current * $rowCount ) - $rowCount );
2020-01-23 15:16:30 +01:00
$limit_high = $rowCount ;
}
if ( $rowCount != - 1 ) {
$sql .= " LIMIT $limit_low , $limit_high " ;
}
$sql = " SELECT COUNT(*), D.device_id, R.name $sql " ;
$rulei = 0 ;
foreach ( dbFetchRows ( $sql , $param ) as $alertlog ) {
$dev = device_by_id_cache ( $alertlog [ 'device_id' ]);
$response [] = [
'id' => $rulei ++ ,
'count' => $alertlog [ 'COUNT(*)' ],
2021-07-04 20:58:05 +02:00
'hostname' => '<div class="incident">' . generate_device_link ( $dev ),
2020-01-23 15:16:30 +01:00
'alert_rule' => $alertlog [ 'name' ],
];
} //end foreach
$output = [
'current' => $current ,
'rowCount' => $rowCount ,
'rows' => $response ,
'total' => $total ,
];
2021-03-04 07:55:41 -06:00
echo json_encode ( $output , JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );