Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions app/Http/Controllers/DevicePopupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@

use App\Facades\LibrenmsConfig;
use App\Models\Device;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Str;
use LibreNMS\Util\Graph;

class DevicePopupController
{
public function __invoke(Device $device)
public function __invoke(Request $request, Device $device)
{
if (! LibrenmsConfig::get('web_mouseover', true)) {
return response('Disabled');
Expand All @@ -42,7 +44,31 @@ public function __invoke(Device $device)
// Check access permissions
Gate::authorize('view', $device);

// Build graphs HTML using existing graph-row component
return view('device.popup', [
'device' => $device,
'osText' => LibrenmsConfig::getOsSetting($device->os ?? '', 'text'),
'href' => route('device', ['device' => $device->device_id]),
'graphs' => $this->buildGraphs($request, $device),
]);
}

/**
* @return array[]
*/
private function buildGraphs(Request $request, Device $device): array
{
$type = $request->string('type');
if ($type->isNotEmpty()) {
return [
[
'device' => $device,
'type' => $type,
'title' => Str::title($type),
'graphs' => [['from' => '-1d'], ['from' => '-7d'], ['from' => '-14d'], ['from' => '-30d']],
],
];
}

$graphs = [];
foreach (Graph::getOverviewGraphsForDevice($device) as $graph) {
if (isset($graph['text'], $graph['graph'])) {
Expand All @@ -55,11 +81,6 @@ public function __invoke(Device $device)
}
}

return view('device.popup', [
'device' => $device,
'osText' => LibrenmsConfig::getOsSetting($device->os ?? '', 'text'),
'href' => route('device', ['device' => $device->device_id]),
'graphs' => $graphs,
]);
return $graphs;
}
}
229 changes: 229 additions & 0 deletions app/Http/Controllers/DevicesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
<?php

namespace App\Http\Controllers;

use App\Models\Device;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Validation\Rule;
use Illuminate\View\View;
use LibreNMS\Util\Url;

class DevicesController extends Controller
{
public function index(Request $request, ?string $view = null, ?string $graph = null): View
{
$request->validate([
'format' => 'in:list_basic,list_detail,graph_bits,graph_processor,graph_ucd_load,graph_mempool,graph_uptime,graph_storage,graph_diskio,graph_poller_perf,graph_icmp_perf,graph_temperature', // legacy
'bare' => ['nullable', 'in:yes'],
'searchbar' => ['nullable', 'in:hide'],
'per_page' => ['nullable', 'integer'],
'page' => ['nullable', 'integer'],
'to' => ['nullable', 'date_or_relative'],
'from' => ['nullable', 'date_or_relative'],
...Device::filterValidationRules(),
'sort' => Rule::in([
'status',
'device_id',
'maintenance',
'hostname',
'hardware',
'os',
'uptime',
'location',
]),
]);

$bare = $request->input('bare') === 'yes';
$hideFilter = $request->input('searchbar') === 'hide';
$perPage = $request->integer('per_page', 50);

$legacyFormat = (string) $request->string('format');
if ($legacyFormat) {
if (str_starts_with($legacyFormat, 'graph_')) {
$view ??= 'graph';
$graph ??= substr($legacyFormat, 6);
} else {
$view ??= match ($legacyFormat) {
'list_basic' => 'basic',
default => 'detail',
};
$graph ??= '';
}
}

$graphTemplate = [
'height' => 110,
'width' => session('widescreen') ? 270 : 315,
'id' => 0,
'type' => 'device_' . $graph,
'from' => $request->input('from', '-1d'),
'legend' => 'no',
'title' => 'yes',
];
if ($request->input('to')) {
$graphTemplate['to'] = $request->input('to');
}

$devices = $this->getDevices($view, $perPage);

return view('device.index', [
'view' => $view,
'graph' => $graph,
'detailed' => $view === 'detail',
'devices' => $devices,
'deviceGraphs' => $devices->map(function (Device $device) use ($graphTemplate) {
$graph = array_merge($graphTemplate, ['device' => $device->device_id]);

return [
'link' => Url::graphPageUrl($graph['type'], Arr::except($graph, ['height', 'width', 'legend', 'title'])),
'graphTag' => Url::lazyGraphTag($graph, 'tw:w-full tw:h-auto'),
'deviceLinkOptions' => ['device_id' => $device->device_id, 'params' => ['type' => $graph['type']]],
];
}),
'perPage' => $perPage,
'paginationOptions' => [50, 100, 250, -1],
'nav' => [
'detail' => ['text' => 'Detail', 'link' => route('devices', ['view' => 'detail', ...$request->query()])],
'basic' => ['text' => 'Basic', 'link' => route('devices', ['view' => 'basic', ...$request->query()])],
],
'graphNav' => [
'bits' => ['text' => 'Bits', 'link' => route('devices', ['view' => 'graph', 'graph' => 'bits', ...$request->query()])],
'processor' => ['text' => 'CPU', 'link' => route('devices', ['view' => 'graph', 'graph' => 'processor', ...$request->query()])],
'ucd_load' => ['text' => 'Load', 'link' => route('devices', ['view' => 'graph', 'graph' => 'ucd_load', ...$request->query()])],
'mempool' => ['text' => 'Memory', 'link' => route('devices', ['view' => 'graph', 'graph' => 'mempool', ...$request->query()])],
'uptime' => ['text' => 'Uptime', 'link' => route('devices', ['view' => 'graph', 'graph' => 'uptime', ...$request->query()])],
'storage' => ['text' => 'Storage', 'link' => route('devices', ['view' => 'graph', 'graph' => 'storage', ...$request->query()])],
'diskio' => ['text' => 'Disk I/O', 'link' => route('devices', ['view' => 'graph', 'graph' => 'diskio', ...$request->query()])],
'poller_perf' => ['text' => 'Poller', 'link' => route('devices', ['view' => 'graph', 'graph' => 'poller_perf', ...$request->query()])],
'icmp_perf' => ['text' => 'Ping', 'link' => route('devices', ['view' => 'graph', 'graph' => 'icmp_perf', ...$request->query()])],
'temperature' => ['text' => 'Temperature', 'link' => route('devices', ['view' => 'graph', 'graph' => 'temperature', ...$request->query()])],
],
'bare' => $bare,
'bareLink' => $bare ? $request->fullUrlWithoutQuery('bare') : $request->fullUrlWithQuery(['bare' => 'yes']),
'filter' => $request->array('filter'),
'hideFilterLink' => $hideFilter ? $request->fullUrlWithoutQuery('searchbar') : $request->fullUrlWithQuery(['searchbar' => 'hide']),
'hideFilter' => $hideFilter,
'filterFields' => $this->filterFields(),
'graphTemplate' => $graphTemplate,
'group' => $request->input('filter.groups\.id.eq'),
]);
}

private function getDevices(?string $view, int $perPage): LengthAwarePaginator|Collection
{
if ($view !== 'graph') {
return new Collection;
}

$devicesQuery = Device::hasAccess(request()->user())
->select(['devices.*'])
->with('location')
->when(request()->array('filter'), fn (Builder $query, $filters) => $query->applyFilters($filters));

$devicesQuery->orderBy('hostname');

return $devicesQuery->paginate($perPage);
}

private function filterFields(): array
{
return [
[
'key' => 'search',
'label' => __('Search'),
'type' => 'text',
],
[
'key' => 'state',
'label' => __('device.status'),
'type' => 'select',
'options' => [
'up' => __('device.status_up'),
'down' => __('device.status_down'),
],
],
[
'key' => 'os',
'label' => __('device.os'),
'type' => 'select',
'endpoint' => route('ajax.select.device-field'),
'params' => [
'field' => 'os',
],
],
[
'key' => 'version',
'label' => __('Version'),
'type' => 'select',
'endpoint' => route('ajax.select.device-field'),
'params' => [
'field' => 'version',
],
],
[
'key' => 'hardware',
'label' => __('Platform'),
'type' => 'select',
'endpoint' => route('ajax.select.device-field'),
'params' => [
'field' => 'hardware',
],
],
[
'key' => 'features',
'label' => __('Featureset'),
'type' => 'select',
'endpoint' => route('ajax.select.device-field'),
'params' => [
'field' => 'features',
],
],
[
'key' => 'location_id',
'label' => __('Location'),
'type' => 'select',
'endpoint' => route('ajax.select.location'),
],
[
'key' => 'type',
'label' => __('device.device_type'),
'type' => 'select',
'endpoint' => route('ajax.select.device-field'),
'params' => [
'field' => 'type',
],
],
[
'key' => 'groups.id',
'label' => __('device.device_group'),
'type' => 'select',
'endpoint' => route('ajax.select.device-group'),
],
[
'key' => 'poller_group',
'label' => __('device.edit.poller_group'),
'type' => 'select',
'endpoint' => route('ajax.select.poller-group'),
],
[
'key' => 'disabled',
'label' => __('Disabled'),
'type' => 'boolean',
],
[
'key' => 'ignore',
'label' => __('Ignored'),
'type' => 'boolean',
],
[
'key' => 'disable_notify',
'label' => __('device.alerts_disabled'),
'type' => 'boolean',
],
];
}
}
2 changes: 2 additions & 0 deletions app/Http/Controllers/Table/DeviceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected function rules(): array
{
return [
'format' => 'nullable|in:list_basic,list_detail',
...Device::filterValidationRules(),
'os' => 'nullable|string',
'version' => 'nullable|string',
'hardware' => 'nullable|string',
Expand Down Expand Up @@ -101,6 +102,7 @@ protected function baseQuery(Request $request): Builder
/** @var Builder $query */
$query = Device::hasAccess($request->user())
->with(['location', 'groups'])
->applyFilters($request->array('filter'))
->withCount(['ports', 'sensors', 'wirelessSensors']);

// if searching or sorting the location field, join the locations table
Expand Down
43 changes: 42 additions & 1 deletion app/Models/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Facades\LibrenmsConfig;
use App\Models\Traits\Filterable;
use App\View\SimpleTemplate;
use Carbon\Carbon;
use Fico7489\Laravel\Pivot\Traits\PivotEventTrait;
Expand Down Expand Up @@ -38,7 +39,7 @@
*/
class Device extends BaseModel
{
use PivotEventTrait, HasFactory;
use PivotEventTrait, HasFactory, Filterable;

private ?MaintenanceStatus $maintenanceStatus = null;

Expand Down Expand Up @@ -88,6 +89,27 @@ class Device extends BaseModel
'uptime',
];

protected array $filterable = [
'device_id',
'hostname',
'sysName',
'display',
'hardware',
'os',
'location_id',
'version',
'features',
'type',
'status',
'disabled',
'ignore',
'disable_notify',
'poller_group',
'groups.id',
'search',
'state',
];

/**
* @return array{inserted: 'datetime', last_discovered: 'datetime', last_polled: 'datetime', last_ping: 'datetime', status: 'boolean'}
*/
Expand Down Expand Up @@ -540,6 +562,25 @@ public function setSysNameAttribute(?string $sysName): void

// ---- Query scopes ----

public function filterState(Builder $query, mixed $value, array $config): void
{
$this->applyMappedFilter($query, $value, $config, fn (Builder $q, $state) => match ($state) {
'up' => $q->where('status', 1)->where('disabled', 0)->where('disable_notify', 0),
'down' => $q->where('status', 0)->where('disabled', 0)->where('disable_notify', 0),
default => $q,
});
}

public function filterSearch(Builder $query, mixed $value, array $config): void
{
$this->applyFilterSearch(
['sysName', 'hostname', 'display', 'hardware', 'os', 'location.location'],
$query,
$value,
$config,
);
}

public function scopeIsUp($query)
{
return $query->where([
Expand Down
Loading