Alertlog stats dashboard widget (#10967)

* Dashboard for alertlog stats

* Dashboard for alertlog stats

* Dashboard for alertlog stats

* Dashboard for alertlog stats

* Dashboard for alertlog stats

* Cleanup

* Cleanup

* Cleanup

* date selection

* date selection

* fix js rule details

* cleanup js

* dummy commit to reload CI/CD

* dummy commit to reload CI/CD
This commit is contained in:
louis-oui
2020-01-23 15:16:30 +01:00
committed by Kevin Krumm
parent 4395b002ab
commit 6931df8990
6 changed files with 200 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
<?php
/**
* AlertlogController.php
*
* -Description-
*
* 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 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Http\Controllers\Widgets;
use Illuminate\Http\Request;
class AlertlogStatsController extends WidgetController
{
protected $title = 'Alert history stats';
protected $defaults = [
'title' => null,
'device_id' => '',
'min_severity' => 2,
'time_interval' => 7
];
public function getView(Request $request)
{
return view('widgets.alertlog_stats', $this->getSettings());
}
public function getSettingsView(Request $request)
{
$data = $this->getSettings(true);
$data['severities'] = [
// alert_rules.status is enum('ok','warning','critical')
'ok' => 1,
'warning' => 2,
'critical' => 3,
'ok only' => 4,
'warning only' => 5,
'critical only' => 6,
];
return view('widgets.settings.alertlog_stats', $data);
}
}

View File

@@ -87,6 +87,11 @@ class DefaultWidgetSeeder extends Seeder
"widget" => "component-status",
"base_dimensions" => "6,3",
],
[
"widget_title" => "Alert History Stats",
"widget" => "alertlog-stats",
"base_dimensions" => "6,3",
],
[
"widget_title" => "Server Stats",
"widget" => "server-stats",

View File

@@ -0,0 +1,82 @@
<?php
/*
* 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 2018 LibreNMS
* @author LibreNMS Contributors
*/
$where = 1;
if (is_numeric($vars['device_id'])) {
$where .= ' AND E.device_id = ?';
$param[] = $vars['device_id'];
}
$where .= ' AND `E`.`state` = 1'; // state 1 => alert
if (is_numeric($vars['time_interval'])) {
$where .= ' AND E.`time_logged` > DATE_SUB(NOW(),INTERVAL ? DAY)';
$param[] = $vars['time_interval'];
}
if (isset($vars['min_severity'])) {
$where .= get_sql_filter_min_severity($vars['min_severity'], "R");
}
if (Auth::user()->hasGlobalRead()) {
$sql = " FROM `alert_log` AS E LEFT JOIN devices AS D ON E.device_id=D.device_id RIGHT JOIN alert_rules AS R ON E.rule_id=R.id WHERE $where";
} else {
$sql = " FROM `alert_log` AS E LEFT JOIN devices AS D ON E.device_id=D.device_id RIGHT JOIN alert_rules AS R ON E.rule_id=R.id RIGHT JOIN devices_perms AS P ON E.device_id = P.device_id WHERE $where AND P.user_id = ?";
$param[] = array(Auth::id());
}
if (isset($searchPhrase) && !empty($searchPhrase)) {
$sql .= " AND (`D`.`hostname` LIKE '%$searchPhrase%' OR `D`.`sysName` LIKE '%$searchPhrase%' OR `E`.`time_logged` LIKE '%$searchPhrase%' OR `name` LIKE '%$searchPhrase%')";
}
$count_sql = "SELECT COUNT(DISTINCT D.sysname, R.name) $sql";
$total = dbFetchCell($count_sql, $param);
if (empty($total)) {
$total = 0;
}
$sql .= " GROUP BY D.device_id, R.name ORDER BY COUNT(*) DESC";
if (isset($current)) {
$limit_low = (($current * $rowCount) - ($rowCount));
$limit_high = $rowCount;
}
if ($rowCount != -1) {
$sql .= " LIMIT $limit_low,$limit_high";
}
$sql = "SELECT COUNT(*), D.device_id, R.name $sql";
$rulei = 0;
foreach (dbFetchRows($sql, $param) as $alertlog) {
$dev = device_by_id_cache($alertlog['device_id']);
$response[] = array(
'id' => $rulei++,
'count' => $alertlog['COUNT(*)'],
'hostname' => '<div class="incident">' . generate_device_link($dev, shorthost($dev['hostname'])),
'alert_rule' => $alertlog['name'],
);
}//end foreach
$output = array(
'current' => $current,
'rowCount' => $rowCount,
'rows' => $response,
'total' => $total,
);
echo _json_encode($output);

View File

@@ -0,0 +1,31 @@
<div class="row">
<div class="col-sm-12">
<span id="message"></span>
</div>
</div>
<div class="table-responsive">
<table id="alertlog-stats_{{ $id }}" class="table table-hover table-condensed table-striped">
<thead>
<tr>
<th data-column-id="count">Count</th>
<th data-column-id="hostname">Device</th>
<th data-column-id="alert_rule">Alert rule</th>
</tr>
</thead>
</table>
</div>
<script>
$("#alertlog-stats_{{ $id }}").bootgrid({
ajax: true,
rowCount: [50, 100, 250, -1],
post: function () {
return {
id: "alertlog-stats",
device_id: "",
min_severity: '{{ $min_severity }}',
time_interval: '{{ $time_interval }}'
};
},
url: "ajax_table.php"
});
</script>

View File

@@ -0,0 +1,21 @@
@extends('widgets.settings.base')
@section('form')
<div class="form-group row">
<label for="title-{{ $id }}" class="control-label">@lang('Widget title')</label>
<input type="text" class="form-control" name="title" id="title-{{ $id }}" placeholder="@lang('Custom title')" value="{{ $title }}">
</div>
<div class="form-group row">
<label for="min_severity-{{ $id }}" class="control-label">@lang('Displayed severity'):</label>
<select class="form-control" name="min_severity" id="min_severity-{{ $id }}">
<option value="">@lang('any severity')</option>
@foreach($severities as $name => $val)
<option value="{{ $val }}" @if($min_severity == $val) selected @endif>{{ $name }}{{$val > 3 ? '' : ' ' . __('or higher')}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="time_interval-{{ $id }}" class="control-label">@lang('Last days')</label>
<input class="form-control" name="time_interval" id="time_interval-{{ $id }}" value="{{ $time_interval }}">
</div>
@endsection

View File

@@ -134,6 +134,7 @@ Route::group(['middleware' => ['auth', '2fa'], 'guard' => 'auth'], function () {
Route::post('top-devices', 'TopDevicesController');
Route::post('top-interfaces', 'TopInterfacesController');
Route::post('worldmap', 'WorldMapController');
Route::post('alertlog-stats', 'AlertlogStatsController');
});
});