2015-03-30 12:16:56 +01:00
< ? php
$where = 1 ;
if ( is_numeric ( $_POST [ 'device_id' ]) && $_POST [ 'device_id' ] > 0 ) {
$where .= ' AND `alerts`.`device_id`=' . $_POST [ 'device_id' ];
}
if ( isset ( $searchPhrase ) && ! empty ( $searchPhrase )) {
2015-04-19 23:19:13 +01:00
$sql .= " AND (`timestamp` LIKE '% $searchPhrase %' OR `rule` LIKE '% $searchPhrase %' OR `name` LIKE '% $searchPhrase %' OR `hostname` LIKE '% $searchPhrase %') " ;
2015-03-30 12:16:56 +01:00
}
$sql = " FROM `alerts` LEFT JOIN `devices` ON `alerts`.`device_id`=`devices`.`device_id` RIGHT JOIN alert_rules ON alerts.rule_id=alert_rules.id WHERE $where AND `state` IN (1,2,3,4) $sql " ;
$count_sql = " SELECT COUNT(`alerts`.`id`) $sql " ;
$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 )) {
$limit_low = ( $current * $rowCount ) - ( $rowCount );
$limit_high = $rowCount ;
}
if ( $rowCount != - 1 ) {
$sql .= " LIMIT $limit_low , $limit_high " ;
}
$sql = " SELECT `alerts`.*, `devices`.`hostname` AS `hostname`,`alert_rules`.`rule` AS `rule`, `alert_rules`.`name` AS `name`, `alert_rules`.`severity` AS `severity` $sql " ;
$rulei = 0 ;
2015-04-19 23:19:13 +01:00
$format = $_POST [ 'format' ];
2015-03-30 12:16:56 +01:00
foreach ( dbFetchRows ( $sql , $param ) as $alert ) {
$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
$ico = " ok " ;
$col = " green " ;
$extra = " " ;
2015-03-31 18:47:10 +01:00
$msg = " " ;
2015-03-30 12:16:56 +01:00
if ( ( int ) $alert [ 'state' ] === 0 ) {
$ico = " ok " ;
$col = " green " ;
2015-03-31 18:47:10 +01:00
$extra = " success " ;
2015-04-29 01:31:52 +01:00
$msg = " ok " ;
2015-03-31 18:47:10 +01:00
} elseif ( ( int ) $alert [ 'state' ] === 1 || ( int ) $alert [ 'state' ] === 3 || ( int ) $alert [ 'state' ] === 4 ) {
2015-03-30 12:16:56 +01:00
$ico = " volume-up " ;
$col = " red " ;
$extra = " danger " ;
2015-04-29 01:31:52 +01:00
$msg = " alert " ;
2015-03-31 18:47:10 +01:00
if ( ( int ) $alert [ 'state' ] === 3 ) {
2015-04-29 01:31:52 +01:00
$msg = " worse " ;
2015-03-31 18:47:10 +01:00
} elseif ( ( int ) $alert [ 'state' ] === 4 ) {
2015-04-29 01:31:52 +01:00
$msg = " better " ;
2015-03-31 18:47:10 +01:00
}
} elseif ( ( int ) $alert [ 'state' ] === 2 ) {
2015-03-30 12:16:56 +01:00
$ico = " volume-off " ;
$col = " #800080 " ;
$extra = " warning " ;
2015-04-29 01:31:52 +01:00
$msg = " muted " ;
2015-03-30 12:16:56 +01:00
}
$alert_checked = '' ;
$orig_ico = $ico ;
$orig_col = $col ;
$orig_class = $extra ;
$severity = $alert [ 'severity' ];
if ( $alert [ 'state' ] == 3 ) {
$severity .= " <strong>+</strong> " ;
} elseif ( $alert [ 'state' ] == 4 ) {
$severity .= " <strong>-</strong> " ;
}
if ( $_SESSION [ 'userlevel' ] >= '10' ) {
$ack_ico = 'volume-up' ;
$ack_col = 'success' ;
2015-04-24 19:05:55 +00:00
if ( $alert [ 'state' ] == 2 ) {
2015-03-30 12:16:56 +01:00
$ack_ico = 'volume-off' ;
$ack_col = 'danger' ;
}
}
2015-04-23 15:07:55 +00:00
$hostname = '
< div class = " incident " >
'.generate_device_link($alert).'
< div id = " incident'.( $rulei +1).' " class = " collapse " > '.$fault_detail.' </ div >
</ div > ' ;
$response [] = array ( 'id' => $rulei ++ ,
2015-04-08 03:01:15 +01:00
'rule' => " <i title= \" " . htmlentities ( $alert [ 'rule' ]) . " \" > " . htmlentities ( $alert [ 'name' ]) . " </i> " ,
2015-04-23 15:07:55 +00:00
'details' => '<a class="glyphicon glyphicon-plus incident-toggle" style="display:none" data-toggle="collapse" data-target="#incident' . ( $rulei ) . '" data-parent="#alerts"></a>' ,
2015-04-19 23:19:13 +01:00
'hostname' => $hostname ,
2015-03-30 12:16:56 +01:00
'timestamp' => ( $alert [ 'timestamp' ] ? $alert [ 'timestamp' ] : " N/A " ),
'severity' => $severity ,
'ack_col' => $ack_col ,
'state' => $alert [ 'state' ],
'alert_id' => $alert [ 'id' ],
'ack_ico' => $ack_ico ,
2015-03-31 18:47:10 +01:00
'extra' => $extra ,
'msg' => $msg );
2015-03-30 12:16:56 +01:00
}
$output = array ( 'current' => $current , 'rowCount' => $rowCount , 'rows' => $response , 'total' => $total );
echo _json_encode ( $output );