2015-07-21 23:01:57 +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
*/
2015-07-21 23:01:57 +01:00
2017-12-24 21:55:24 +02:00
require_once $config [ 'install_dir' ] . '/includes/device-groups.inc.php' ;
2016-01-21 12:51:49 -07:00
2016-01-20 15:23:09 -07:00
/* FIXME: is there a central place we can put this? */
$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-20 15:23:09 -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-20 15:23:09 -07:00
);
2016-08-18 20:28:22 -05:00
//if( defined('SHOW_SETTINGS') || empty($widget_settings) ) {
if ( defined ( 'SHOW_SETTINGS' )) {
2016-01-20 15:23:09 -07:00
$current_acknowledged = isset ( $widget_settings [ 'acknowledged' ]) ? $widget_settings [ 'acknowledged' ] : '' ;
2018-05-16 05:54:02 +10:00
$current_fired = isset ( $widget_settings [ 'fired' ]) ? $widget_settings [ 'fired' ] : '' ;
2017-12-24 21:55:24 +02:00
$current_severity = isset ( $widget_settings [ 'severity' ]) ? $widget_settings [ 'severity' ] : '' ;
$current_state = isset ( $widget_settings [ 'state' ]) ? $widget_settings [ 'state' ] : '' ;
$current_group = isset ( $widget_settings [ 'group' ]) ? $widget_settings [ 'group' ] : '' ;
$current_proc = isset ( $widget_settings [ 'proc' ]) ? $widget_settings [ 'proc' ] : '' ;
2018-07-12 22:48:18 +02:00
$current_sorting = isset ( $widget_settings [ 'sort' ]) ? $widget_settings [ 'sort' ] : '' ;
2016-01-20 15:23:09 -07:00
$common_output [] = '
<form class="form" onsubmit="widget_settings(this); return false;">
<div class="form-group row">
<div class="col-sm-4">
<label for="acknowledged" class="control-label">Show acknowledged alerts: </label>
</div>
<div class="col-sm-8">
<select class="form-control" name="acknowledged">' ;
2017-12-24 21:55:24 +02:00
$common_output [] = '<option value=""' . ( $current_acknowledged == '' ? ' selected' : ' ' ) . '>not filtered</option>' ;
$common_output [] = '<option value="1"' . ( $current_acknowledged == '1' ? ' selected' : ' ' ) . '>show only acknowledged</option>' ;
$common_output [] = '<option value="0"' . ( $current_acknowledged == '0' ? ' selected' : ' ' ) . '>hide acknowledged</option>' ;
2016-01-20 15:23:09 -07:00
$common_output [] = '
</select>
</div>
</div>
2018-05-16 05:54:02 +10:00
<div class="form-group row">
<div class="col-sm-4">
<label for="fired" class="control-label">Show only Fired alerts: </label>
</div>
<div class="col-sm-8">
<select class="form-control" name="fired">' ;
$common_output [] = '<option value=""' . ( $current_fired == '' ? ' selected' : ' ' ) . '>not filtered</option>' ;
$common_output [] = '<option value="1"' . ( $current_fired == '1' ? ' selected' : ' ' ) . '>show only Fired alerts</option>' ;
$common_output [] = '
</select>
</div>
</div>
2016-01-20 15:23:09 -07:00
<div class="form-group row">
<div class="col-sm-4">
2017-10-15 12:50:55 -06:00
<label for="min_severity" class="control-label">Displayed severity:</label>
2016-01-20 15:23:09 -07:00
</div>
<div class="col-sm-8">
<select class="form-control" name="min_severity">
<option value="">any severity</option>' ;
foreach ( $alert_severities as $name => $val ) {
2017-12-24 21:55:24 +02:00
$common_output [] = "<option value= \" $val \" " . ( $current_severity == $name || $current_severity == $val ? ' selected' : '' ) . "> $name " . ( $val > 3 ? "" : " or higher" ) . "</option>" ;
2016-01-20 15:23:09 -07:00
}
$common_output [] = '
</select>
</div>
</div>
<div class="form-group row">
<div class="col-sm-4">
<label for="state" class="control-label">State:</label>
</div>
<div class="col-sm-8">
<select class="form-control" name="state">' ;
2017-12-24 21:55:24 +02:00
$common_output [] = '<option value=""' . ( $current_state == '' ? ' selected' : '' ) . '>any state</option>' ;
2016-01-20 15:23:09 -07:00
foreach ( $alert_states as $name => $val ) {
2017-12-24 21:55:24 +02:00
$common_output [] = "<option value= \" $val \" " . ( $current_state == $name || ( is_numeric ( $current_state ) && $current_state == $val ) ? ' selected' : '' ) . "> $name </option>" ;
2016-01-20 15:23:09 -07:00
}
$common_output [] = '
</select>
</div>
</div>
2016-01-21 12:51:49 -07:00
<div class="form-group row">
<div class="col-sm-4">
<label for="group" class="control-label">Device Group:</label>
</div>
<div class="col-sm-8">
<select class="form-control" name="group">' ;
2017-12-24 21:55:24 +02:00
$common_output [] = '<option value=""' . ( $current_group == '' ? ' selected' : '' ) . '>any group</option>' ;
2016-01-21 12:51:49 -07:00
$device_groups = GetDeviceGroups ();
2017-12-24 21:55:24 +02:00
$common_output [] = "<!-- " . print_r ( $device_groups , true ) . " -->" ;
2016-01-21 12:51:49 -07:00
foreach ( $device_groups as $group ) {
$group_id = $group [ 'id' ];
2017-12-24 21:55:24 +02:00
$common_output [] = "<option value= \" $group_id \" " . ( is_numeric ( $current_group ) && $current_group == $group_id ? ' selected' : '' ) . ">" . $group [ 'name' ] . " - " . $group [ 'description' ] . "</option>" ;
2016-01-21 12:51:49 -07:00
}
$common_output [] = '
</select>
</div>
</div>
2016-05-27 16:13:51 +02:00
<div class="form-group row">
<div class="col-sm-4">
2016-06-06 11:41:41 +02:00
<label for="proc" class="control-label">Show Procedure field: </label>
2016-05-27 16:13:51 +02:00
</div>
<div class="col-sm-8">
<select class="form-control" name="proc">' ;
2017-12-24 21:55:24 +02:00
$common_output [] = '<option value="1"' . ( $current_proc == '1' ? ' selected' : ' ' ) . '>show</option>' ;
$common_output [] = '<option value="0"' . ( $current_proc == '0' ? ' selected' : ' ' ) . '>hide</option>' ;
2016-05-27 16:13:51 +02:00
$common_output [] = '
</select>
</div>
</div>
2018-07-12 22:48:18 +02:00
<div class="form-group row">
<div class="col-sm-4">
<label for="sort" class="control-label">Sort alerts by: </label>
</div>
<div class="col-sm-8">
<select class="form-control" name="sort">' ;
$common_output [] = '<option value=""' . ( $current_sorting == '' ? ' selected' : '' )
. '>timestamp, descending</option>' ;
$common_output [] = '<option value="severity"' . ( $current_sorting == 'severity' ? ' selected' : ' ' )
. '>severity, descending</option>' ;
$common_output [] = '
</select>
</div>
</div>
2016-01-21 12:51:49 -07:00
2016-01-20 15:23:09 -07:00
<div class="form-group">
<div class="col-sm-12">
<button type="submit" class="btn btn-default">Set</button>
</div>
</div>
</form>
' ;
2016-08-18 20:28:22 -05:00
} else {
2017-12-24 21:55:24 +02:00
$device_id = $device [ 'device_id' ];
2016-01-20 15:23:09 -07:00
$acknowledged = $widget_settings [ 'acknowledged' ];
2018-05-16 05:54:02 +10:00
$fired = $widget_settings [ 'fired' ];
2017-12-24 21:55:24 +02:00
$state = $widget_settings [ 'state' ];
2016-01-20 15:23:09 -07:00
$min_severity = $widget_settings [ 'min_severity' ];
2017-12-24 21:55:24 +02:00
$group = $widget_settings [ 'group' ];
$proc = $widget_settings [ 'proc' ];
2018-07-12 22:48:18 +02:00
$sort = $widget_settings [ 'sort' ];
2016-01-20 15:23:09 -07:00
2016-01-21 17:01:45 -07:00
$title = "Alerts" ;
2016-01-20 15:23:09 -07:00
2016-01-21 17:01:45 -07:00
// state can be 0 or '', be sure they are treated differently
if ( is_numeric ( $state )) {
$state_name = array_search ( $state , $alert_states );
$title = " $title ( $state_name )" ;
2016-08-18 20:28:22 -05:00
} elseif ( $state ) {
2016-01-21 17:01:45 -07:00
$title = " $title ( $state )" ;
}
if ( is_numeric ( $acknowledged )) {
if ( $acknowledged == '0' ) {
$title = "Unacknowledged $title " ;
2016-08-18 20:28:22 -05:00
} elseif ( $acknowledged == '1' ) {
2016-01-21 17:01:45 -07:00
$title = "Acknowledged $title " ;
}
}
2018-05-16 05:54:02 +10:00
if ( is_numeric ( $fired )) {
$title = "Fired $title " ;
}
2016-01-21 17:01:45 -07:00
if ( is_numeric ( $group )) {
$group_row = dbFetchRow ( "SELECT * FROM device_groups WHERE id = ?" , array ( $group ));
if ( $group_row ) {
2017-12-24 21:55:24 +02:00
$title = " $title for " . $group_row [ 'name' ];
2016-01-21 17:01:45 -07:00
}
}
if ( $min_severity ) {
$sev_name = $min_severity ;
if ( is_numeric ( $min_severity )) {
$sev_name = array_search ( $min_severity , $alert_severities );
2017-12-24 21:55:24 +02:00
$title = " $title " . ( $min_severity > 3 ? "" : ">" ) . "= $sev_name " ;
2016-01-21 17:01:45 -07:00
}
}
2018-07-12 22:48:18 +02:00
if ( ! empty ( $sort )) {
$title = " $title " . "sorted by severity (higher first)" ;
}
2016-01-21 17:01:45 -07:00
$widget_settings [ 'title' ] = $title ;
2017-12-24 21:55:24 +02:00
$group = $widget_settings [ 'group' ];
2016-01-20 15:23:09 -07:00
$common_output [] = '
2015-07-21 23:01:57 +01:00
<div class="row">
<div class="col-sm-12">
<span id="message"></span>
</div>
</div>
<div class="table-responsive">
2017-12-24 21:55:24 +02:00
<table id="alerts_' . $unique_id . '" class="table table-hover table-condensed alerts">
2015-07-21 23:01:57 +01:00
<thead>
<tr>
2017-12-24 21:55:24 +02:00
<th data-column-id="severity"></th>
<th data-column-id="timestamp">Timestamp</th>
2015-07-21 23:01:57 +01:00
<th data-column-id="rule">Rule</th>
2017-12-24 21:55:24 +02:00
<th data-column-id="details" data-sortable="false"></th>
2015-07-21 23:01:57 +01:00
<th data-column-id="hostname">Hostname</th>
2018-04-26 06:00:56 +01:00
<th data-column-id="ack_ico" data-sortable="false">ACK</th>
<th data-column-id="notes" data-sortable="false">Notes</th>' ;
2017-12-24 21:55:24 +02:00
if ( $proc == '1' ) {
$common_output [] = '<th data-column-id="proc" data-sortable="false">URL</th>' ;
2016-05-27 16:13:51 +02:00
}
2017-12-24 21:55:24 +02:00
2016-05-27 16:13:51 +02:00
$common_output [] = '
2015-07-21 23:01:57 +01:00
</tr>
</thead>
</table>
</div>
<script>
2017-12-24 21:55:24 +02:00
var alerts_grid = $("#alerts_' . $unique_id . '").bootgrid({
2015-07-21 23:01:57 +01:00
ajax: true,
post: function ()
{
return {
id: "alerts",
2016-01-20 15:23:09 -07:00
' ;
if ( is_numeric ( $acknowledged )) {
2017-12-24 21:55:24 +02:00
$common_output [] = "acknowledged: ' $acknowledged ', \n " ;
2016-01-20 15:23:09 -07:00
}
2018-05-16 05:54:02 +10:00
if ( is_numeric ( $fired )) {
$common_output [] = "fired: ' $fired ', \n " ;
}
2016-01-20 15:23:09 -07:00
if ( isset ( $state ) && $state != '' ) {
2017-12-24 21:55:24 +02:00
$common_output [] = "state: ' $state ', \n " ;
2016-01-20 15:23:09 -07:00
}
if ( isset ( $min_severity ) && $min_severity != '' ) {
2017-12-24 21:55:24 +02:00
$common_output [] = "min_severity: ' $min_severity ', \n " ;
2016-01-20 15:23:09 -07:00
}
2016-01-21 12:51:49 -07:00
if ( is_numeric ( $group )) {
2017-12-24 21:55:24 +02:00
$common_output [] = "group: ' $group ', \n " ;
2016-01-21 12:51:49 -07:00
}
2016-05-27 16:13:51 +02:00
if ( is_numeric ( $proc )) {
2017-12-24 21:55:24 +02:00
$common_output [] = "proc: ' $proc ', \n " ;
2016-05-27 16:13:51 +02:00
}
2016-01-21 12:51:49 -07:00
2018-07-12 22:48:18 +02:00
if ( isset ( $sort ) && sort != '' ) {
$common_output [] = "sort: ' $sort ', \n " ;
}
2017-12-24 21:55:24 +02:00
$common_output [] = '
device_id: \'' . $device [ 'device_id' ] . '\'
2016-01-20 15:23:09 -07:00
}
2015-07-21 23:01:57 +01:00
},
2015-08-11 14:54:05 -07:00
url: "ajax_table.php",
2017-06-09 04:10:30 +01:00
rowCount: [50, 100, 250, -1],
2017-12-24 21:55:24 +02:00
2015-07-21 23:01:57 +01:00
}).on("loaded.rs.jquery.bootgrid", function() {
2017-03-28 09:00:09 -06:00
alerts_grid = $(this);
2015-08-08 09:43:28 +00:00
alerts_grid.find(".incident-toggle").each( function() {
2015-07-21 23:01:57 +01:00
$(this).parent().addClass(\'incident-toggle-td\');
}).on("click", function(e) {
var target = $(this).data("target");
$(target).collapse(\'toggle\');
2016-09-22 10:44:17 -04:00
$(this).toggleClass(\'fa-plus fa-minus\');
2015-07-21 23:01:57 +01:00
});
2015-08-08 09:43:28 +00:00
alerts_grid.find(".incident").each( function() {
2015-07-21 23:01:57 +01:00
$(this).parent().addClass(\'col-lg-4 col-md-4 col-sm-4 col-xs-4\');
$(this).parent().parent().on("mouseenter", function() {
2016-05-27 16:15:59 +02:00
$(this).find(".incident-toggle").fadeIn(200);
2015-07-21 23:01:57 +01:00
}).on("mouseleave", function() {
2016-05-27 16:15:59 +02:00
$(this).find(".incident-toggle").fadeOut(200);
2015-07-21 23:01:57 +01:00
});
});
2015-08-08 09:43:28 +00:00
alerts_grid.find(".command-ack-alert").on("click", function(e) {
2015-07-21 23:01:57 +01:00
e.preventDefault();
2018-04-26 06:00:56 +01:00
var alert_state = $(this).data("alert_state");
2018-09-19 23:38:01 +01:00
var alert_id = $(this).data(\'alert_id\');
$(\'#ack_alert_id\').val(alert_id);
$(\'#ack_alert_state\').val(alert_state);
$(\'#ack_msg\').val(\'\');
$("#alert_ack_modal").modal(\'show\');
2018-04-26 06:00:56 +01:00
});
alerts_grid.find(".command-alert-note").on("click", function(e) {
e.preventDefault();
var alert_id = $(this).data(\'alert_id\');
$(\'#alert_id\').val(alert_id);
$("#alert_notes_modal").modal(\'show\');
2015-07-21 23:01:57 +01:00
});
});
</script>' ;
2016-01-20 15:23:09 -07:00
}