2015-03-21 21:30:55 +00:00
< ? php
2018-03-14 20:25:19 +00:00
use LibreNMS\Alerting\QueryBuilderParser ;
2015-07-13 20:10:26 +02:00
$no_refresh = true ;
2015-03-21 21:30:55 +00:00
?>
2014-11-30 17:49:52 +00:00
< div class = " row " >
< div class = " col-sm-12 " >
< span id = " message " ></ span >
</ div >
</ div >
< ? php
2015-07-13 20:10:26 +02:00
if ( isset ( $_POST [ 'create-default' ])) {
2017-05-13 03:24:47 -05:00
$default_rules = array_filter ( get_rules_from_json (), function ( $rule ) {
return isset ( $rule [ 'default' ]) && $rule [ 'default' ];
});
$default_extra = array (
'mute' => false ,
'count' => - 1 ,
'delay' => 300 ,
'invert' => false ,
'interval' => 300 ,
2016-07-03 19:47:12 +01:00
);
2017-05-13 03:24:47 -05:00
2015-07-13 20:10:26 +02:00
foreach ( $default_rules as $add_rule ) {
2017-05-13 03:24:47 -05:00
$extra = $default_extra ;
if ( isset ( $add_rule [ 'extra' ])) {
$extra = array_replace ( $extra , json_decode ( $add_rule [ 'extra' ], true ));
}
2018-03-14 20:25:19 +00:00
$qb = QueryBuilderParser :: fromOld ( $add_rule [ 'rule' ]);
2017-05-13 03:24:47 -05:00
$insert = array (
2018-03-14 20:25:19 +00:00
'rule' => '' ,
'builder' => json_encode ( $qb ),
'query' => $qb -> toSql (),
'severity' => 'critical' ,
'extra' => json_encode ( $extra ),
'disabled' => 0 ,
'name' => $add_rule [ 'name' ]
2017-05-13 03:24:47 -05:00
);
dbInsert ( $insert , 'alert_rules' );
2014-12-22 16:58:58 +00:00
}
2018-03-14 20:25:19 +00:00
unset ( $qb );
}
2014-11-30 17:49:52 +00:00
2019-04-11 23:26:42 -05:00
require_once 'includes/html/modal/new_alert_rule.inc.php' ;
require_once 'includes/html/modal/delete_alert_rule.inc.php' ;
require_once 'includes/html/modal/alert_rule_collection.inc.php' ;
2014-11-30 17:49:52 +00:00
?>
< form method = " post " action = " " id = " result_form " >
< ? php
2019-07-17 07:20:26 -05:00
echo csrf_field ();
2015-07-13 20:10:26 +02:00
if ( isset ( $_POST [ 'results_amount' ]) && $_POST [ 'results_amount' ] > 0 ) {
2014-11-30 17:49:52 +00:00
$results = $_POST [ 'results' ];
2016-08-18 20:28:22 -05:00
} else {
2014-11-30 17:49:52 +00:00
$results = 50 ;
}
echo ' < div class = " table-responsive " >
2015-07-13 20:10:26 +02:00
< table class = " table table-hover table-condensed " width = " 100% " >
< tr >
2014-11-30 17:49:52 +00:00
< th > #</th>
2014-12-21 15:44:50 +00:00
< th > Name </ th >
2014-11-30 17:49:52 +00:00
< th > Rule </ th >
< th > Severity </ th >
< th > Status </ th >
< th > Extra </ th >
< th > Enabled </ th >
2018-03-14 20:25:19 +00:00
< th style = " width:86px; " > Action </ th >
2015-07-13 20:10:26 +02:00
</ tr > ' ;
2014-11-30 17:49:52 +00:00
2015-07-13 20:10:26 +02:00
echo '<td colspan="7">' ;
2019-08-05 14:16:05 -05:00
if ( Auth :: user () -> hasGlobalAdmin ()) {
2015-09-16 18:38:09 +05:30
echo '<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#create-alert" data-device_id="' . $device [ 'device_id' ] . '"><i class="fa fa-plus"></i> Create new alert rule</button>' ;
2016-10-26 06:54:48 +00:00
echo '<i> - OR - </i>' ;
echo '<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#search_rule_modal" data-device_id="' . $device [ 'device_id' ] . '"><i class="fa fa-plus"></i> Create rule from collection</button>' ;
2014-11-30 17:49:52 +00:00
}
2015-07-13 20:10:26 +02:00
echo ' </ td >
< td >< select name = " results " id = " results " class = " form-control input-sm " onChange = " updateResults(this); " > ' ;
$result_options = array (
'10' ,
'50' ,
'100' ,
'250' ,
'500' ,
'1000' ,
'5000' ,
);
foreach ( $result_options as $option ) {
2014-11-30 17:49:52 +00:00
echo " <option value=' $option ' " ;
2015-07-13 20:10:26 +02:00
if ( $results == $option ) {
echo ' selected' ;
2014-11-30 17:49:52 +00:00
}
2015-07-13 20:10:26 +02:00
2014-11-30 17:49:52 +00:00
echo " > $option </option> " ;
}
2015-07-13 20:10:26 +02:00
echo '</select></td>' ;
2018-03-14 20:25:19 +00:00
$param = [];
2015-07-13 20:10:26 +02:00
if ( isset ( $device [ 'device_id' ]) && $device [ 'device_id' ] > 0 ) {
2019-11-27 11:32:09 +01:00
//device selected
$global_rules = " SELECT ar1.* FROM alert_rules AS ar1 WHERE ar1.id NOT IN (SELECT agm1.rule_id FROM alert_group_map AS agm1 UNION DISTINCT SELECT adm1.rule_id FROM alert_device_map AS adm1) " ;
$device_rules = " SELECT ar2.* FROM alert_rules AS ar2 WHERE ar2.id IN (SELECT adm2.rule_id FROM alert_device_map AS adm2 WHERE adm2.device_id=?) " ;
$param [] = $device [ 'device_id' ];
$device_group_rules = " SELECT ar3.* FROM alert_rules AS ar3 WHERE ar3.id IN (SELECT agm3.rule_id FROM alert_group_map AS agm3 LEFT JOIN device_group_device AS dgd3 ON agm3.group_id=dgd3.device_group_id WHERE dgd3.device_id=?) " ;
2018-03-14 20:25:19 +00:00
$param [] = $device [ 'device_id' ];
2019-11-27 11:32:09 +01:00
$full_query = '(' . $global_rules . ') UNION DISTINCT (' . $device_rules . ') UNION DISTINCT (' . $device_group_rules . ')' ;
} else {
// no device selected
$full_query = 'SELECT alert_rules.* FROM alert_rules' ;
2014-11-30 17:49:52 +00:00
}
2015-07-13 20:10:26 +02:00
2019-11-27 11:32:09 +01:00
$full_query .= ' ORDER BY id ASC' ;
$rule_list = dbFetchRows ( $full_query , $param );
$count = count ( $rule_list );
2018-03-14 20:25:19 +00:00
if ( isset ( $_POST [ 'page_number' ]) && $_POST [ 'page_number' ] > 0 && $_POST [ 'page_number' ] <= $count ) {
2014-11-30 17:49:52 +00:00
$page_number = $_POST [ 'page_number' ];
2018-03-14 20:25:19 +00:00
} else {
$page_number = 1 ;
2014-11-30 17:49:52 +00:00
}
2015-07-13 20:10:26 +02:00
2018-03-14 20:25:19 +00:00
$start = (( $page_number - 1 ) * $results );
2015-07-13 20:10:26 +02:00
2019-11-27 11:32:09 +01:00
$index = 0 ;
foreach ( $rule_list as $rule ) {
$index ++ ;
if ( $index < $start ) {
continue ;
}
if ( $index > $start + $results ) {
break ;
}
2016-06-20 23:23:17 +01:00
$sub = dbFetchRows ( 'SELECT * FROM alerts WHERE rule_id = ? ORDER BY `state` DESC, `id` DESC LIMIT 1' , array ( $rule [ 'id' ]));
2016-09-22 10:44:17 -04:00
$ico = 'check' ;
2015-07-13 20:10:26 +02:00
$col = 'success' ;
$extra = '' ;
if ( sizeof ( $sub ) == 1 ) {
$sub = $sub [ 0 ];
if (( int ) $sub [ 'state' ] === 0 ) {
2016-09-22 10:44:17 -04:00
$ico = 'check' ;
2015-07-13 20:10:26 +02:00
$col = 'success' ;
2016-08-18 20:28:22 -05:00
} elseif (( int ) $sub [ 'state' ] === 1 || ( int ) $sub [ 'state' ] === 2 ) {
2018-05-24 14:14:25 -05:00
$ico = 'exclamation' ;
2015-07-13 20:10:26 +02:00
$col = 'danger' ;
$extra = 'danger' ;
2014-11-30 17:49:52 +00:00
}
2015-07-13 20:10:26 +02:00
}
$alert_checked = '' ;
$orig_ico = $ico ;
$orig_col = $col ;
$orig_class = $extra ;
if ( $rule [ 'disabled' ]) {
$ico = 'pause' ;
$col = '' ;
$extra = 'active' ;
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
$alert_checked = 'checked' ;
}
2014-12-22 16:58:58 +00:00
2015-07-13 20:10:26 +02:00
$rule_extra = json_decode ( $rule [ 'extra' ], true );
2018-03-14 20:25:19 +00:00
$device_count = dbFetchCell ( 'SELECT COUNT(*) FROM alert_device_map WHERE rule_id=?' , [ $rule [ 'id' ]]);
$group_count = dbFetchCell ( 'SELECT COUNT(*) FROM alert_group_map WHERE rule_id=?' , [ $rule [ 'id' ]]);
if ( $device_count && $group_count ) {
$popover_msg = 'Restricted rule' ;
$icon_indicator = 'fa fa-connectdevelop fa-fw text-primary' ;
} elseif ( $device_count ) {
$popover_msg = 'Device restricted rule' ;
$icon_indicator = 'fa fa-server fa-fw text-primary' ;
} elseif ( $group_count ) {
$popover_msg = 'Group restricted rule' ;
$icon_indicator = 'fa fa-th fa-fw text-primary' ;
2016-08-18 20:28:22 -05:00
} else {
2018-03-14 20:25:19 +00:00
$popover_msg = 'Global alert rule' ;
$icon_indicator = 'fa fa-globe fa-fw text-success' ;
2015-07-19 18:52:39 +01:00
}
2018-03-14 20:25:19 +00:00
2015-07-13 20:10:26 +02:00
echo " <tr class=' " . $extra . " ' id='row_ " . $rule [ 'id' ] . " '> " ;
2018-03-14 20:25:19 +00:00
echo " <td><i># " . (( int ) $rule [ 'id' ]) . " </i><br /><i class= \" $icon_indicator\ " ></ i ></ td > " ;
2015-07-13 20:10:26 +02:00
echo '<td>' . $rule [ 'name' ] . '</td>' ;
echo " <td class='col-sm-4'> " ;
if ( $rule_extra [ 'invert' ] === true ) {
echo '<strong><em>Inverted</em></strong> ' ;
}
2018-03-14 20:25:19 +00:00
if ( empty ( $rule [ 'builder' ])) {
$rule_display = $rule [ 'rule' ];
2018-09-19 13:47:45 +01:00
} elseif ( $rule_extra [ 'options' ][ 'override_query' ] === 'on' ) {
$rule_display = 'Custom SQL Query' ;
2018-03-14 20:25:19 +00:00
} else {
$rule_display = QueryBuilderParser :: fromJson ( $rule [ 'builder' ]) -> toSql ( false );
}
echo '<i>' . htmlentities ( $rule_display ) . '</i></td>' ;
2015-07-13 20:10:26 +02:00
echo '<td>' . $rule [ 'severity' ] . '</td>' ;
2016-09-22 14:45:41 -04:00
echo " <td><span id='alert-rule- " . $rule [ 'id' ] . " ' class='fa fa-fw fa-2x fa- " . $ico . ' text-' . $col . " '></span> " ;
2015-07-13 20:10:26 +02:00
if ( $rule_extra [ 'mute' ] === true ) {
2016-09-22 14:45:41 -04:00
echo " <i class='fa fa-fw fa-2x fa-volume-off text-primary' aria-hidden='true'></i></td> " ;
2015-07-13 20:10:26 +02:00
}
echo '<td><small>Max: ' . $rule_extra [ 'count' ] . '<br />Delay: ' . $rule_extra [ 'delay' ] . '<br />Interval: ' . $rule_extra [ 'interval' ] . '</small></td>' ;
echo '<td>' ;
2019-08-05 14:16:05 -05:00
if ( Auth :: user () -> hasGlobalAdmin ()) {
2015-07-19 18:52:39 +01:00
echo " <input id=' " . $rule [ 'id' ] . " ' type='checkbox' name='alert-rule' data-orig_class=' " . $orig_class . " ' data-orig_colour=' " . $orig_col . " ' data-orig_state=' " . $orig_ico . " ' data-alert_id=' " . $rule [ 'id' ] . " ' " . $alert_checked . " data-size='small' data-content=' " . $popover_msg . " ' data-toggle='modal'> " ;
2015-07-13 20:10:26 +02:00
}
echo '</td>' ;
echo '<td>' ;
2019-08-05 14:16:05 -05:00
if ( Auth :: user () -> hasGlobalAdmin ()) {
2016-09-22 10:44:17 -04:00
echo " <div class='btn-group btn-group-sm' role='group'> " ;
2018-03-14 20:25:19 +00:00
echo " <button type='button' class='btn btn-primary' data-toggle='modal' data-target='#create-alert' data-rule_id=' " . $rule [ 'id' ] . " ' name='edit-alert-rule' data-content=' " . $popover_msg . " ' data-container='body'><i class='fa fa-lg fa-pencil' aria-hidden='true'></i></button> " ;
2016-09-22 13:44:14 -04:00
echo " <button type='button' class='btn btn-danger' aria-label='Delete' data-toggle='modal' data-target='#confirm-delete' data-alert_id=' " . $rule [ 'id' ] . " ' name='delete-alert-rule' data-content=' " . $popover_msg . " ' data-container='body'><i class='fa fa-lg fa-trash' aria-hidden='true'></i></button> " ;
2015-07-13 20:10:26 +02:00
}
echo '</td>' ;
echo " </tr> \r \n " ;
} //end foreach
if (( $count % $results ) > 0 ) {
echo ' < tr >
< td colspan = " 8 " align = " center " > '.generate_pagination($count, $results, $page_number).' </ td >
</ tr > ' ;
2014-11-30 17:49:52 +00:00
}
2015-07-13 20:10:26 +02:00
2014-11-30 17:49:52 +00:00
echo ' </ table >
2015-07-13 20:10:26 +02:00
< input type = " hidden " name = " page_number " id = " page_number " value = " '. $page_number .' " >
< input type = " hidden " name = " results_amount " id = " results_amount " value = " '. $results .' " >
</ form >
</ div > ' ;
2014-12-22 16:58:58 +00:00
2015-07-13 20:10:26 +02:00
if ( $count < 1 ) {
2019-08-05 14:16:05 -05:00
if ( Auth :: user () -> hasGlobalAdmin ()) {
2014-12-23 15:56:17 +00:00
echo ' < div class = " row " >
2015-07-13 20:10:26 +02:00
< div class = " col-sm-12 " >
< form role = " form " method = " post " >
2019-07-17 07:20:26 -05:00
' . csrf_field() . '
2015-07-13 20:10:26 +02:00
< p class = " text-center " >
2016-10-21 09:53:20 -05:00
< button type = " submit " class = " btn btn-success btn-lg " id = " create-default " name = " create-default " >< i class = " fa fa-plus " ></ i > Click here to create the default alert rules !</ button >
2015-07-13 20:10:26 +02:00
</ p >
</ form >
</ div >
</ div > ' ;
2014-12-23 15:56:39 +00:00
}
2014-12-22 16:58:58 +00:00
}
2014-11-30 17:49:52 +00:00
?>
< script >
2015-07-19 18:52:39 +01:00
$ ( " [data-toggle='modal'], [data-toggle='popover'] " ) . popover ({
trigger : 'hover' ,
'placement' : 'top'
});
2014-11-30 17:49:52 +00:00
$ ( '#ack-alert' ) . click ( '' , function ( e ) {
event . preventDefault ();
var alert_id = $ ( this ) . data ( " alert_id " );
$ . ajax ({
type : " POST " ,
2015-08-11 14:54:05 -07:00
url : " ajax_form.php " ,
2015-07-13 20:10:26 +02:00
data : { type : " ack-alert " , alert_id : alert_id },
success : function ( msg ){
$ ( " #message " ) . html ( '<div class="alert alert-info">' + msg + '</div>' );
if ( msg . indexOf ( " ERROR: " ) <= - 1 ) {
setTimeout ( function () {
location . reload ( 1 );
}, 1000 );
}
},
error : function (){
$ ( " #message " ) . html ( '<div class="alert alert-info">An error occurred acking this alert.</div>' );
}
2014-11-30 17:49:52 +00:00
});
});
$ ( " [name='alert-rule'] " ) . bootstrapSwitch ( 'offColor' , 'danger' );
$ ( 'input[name="alert-rule"]' ) . on ( 'switchChange.bootstrapSwitch' , function ( event , state ) {
event . preventDefault ();
var $this = $ ( this );
var alert_id = $ ( this ) . data ( " alert_id " );
var orig_state = $ ( this ) . data ( " orig_state " );
var orig_colour = $ ( this ) . data ( " orig_colour " );
var orig_class = $ ( this ) . data ( " orig_class " );
$ . ajax ({
type : 'POST' ,
2015-08-11 14:54:05 -07:00
url : 'ajax_form.php' ,
2015-07-13 20:10:26 +02:00
data : { type : " update-alert-rule " , alert_id : alert_id , state : state },
dataType : " html " ,
success : function ( msg ) {
if ( msg . indexOf ( " ERROR: " ) <= - 1 ) {
if ( state ) {
2016-09-22 10:44:17 -04:00
$ ( '#alert-rule-' + alert_id ) . removeClass ( 'fa-pause' );
$ ( '#alert-rule-' + alert_id ) . addClass ( 'fa-' + orig_state );
2015-07-13 20:10:26 +02:00
$ ( '#alert-rule-' + alert_id ) . removeClass ( 'text-default' );
$ ( '#alert-rule-' + alert_id ) . addClass ( 'text-' + orig_colour );
$ ( '#row_' + alert_id ) . removeClass ( 'active' );
$ ( '#row_' + alert_id ) . addClass ( orig_class );
} else {
2016-09-22 10:44:17 -04:00
$ ( '#alert-rule-' + alert_id ) . removeClass ( 'fa-' + orig_state );
$ ( '#alert-rule-' + alert_id ) . addClass ( 'fa-pause' );
2015-07-13 20:10:26 +02:00
$ ( '#alert-rule-' + alert_id ) . removeClass ( 'text-' + orig_colour );
$ ( '#alert-rule-' + alert_id ) . addClass ( 'text-default' );
$ ( '#row_' + alert_id ) . removeClass ( 'warning' );
$ ( '#row_' + alert_id ) . addClass ( 'active' );
}
2014-11-30 17:49:52 +00:00
} else {
2015-07-13 20:10:26 +02:00
$ ( " #message " ) . html ( '<div class="alert alert-info">' + msg + '</div>' );
$ ( '#' + alert_id ) . bootstrapSwitch ( 'toggleState' , true );
}
},
error : function () {
$ ( " #message " ) . html ( '<div class="alert alert-info">This alert could not be updated.</div>' );
$ ( '#' + alert_id ) . bootstrapSwitch ( 'toggleState' , true );
2014-11-30 17:49:52 +00:00
}
});
});
function updateResults ( results ) {
2015-07-13 20:10:26 +02:00
$ ( '#results_amount' ) . val ( results . value );
$ ( '#page_number' ) . val ( 1 );
$ ( '#result_form' ) . submit ();
2014-11-30 17:49:52 +00:00
}
function changePage ( page , e ) {
e . preventDefault ();
$ ( '#page_number' ) . val ( page );
$ ( '#result_form' ) . submit ();
}
2015-03-21 21:30:55 +00:00
2014-11-30 17:49:52 +00:00
</ script >