diff --git a/app/Http/Controllers/Widgets/AlertlogStatsController.php b/app/Http/Controllers/Widgets/AlertlogStatsController.php new file mode 100644 index 0000000000..883fa7437b --- /dev/null +++ b/app/Http/Controllers/Widgets/AlertlogStatsController.php @@ -0,0 +1,60 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2018 Tony Murray + * @author Tony Murray + */ + +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); + } +} diff --git a/database/seeds/DefaultWidgetSeeder.php b/database/seeds/DefaultWidgetSeeder.php index 456364f57d..dcf802a811 100644 --- a/database/seeds/DefaultWidgetSeeder.php +++ b/database/seeds/DefaultWidgetSeeder.php @@ -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", diff --git a/includes/html/table/alertlog-stats.inc.php b/includes/html/table/alertlog-stats.inc.php new file mode 100644 index 0000000000..5e6670d20c --- /dev/null +++ b/includes/html/table/alertlog-stats.inc.php @@ -0,0 +1,82 @@ + 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' => '
' . 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); diff --git a/resources/views/widgets/alertlog_stats.blade.php b/resources/views/widgets/alertlog_stats.blade.php new file mode 100644 index 0000000000..9cac5b30c5 --- /dev/null +++ b/resources/views/widgets/alertlog_stats.blade.php @@ -0,0 +1,31 @@ +
+
+ +
+
+
+ + + + + + + + +
CountDeviceAlert rule
+
+ diff --git a/resources/views/widgets/settings/alertlog_stats.blade.php b/resources/views/widgets/settings/alertlog_stats.blade.php new file mode 100644 index 0000000000..175a2a9268 --- /dev/null +++ b/resources/views/widgets/settings/alertlog_stats.blade.php @@ -0,0 +1,21 @@ +@extends('widgets.settings.base') + +@section('form') +
+ + +
+
+ + +
+
+ + +
+@endsection diff --git a/routes/web.php b/routes/web.php index cc48752f9b..4050a0d9f9 100644 --- a/routes/web.php +++ b/routes/web.php @@ -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'); }); });