2015-03-21 21:30:55 +00:00
< ? php
2020-02-09 20:06:12 -05:00
/**
* print - alert - rules . inc . php
*
* LibreNMS print alert rules table
*
* 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 .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*
* @ package LibreNMS
* @ link http :// librenms . org
* @ copyright 2020 The LibreNMS Community
* @ author Original Author < unknown >
* @ author Joseph Tingiris < joseph . tingiris @ gmail . com >
*/
if ( ! Auth :: user () -> hasGlobalAdmin ()) {
die ( 'ERROR: You need to be admin' );
}
2015-03-21 21:30:55 +00:00
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' ;
2020-02-09 20:06:12 -05:00
require_once 'includes/html/modal/delete_alert_rule.inc.php' ; // Also dies if !Auth::user()->hasGlobalAdmin()
require_once 'includes/html/modal/alert_rule_collection.inc.php' ; // Also dies if !Auth::user()->hasGlobalAdmin()
2020-02-26 15:07:48 +01:00
require_once 'includes/html/modal/alert_rule_list.inc.php' ; // Also dies if !Auth::user()->hasGlobalAdmin()
2020-02-09 20:06:12 -05:00
require_once 'includes/html/modal/edit_transport_group.inc.php' ;
require_once 'includes/html/modal/edit_alert_transport.inc.php' ;
echo '<form method="post" action="" id="result_form">' ;
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 ;
}
2020-02-09 20:06:12 -05:00
echo '<div class="table-responsive">' ;
echo '<div class="col pull-left">' ;
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>' ;
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>' ;
echo '</div>' ;
echo '<div class="col pull-right">' ;
echo '<select data-toggle="popover" data-placement="left" data-content="results per page" name="results" id="results" class="form-control input-sm" onChange="updateResults(this);">' ;
2015-07-13 20:10:26 +02:00
$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
}
echo " > $option </option> " ;
}
2020-02-09 20:06:12 -05:00
echo '</select>' ;
echo '</div>' ;
echo '</div>' ;
2015-07-13 20:10:26 +02:00
2020-02-09 20:06:12 -05:00
echo '<br>' ;
2015-07-13 20:10:26 +02:00
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
2020-02-12 19:53:26 +01:00
$device_location_rules = " SELECT ar4.* FROM alert_rules AS ar4 WHERE ar4.id IN (SELECT alm4.rule_id FROM alert_location_map AS alm4 LEFT JOIN devices AS d4 ON alm4.location_id=d4.location_id WHERE d4.device_id=?) " ;
$param [] = $device [ 'device_id' ];
$full_query = '(' . $global_rules . ') UNION DISTINCT (' . $device_rules . ') UNION DISTINCT (' . $device_group_rules . ') UNION DISTINCT (' . $device_location_rules . ')' ;
2019-11-27 11:32:09 +01:00
} 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
2020-02-09 20:06:12 -05:00
$full_query .= ' ORDER BY name ASC' ;
2019-11-27 11:32:09 +01:00
$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
2020-02-09 20:06:12 -05:00
?>
< div class = " table-responsive " >
< table id = " alert-rules-table " class = " table table-condensed table-hover table-striped " >
< thead >
< tr >
< th data - column - id = " Type " > Type < th >
< th data - column - id = " Name " > Name </ th >
< th data - column - id = " Devices " > Devices < th >
< th data - column - id = " Transports " > Transports < th >
< th data - column - id = " Extra " > Extra </ th >
< th data - column - id = " Rule " > Rule </ th >
< th data - column - id = " Severity " > Severity </ th >
< th data - column - id = " Status " > Status </ th >
< th data - column - id = " Enabled " > Enabled </ th >
< th data - column - id = " Action " style = " width:86px; " > Action </ th >
</ tr >
</ thead >
< tbody >
< ? php
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' ]));
2020-03-01 15:50:45 +01:00
$severity = dbFetchCell ( 'SELECT severity FROM alert_rules where id = ?' , array ( $rule [ 'id' ]));
2016-09-22 10:44:17 -04:00
$ico = 'check' ;
2015-07-13 20:10:26 +02:00
$col = 'success' ;
$extra = '' ;
2020-02-09 20:06:12 -05:00
$status_msg = '' ;
2015-07-13 20:10:26 +02:00
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' ;
2020-02-09 20:06:12 -05:00
$status_msg = " All devices matching " . $rule [ 'name' ] . " are OK " ;
}
if (( int ) $sub [ 'state' ] === 1 || ( int ) $sub [ 'state' ] === 2 ) {
2020-03-01 15:50:45 +01:00
$alert_style = alert_layout ( $severity );
$ico = $alert_style [ 'icon' ];
$col = $alert_style [ 'icon_color' ];
$extra = $alert_style [ 'background_color' ];
2020-02-09 20:06:12 -05:00
$status_msg = " Some devices matching " . $rule [ 'name' ] . " are currently alerting " ;
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' ;
2020-02-09 20:06:12 -05:00
$status_msg = $rule [ 'name' ] . " is OFF " ;
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' ]]);
2020-02-12 19:53:26 +01:00
$location_count = dbFetchCell ( 'SELECT COUNT(*) FROM alert_location_map WHERE rule_id=?' , [ $rule [ 'id' ]]);
$popover_msg_parts = [];
$icon_indicator = 'fa fa-globe fa-fw text-success' ;
2020-02-09 20:06:12 -05:00
if ( $device_count ) {
2020-02-12 19:53:26 +01:00
$popover_msg_parts [] = 'Device' ;
2018-03-14 20:25:19 +00:00
$icon_indicator = 'fa fa-server fa-fw text-primary' ;
2020-02-09 20:06:12 -05:00
}
if ( $group_count ) {
2020-02-12 19:53:26 +01:00
$popover_msg_parts [] = 'Group' ;
2018-03-14 20:25:19 +00:00
$icon_indicator = 'fa fa-th fa-fw text-primary' ;
2020-02-09 20:06:12 -05:00
}
2020-02-12 19:53:26 +01:00
if ( $location_count ) {
$popover_msg_parts [] = 'Location' ;
$icon_indicator = 'fa fa-th fa-fw text-primary' ;
2020-02-09 20:06:12 -05:00
}
2020-02-12 19:53:26 +01:00
if ( count ( $popover_msg_parts )) {
$popover_msg = implode ( ', ' , $popover_msg_parts );
} else {
$popover_msg = 'Global' ;
2015-07-19 18:52:39 +01:00
}
2020-02-12 19:53:26 +01:00
$popover_msg .= ' alert Rule #' . $rule [ 'id' ];
2018-03-14 20:25:19 +00:00
2020-02-09 20:06:12 -05:00
echo " <tr class=' " . $extra . " ' id='rule_id_ " . $rule [ 'id' ] . " '> " ;
// Type
echo " <td colspan= \" 2 \" ><div data-toggle='popover' data-placement='top' data-content= \" $popover_msg\ " class = \ " $icon_indicator\ " ></ div ></ td > " ;
// Name
2015-07-13 20:10:26 +02:00
echo '<td>' . $rule [ 'name' ] . '</td>' ;
2020-02-09 20:06:12 -05:00
// Devices (and Groups)
if ( $rule [ 'invert_map' ] == 0 ) {
$groups_msg = 'Only devices in this group.' ;
$devices_msg = 'Only this device.' ;
$except_device_or_group = null ;
}
if ( $rule [ 'invert_map' ] == 1 ) {
$devices_msg = 'All devices EXCEPT this device. ' ;
$groups_msg = 'All devices EXCEPT this group.' ;
$except_device_or_group = '<strong><em>EXCEPT</em></strong> ' ;
}
2020-02-12 19:53:26 +01:00
$popover_position = 'right' ;
$locations = null ;
if ( $location_count ) {
$location_query = 'SELECT locations.location, locations.id FROM alert_location_map, locations WHERE alert_location_map.rule_id=? and alert_location_map.location_id = locations.id ORDER BY location' ;
$location_maps = dbFetchRows ( $location_query , [ $rule [ 'id' ]]);
foreach ( $location_maps as $location_map ) {
$locations .= " $except_device_or_group <a href= \" /devices \ /location= " . $location_map [ 'id' ] . " \" data-container='body' data-toggle='popover' data-placement=' $popover_position ' data-content='View Devices for Location' target= \" _blank \" > " . $location_map [ 'location' ] . " </a><br> " ;
}
}
2020-02-09 20:06:12 -05:00
2020-02-12 19:53:26 +01:00
$groups = null ;
2020-02-09 20:06:12 -05:00
if ( $group_count ) {
$group_query = 'SELECT device_groups.name, device_groups.id FROM alert_group_map, device_groups WHERE alert_group_map.rule_id=? and alert_group_map.group_id = device_groups.id ORDER BY name' ;
$group_maps = dbFetchRows ( $group_query , [ $rule [ 'id' ]]);
foreach ( $group_maps as $group_map ) {
2020-02-12 19:53:26 +01:00
$groups .= " $except_device_or_group <a href= \" /device-groups/ " . $group_map [ 'id' ] . " /edit \" data-container='body' data-toggle='popover' data-placement=' $popover_position ' data-content=' " . $group_map [ 'name' ] . " ' title=' $groups_msg ' target= \" _blank \" > " . $group_map [ 'name' ] . " </a><br> " ;
2020-02-09 20:06:12 -05:00
}
}
2020-02-12 19:53:26 +01:00
$devices = null ;
2020-02-09 20:06:12 -05:00
if ( $device_count ) {
$device_query = 'SELECT devices.device_id,devices.hostname FROM alert_device_map, devices WHERE alert_device_map.rule_id=? and alert_device_map.device_id = devices.device_id ORDER BY hostname' ;
$device_maps = dbFetchRows ( $device_query , [ $rule [ 'id' ]]);
foreach ( $device_maps as $device_map ) {
2020-02-12 19:53:26 +01:00
$devices .= " $except_device_or_group <a href= \" /device/device= " . $device_map [ 'device_id' ] . " /tab=edit/ \" data-container='body' data-toggle='popover' data-placement=' $popover_position ' data-content=' " . $device_map [ 'hostname' ] . " ' title=' $devices_msg ' target= \" _blank \" > " . $device_map [ 'hostname' ] . " </a><br> " ;
2020-02-09 20:06:12 -05:00
}
}
echo " <td colspan='2'> " ;
2020-02-12 19:53:26 +01:00
if ( $locations ) {
echo $locations ;
}
2020-02-09 20:06:12 -05:00
if ( $groups ) {
echo $groups ;
}
if ( $devices ) {
echo $devices ;
}
2020-02-12 19:53:26 +01:00
if ( ! $devices && ! $groups && ! $locations ) {
2020-02-09 20:06:12 -05:00
// All Devices
2020-02-12 19:53:26 +01:00
echo " <a href= \" /devices \" data-container='body' data-toggle='popover' data-placement=' $popover_position ' data-content='View All Devices' target= \" _blank \" >All Devices</a><br> " ;
2020-02-09 20:06:12 -05:00
}
echo " </td> " ;
// Transports
$transport_count = dbFetchCell ( 'SELECT COUNT(*) FROM alert_transport_map WHERE rule_id=?' , [ $rule [ 'id' ]]);
$transports_popover = 'right' ;
$transports = null ;
if ( $transport_count ) {
$transport_maps = dbFetchRows ( 'SELECT transport_or_group_id,target_type FROM alert_transport_map WHERE alert_transport_map.rule_id=? ORDER BY target_type' , [ $rule [ 'id' ]]);
foreach ( $transport_maps as $transport_map ) {
$transport_name = null ;
if ( $transport_map [ 'target_type' ] == " group " ) {
$transport_name = dbFetchCell ( 'SELECT transport_group_name FROM alert_transport_groups WHERE transport_group_id=?' , [ $transport_map [ 'transport_or_group_id' ]]);
$transport_edit = " <a href='' data-toggle='modal' data-target='#edit-transport-group' data-group_id=' " . $transport_map [ 'transport_or_group_id' ] . " ' data-container='body' data-toggle='popover' data-placement=' $transports_popover ' data-content='Edit transport group $transport_name '> " . $transport_name . " </a> " ;
}
if ( $transport_map [ 'target_type' ] == " single " ) {
$transport_name = dbFetchCell ( 'SELECT transport_name FROM alert_transports WHERE transport_id=?' , [ $transport_map [ 'transport_or_group_id' ]]);
$transport_edit = " <a href='' data-toggle='modal' data-target='#edit-alert-transport' data-transport_id=' " . $transport_map [ 'transport_or_group_id' ] . " ' data-container='body' data-toggle='popover' data-placement=' $transports_popover ' data-content='Edit transport $transport_name '> " . $transport_name . " </a> " ;
}
$transports .= $transport_edit . " <br> " ;
}
}
if ( ! $transport_count || ! $transports ) {
$default_transports = dbFetchRows ( 'SELECT transport_id, transport_name FROM alert_transports WHERE is_default=1 ORDER BY transport_name' , []);
foreach ( $default_transports as $default_transport ) {
$transport_edit = " <a href='' data-toggle='modal' data-target='#edit-alert-transport' data-transport_id=' " . $default_transport [ 'transport_id' ] . " ' data-container='body' data-toggle='popover' data-placement=' $transports_popover ' data-content='Edit default transport " . $default_transport [ 'transport_name' ] . " '> " . $default_transport [ 'transport_name' ] . " </a> " ;
$transports .= $transport_edit . " <br> " ;
}
}
if ( ! $transports ) {
$transports = " none " ;
}
echo " <td colspan='2'> $transports </td> " ;
// Extra
echo '<td><small>Max: ' . $rule_extra [ 'count' ] . '<br />Delay: ' . $rule_extra [ 'delay' ] . '<br />Interval: ' . $rule_extra [ 'interval' ] . '</small></td>' ;
// Rule
2015-07-13 20:10:26 +02:00
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>' ;
2020-02-09 20:06:12 -05:00
// Severity
echo '<td>' . ( $rule [ 'severity' ] == " ok " ? strtoupper ( $rule [ 'severity' ]) : ucwords ( $rule [ 'severity' ])) . '</td>' ;
// Status
$status_popover = 'top' ;
echo " <td><span data-toggle='popover' data-placement=' $status_popover ' data-content=' $status_msg ' 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 ) {
2020-02-09 20:06:12 -05:00
echo " <div data-toggle='popover' data-content='Alerts for " . $rule [ 'name' ] . " are muted' class='fa fa-fw fa-2x fa-volume-off text-primary' aria-hidden='true'></div></td> " ;
2015-07-13 20:10:26 +02:00
}
2020-02-09 20:06:12 -05:00
// Enabled
$enabled_popover = 'top' ;
2015-07-13 20:10:26 +02:00
echo '<td>' ;
2020-02-09 20:06:12 -05:00
if ( $rule [ 'disabled' ]) {
$enabled_msg = $rule [ 'name' ] . " is OFF " ;
}
if ( ! $rule [ 'disabled' ]) {
$enabled_msg = $rule [ 'name' ] . " is ON " ;
2015-07-13 20:10:26 +02:00
}
2020-02-09 20:06:12 -05:00
echo " <div id='on-off-checkbox- " . $rule [ 'id' ] . " ' data-toggle='popover' data-placement=' $enabled_popover ' data-content=' " . $enabled_msg . " ' class='btn-group btn-group-sm' role='group'> " ;
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' ] . " ' data-alert_name=' " . $rule [ 'name' ] . " ' data-alert_status=' " . $status_msg . " ' " . $alert_checked . " data-size='small' data-toggle='modal'> " ;
echo " </div> " ;
2015-07-13 20:10:26 +02:00
echo '</td>' ;
2020-02-09 20:06:12 -05:00
// Action
$action_popover = 'left' ;
echo '<td>' ;
echo " <div class='btn-group btn-group-sm' role='group'> " ;
echo " <button type='button' class='btn btn-primary' data-toggle='modal' data-placement=' $action_popover ' data-target='#create-alert' data-rule_id=' " . $rule [ 'id' ] . " ' name='edit-alert-rule' data-content='Edit alert rule " . $rule [ 'name' ] . " ' data-container='body'><i class='fa fa-lg fa-pencil' aria-hidden='true'></i></button> " ;
echo " <button type='button' class='btn btn-danger' aria-label='Delete' data-placement=' $action_popover ' data-toggle='modal' data-target='#confirm-delete' data-alert_id=' " . $rule [ 'id' ] . " ' data-alert_name=' " . $rule [ 'name' ] . " ' name='delete-alert-rule' data-content='Delete alert rule " . $rule [ 'name' ] . " ' 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>' ;
2020-02-09 20:06:12 -05:00
2015-07-13 20:10:26 +02:00
echo " </tr> \r \n " ;
} //end foreach
2020-02-09 20:06:12 -05:00
?>
</ tbody >
</ table >
</ div >
< ? php
// Pagination
2015-07-13 20:10:26 +02:00
if (( $count % $results ) > 0 ) {
2020-02-09 20:06:12 -05:00
echo '<div class="table-responsive">' ;
echo '<div class="col pull-left">' ;
echo generate_pagination ( $count , $results , $page_number );
echo '</div>' ;
echo '<div class="col pull-right">' ;
$showing_start = ( $page_number * $results ) - $results + 1 ;
$showing_end = $page_number * $results ;
if ( $showing_end > $count ) {
$showing_end = $count ;
}
echo " <p class= \" pagination \" >Showing $showing_start to $showing_end of $count alert rules</p> " ;
echo '</div>' ;
echo '</div>' ;
2014-11-30 17:49:52 +00:00
}
2015-07-13 20:10:26 +02:00
2020-02-09 20:06:12 -05:00
echo '<input type="hidden" name="page_number" id="page_number" value="' . $page_number . ' " >
2015-07-13 20:10:26 +02:00
< input type = " hidden " name = " results_amount " id = " results_amount " value = " '. $results .' " >
2020-02-09 20:06:12 -05:00
</ form > ' ;
2014-12-22 16:58:58 +00:00
2015-07-13 20:10:26 +02:00
if ( $count < 1 ) {
2020-02-09 20:06:12 -05:00
echo ' < div class = " row " >
< div class = " col-sm-12 " >
< form role = " form " method = " post " >
' . csrf_field() . '
< p class = " text-center " >
< 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 >
</ p >
</ form >
</ div >
</ div > ' ;
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 ({
2020-02-09 20:06:12 -05:00
trigger : 'hover'
2015-07-19 18:52:39 +01:00
});
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 " );
2020-02-09 20:06:12 -05:00
var alert_name = $ ( this ) . data ( " alert_name " );
var alert_status = $ ( this ) . data ( " alert_status " );
2014-11-30 17:49:52 +00:00
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 );
2020-02-09 20:06:12 -05:00
$ ( '#alert-rule-' + alert_id ) . attr ( 'data-content' , alert_status );
$ ( '#on-off-checkbox-' + alert_id ) . attr ( 'data-content' , alert_name + ' is ON' );
$ ( '#rule_id_' + alert_id ) . removeClass ( 'active' );
$ ( '#rule_id_' + alert_id ) . addClass ( orig_class );
2015-07-13 20:10:26 +02:00
} 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' );
2020-02-09 20:06:12 -05:00
$ ( '#alert-rule-' + alert_id ) . attr ( 'data-content' , alert_name + ' is OFF' );
$ ( '#on-off-checkbox-' + alert_id ) . attr ( 'data-content' , alert_name + ' is OFF' );
$ ( '#rule_id_' + alert_id ) . removeClass ( 'warning' );
$ ( '#rule_id_' + alert_id ) . addClass ( 'active' );
2015-07-13 20:10:26 +02:00
}
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 ();
}
</ script >