2015-03-30 12:16:56 +01:00
< ? php
2017-12-24 21:55:24 +02:00
/*
* 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
* @ link http :// librenms . org
* @ copyright 2017 LibreNMS
* @ author LibreNMS Contributors
*/
require_once $config [ 'install_dir' ] . '/includes/device-groups.inc.php' ;
2016-01-21 12:51:49 -07:00
2017-03-24 22:06:09 +00:00
$where = ' `devices`.`disabled` = 0' ;
2015-03-30 12:16:56 +01:00
2016-01-19 16:38:41 -07:00
$alert_states = array (
// divined from librenms/alerts.php
'recovered' => 0 ,
'alerted' => 1 ,
'acknowledged' => 2 ,
'worse' => 3 ,
2017-10-15 12:50:55 -06:00
'better' => 4 ,
2016-01-19 16:38:41 -07:00
);
$alert_severities = array (
// alert_rules.status is enum('ok','warning','critical')
'ok' => 1 ,
'warning' => 2 ,
2017-10-15 12:50:55 -06:00
'critical' => 3 ,
'ok only' => 4 ,
'warning only' => 5 ,
'critical only' => 6 ,
2016-01-19 16:38:41 -07:00
);
2016-08-18 20:28:22 -05:00
$show_recovered = false ;
2016-01-21 12:51:49 -07:00
2015-03-30 12:16:56 +01:00
if ( is_numeric ( $_POST [ 'device_id' ]) && $_POST [ 'device_id' ] > 0 ) {
2017-12-24 21:55:24 +02:00
$where .= ' AND `alerts`.`device_id`=' . $_POST [ 'device_id' ];
2015-03-30 12:16:56 +01:00
}
2016-01-19 16:38:41 -07:00
if ( is_numeric ( $_POST [ 'acknowledged' ])) {
// I assume that if we are searching for acknowleged/not, we aren't interested in recovered
2017-12-24 21:55:24 +02:00
$where .= " AND `alerts`.`state` " . ( $_POST [ 'acknowledged' ] ? " = " : " != " ) . $alert_states [ 'acknowledged' ];
2016-01-19 16:38:41 -07:00
}
if ( is_numeric ( $_POST [ 'state' ])) {
2017-12-24 21:55:24 +02:00
$where .= " AND `alerts`.`state`= " . $_POST [ 'state' ];
2016-01-21 12:51:49 -07:00
if ( $_POST [ 'state' ] == $alert_states [ 'recovered' ]) {
2016-08-18 20:28:22 -05:00
$show_recovered = true ;
2016-01-21 12:51:49 -07:00
}
2016-01-19 16:38:41 -07:00
}
if ( isset ( $_POST [ 'min_severity' ])) {
if ( is_numeric ( $_POST [ 'min_severity' ])) {
$min_severity_id = $_POST [ 'min_severity' ];
2016-08-18 20:28:22 -05:00
} elseif ( ! empty ( $_POST [ 'min_severity' ])) {
2016-01-19 16:38:41 -07:00
$min_severity_id = $alert_severities [ $_POST [ 'min_severity' ]];
}
if ( isset ( $min_severity_id )) {
2017-12-24 21:55:24 +02:00
$where .= " AND `alert_rules`.`severity` " . ( $min_severity_id > 3 ? " " : " > " ) . " = " . ( $min_severity_id > 3 ? $min_severity_id - 3 : $min_severity_id );
2016-01-19 16:38:41 -07:00
}
}
2016-01-21 12:51:49 -07:00
if ( is_numeric ( $_POST [ 'group' ])) {
2016-09-18 19:08:33 -05:00
$where .= " AND devices.device_id IN (SELECT `device_id` FROM `device_group_device` WHERE `device_group_id` = ?) " ;
$param [] = $_POST [ 'group' ];
2016-01-21 12:51:49 -07:00
}
if ( ! $show_recovered ) {
2017-12-24 21:55:24 +02:00
$where .= " AND `alerts`.`state`!= " . $alert_states [ 'recovered' ];
2016-01-21 12:51:49 -07:00
}
2015-03-30 12:16:56 +01:00
if ( isset ( $searchPhrase ) && ! empty ( $searchPhrase )) {
2016-01-21 12:51:49 -07:00
$where .= " AND (`timestamp` LIKE '% $searchPhrase %' OR `rule` LIKE '% $searchPhrase %' OR `name` LIKE '% $searchPhrase %' OR `hostname` LIKE '% $searchPhrase %') " ;
2015-03-30 12:16:56 +01:00
}
2015-07-13 20:10:26 +02:00
$sql = ' FROM `alerts` LEFT JOIN `devices` ON `alerts`.`device_id`=`devices`.`device_id`' ;
2015-07-07 23:18:57 +01:00
2015-07-13 20:10:26 +02:00
if ( is_admin () === false && is_read () === false ) {
2017-12-24 21:55:24 +02:00
$sql .= ' LEFT JOIN `devices_perms` AS `DP` ON `devices`.`device_id` = `DP`.`device_id`' ;
$where .= ' AND `DP`.`user_id`=?' ;
2015-07-07 23:18:57 +01:00
$param [] = $_SESSION [ 'user_id' ];
}
2016-01-21 12:51:49 -07:00
$sql .= " RIGHT JOIN `alert_rules` ON `alerts`.`rule_id`=`alert_rules`.`id` WHERE $where " ;
2015-03-30 12:16:56 +01:00
$count_sql = " SELECT COUNT(`alerts`.`id`) $sql " ;
2017-12-24 21:55:24 +02:00
$total = dbFetchCell ( $count_sql , $param );
2015-04-12 11:47:21 +01:00
if ( empty ( $total )) {
$total = 0 ;
}
2015-03-30 12:16:56 +01:00
if ( ! isset ( $sort ) || empty ( $sort )) {
$sort = 'timestamp DESC' ;
}
$sql .= " ORDER BY $sort " ;
if ( isset ( $current )) {
2017-12-24 21:55:24 +02:00
$limit_low = (( $current * $rowCount ) - ( $rowCount ));
2015-03-30 12:16:56 +01:00
$limit_high = $rowCount ;
}
if ( $rowCount != - 1 ) {
$sql .= " LIMIT $limit_low , $limit_high " ;
}
2016-06-08 10:10:34 -06:00
$sql = " SELECT `alerts`.*, `devices`.`hostname`, `devices`.`sysName`, `devices`.`hardware`, `devices`.`location`, `alert_rules`.`rule`, `alert_rules`.`name`, `alert_rules`.`severity` $sql " ;
2015-03-30 12:16:56 +01:00
2017-12-24 21:55:24 +02:00
$rulei = 0 ;
2015-04-19 23:19:13 +01:00
$format = $_POST [ 'format' ];
2015-07-13 20:10:26 +02:00
foreach ( dbFetchRows ( $sql , $param ) as $alert ) {
2017-12-24 21:55:24 +02:00
$log = dbFetchCell ( 'SELECT details FROM alert_log WHERE rule_id = ? AND device_id = ? ORDER BY id DESC LIMIT 1' , array ( $alert [ 'rule_id' ], $alert [ 'device_id' ]));
2015-05-17 11:44:33 +00:00
$fault_detail = alert_details ( $log );
2015-03-30 12:16:56 +01:00
2017-12-27 05:29:28 +00:00
$alert_to_ack = '<button type="button" class="btn btn-danger command-ack-alert fa fa-eye" aria-hidden="true" title="Mark as acknowledged" data-target="ack-alert" data-state="' . $alert [ 'state' ] . '" data-alert_id="' . $alert [ 'id' ] . '" name="ack-alert"></button>' ;
$alert_to_nack = '<button type="button" class="btn btn-warning command-ack-alert fa fa-eye-slash" aria-hidden="true" title="Mark as not acknowledged" data-target="ack-alert" data-state="' . $alert [ 'state' ] . '" data-alert_id="' . $alert [ 'id' ] . '" name="ack-alert"></button>' ;
$ack_ico = $alert_to_ack ;
2017-12-24 21:55:24 +02:00
if (( int ) $alert [ 'state' ] === 0 ) {
$ico = '' ;
$msg = '' ;
} elseif (( int ) $alert [ 'state' ] === 1 || ( int ) $alert [ 'state' ] === 3 || ( int ) $alert [ 'state' ] === 4 ) {
$ico = $alert_to_ack ;
if (( int ) $alert [ 'state' ] === 3 ) {
$msg = '<i class="fa fa-angle-double-down" style="font-size:20px;" aria-hidden="true" title="Status got worse"></i>' ;
} elseif (( int ) $alert [ 'state' ] === 4 ) {
$msg = '<i class="fa fa-angle-double-up" style="font-size:20px;" aria-hidden="true" title="Status got better"></i>' ;
2015-03-31 18:47:10 +01:00
}
2017-12-24 21:55:24 +02:00
} elseif (( int ) $alert [ 'state' ] === 2 ) {
$ico = $alert_to_nack ;
2017-12-27 05:29:28 +00:00
$ack_ico = $alert_to_nack ;
2017-12-24 21:55:24 +02:00
}
2015-03-30 12:16:56 +01:00
$severity = $alert [ 'severity' ];
if ( $alert [ 'state' ] == 3 ) {
2015-07-13 20:10:26 +02:00
$severity .= ' <strong>+</strong>' ;
2016-08-18 20:28:22 -05:00
} elseif ( $alert [ 'state' ] == 4 ) {
2015-07-13 20:10:26 +02:00
$severity .= ' <strong>-</strong>' ;
2015-03-30 12:16:56 +01:00
}
2017-12-24 21:55:24 +02:00
$hostname = '<div class="incident">' . generate_device_link ( $alert , shorthost ( $alert [ 'hostname' ])) . '<div id="incident' . ( $rulei + 1 ) . '" class="collapse">' . $fault_detail . '</div></div>' ;
switch ( $severity ) {
case 'critical' :
$severity_ico = '<span class="alert-status label-danger"> </span>' ;
break ;
case 'warning' :
$severity_ico = '<span class="alert-status label-warning"> </span>' ;
break ;
case 'ok' :
$severity_ico = '<span class="alert-status label-success"> </span>' ;
break ;
default :
$severity_ico = '<span class="alert-status label-info"> </span>' ;
break ;
2015-07-13 20:10:26 +02:00
}
2015-03-30 12:16:56 +01:00
2017-12-27 05:29:28 +00:00
if (( int ) $alert [ 'state' ] === 2 ) {
$severity_ico = '<span class="alert-status label-warning"> </span>' ;
}
2017-12-24 21:55:24 +02:00
$proc = dbFetchCell ( 'SELECT proc FROM alerts,alert_rules WHERE alert_rules.id = alerts.rule_id AND alerts.id = ?' , array ( $alert [ 'id' ]));
if (( $proc == " " ) || ( $proc == " NULL " )) {
$has_proc = '' ;
} else {
if ( ! preg_match ( '/^http[s]*:\/\//' , $proc )) {
$has_proc = '' ;
} else {
$has_proc = '<a href="' . $proc . '" target="_blank"><button type="button" class="btn btn-info fa fa-external-link" aria-hidden="true"></button></a>' ;
}
}
2015-07-13 20:10:26 +02:00
$response [] = array (
2017-12-24 21:55:24 +02:00
'id' => $rulei ++ ,
'rule' => '<i title="' . htmlentities ( $alert [ 'rule' ]) . '"><a href="' . generate_url ( array ( 'page' => 'alert-rules' )) . '">' . htmlentities ( $alert [ 'name' ]) . '</a></i>' ,
'details' => '<a class="fa fa-plus incident-toggle" style="display:none" data-toggle="collapse" data-target="#incident' . ( $rulei ) . '" data-parent="#alerts"></a>' ,
'hostname' => $hostname ,
2015-07-13 20:10:26 +02:00
'timestamp' => ( $alert [ 'timestamp' ] ? $alert [ 'timestamp' ] : 'N/A' ),
2017-12-24 21:55:24 +02:00
'severity' => $severity_ico ,
'state' => $alert [ 'state' ],
'alert_id' => $alert [ 'id' ],
'ack_ico' => $ack_ico ,
'proc' => $has_proc ,
2015-07-13 20:10:26 +02:00
);
2017-12-24 21:55:24 +02:00
}
2015-07-13 20:10:26 +02:00
$output = array (
2017-12-24 21:55:24 +02:00
'current' => $current ,
2015-07-13 20:10:26 +02:00
'rowCount' => $rowCount ,
2017-12-24 21:55:24 +02:00
'rows' => $response ,
'total' => $total ,
2015-07-13 20:10:26 +02:00
);
2015-03-30 12:16:56 +01:00
echo _json_encode ( $output );