Files
librenms-librenms/app/Http/Controllers/Table/PortsController.php
Jellyfrog 0a351b49fd Laravel 9.x Shift (#14504)
* Move `resources/lang` folder

* Shift registered middleware

* Remove `fruitcake/laravel-cors` dependency

* Streamline `$commands` property

* Upgrade to Flysystem 3.0

* Shift core files

* Convert `optional()` to nullsafe operator

* Remove unnecessary `$model` property

* Convert route options to fluent methods

Laravel 8 adopts the tuple syntax for controller actions. Since the old options array is incompatible with this syntax, Shift converted them to use modern, fluent methods.

* Convert deprecated `$dates` property to `$casts`

* Shift config files

* Default config files

In an effort to make upgrading the constantly changing config files
easier, Shift defaulted them and merged your true customizations -
where ENV variables may not be used.

* Bump Laravel dependencies

* Use `<env>` tags for configuration

`<env>` tags have a lower precedence than system environment variables making it easier to overwrite PHPUnit configuration values in additional environments, such a CI.

Review this blog post for more details on configuration precedence when testing Laravel: https://jasonmccreary.me/articles/laravel-testing-configuration-precedence/

* Fix error provider

* Match new symfony syntax

* Match upstream syntax

* Fix route syntax

* generate composer.lock

* Sync back configs

* routes

* composer

* Fix more flare

* fix cors

* sync lang

* Apply fixes from StyleCI (#14517)

Co-authored-by: StyleCI Bot <bot@styleci.io>

* bump larastan

* update packages

* wip

* Temporarily lower phpstan level

* Update phpstan.neon

* wip

* wip

* wip

* Apply fixes from StyleCI (#14592)

Co-authored-by: StyleCI Bot <bot@styleci.io>

* test

* Update CiHelper.php

* Update test.yml

* Update CiHelper.php

* Update CiHelper.php

* Apply fixes from StyleCI (#14616)

Co-authored-by: StyleCI Bot <bot@styleci.io>

* test?

* fix phpstan problems

* dont run snmpsim on github ci

* Fix whitespace

* More whitespace

* More whitespace ???

* I think the space broke it

* fix the reset of the whitespace

* hard code auth guard

---------

Co-authored-by: Shift <shift@laravelshift.com>
Co-authored-by: StyleCI Bot <bot@styleci.io>
Co-authored-by: Tony Murray <murraytony@gmail.com>
2023-04-17 06:51:35 -05:00

179 lines
6.5 KiB
PHP

<?php
/*
* PortsController.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 2021 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Http\Controllers\Table;
use App\Models\Port;
use DB;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use LibreNMS\Util\Number;
use LibreNMS\Util\Rewrite;
use LibreNMS\Util\Url;
class PortsController extends TableController
{
protected function rules()
{
return [
'device_id' => 'nullable|integer',
'deleted' => 'boolean',
'disabled' => 'boolean',
'errors' => 'nullable|boolean',
'hostname' => 'nullable|ip_or_hostname',
'ifAlias' => 'nullable|string',
'ifType' => 'nullable|string',
'ifSpeed' => 'nullable|integer',
'ignore' => 'boolean',
'location' => 'nullable|integer',
'port_descr_type' => 'nullable|string',
'state' => 'nullable|in:up,down,admindown',
];
}
protected function filterFields($request)
{
return [
'ports.device_id' => 'device_id',
'location_id' => 'location',
'ifSpeed',
'ifType',
'port_descr_type',
'ports.disabled' => 'disabled',
'ports.ignore' => 'ignore',
'group' => function ($query, $group) {
return $query->whereHas('groups', function ($query) use ($group) {
return $query->where('id', $group);
});
},
'devicegroup' => function ($query, $devicegroup) {
return $query->whereHas('device', function ($query) use ($devicegroup) {
return $query->whereHas('groups', function ($query) use ($devicegroup) {
return $query->where('id', $devicegroup);
});
});
},
];
}
protected function sortFields($request)
{
return [
'hostname',
'ifIndex',
'ifDescr',
'secondsIfLastChange',
'ifConnectorPresent',
'ifInErrors_delta',
'ifOutErrors_delta',
'ifInOctets_rate',
'ifOutOctets_rate',
'ifInUcastPkts_rate',
'ifOutUcastPkts_rate',
'ifType',
'ifAlias',
'ifMtu',
'ifSpeed',
];
}
protected function baseQuery($request)
{
$query = Port::hasAccess($request->user())
->with('device')
->leftJoin('devices', 'ports.device_id', 'devices.device_id')
->where('deleted', $request->get('deleted', 0)) // always filter deleted
->when($request->get('hostname'), function (Builder $query, $hostname) {
$query->where(function (Builder $query) use ($hostname) {
$query->where('devices.hostname', 'like', "%$hostname%")
->orWhere('devices.sysName', 'like', "%$hostname%");
});
})
->when($request->get('ifAlias'), function (Builder $query, $ifAlias) {
return $query->where('ifAlias', 'like', "%$ifAlias%");
})
->when($request->get('errors'), function (Builder $query) {
$query->where(function (Builder $query) {
return $query->where('ifInErrors_delta', '>', 0)
->orWhere('ifOutErrors_delta', '>', 0);
});
})
->when($request->get('state'), function (Builder $query, $state) {
switch ($state) {
case 'down':
return $query->where('ifAdminStatus', 'up')->where('ifOperStatus', 'down');
case 'up':
return $query->where('ifAdminStatus', 'up')->where('ifOperStatus', 'up');
case 'admindown':
return $query->where('ifAdminStatus', 'down')->where('ports.ignore', 0);
default:
return $query;
}
});
$select = [
'ports.*',
'hostname',
];
if (array_key_exists('secondsIfLastChange', Arr::wrap($request->get('sort')))) {
// for sorting
$select[] = DB::raw('`devices`.`uptime` - `ports`.`ifLastChange` / 100 as secondsIfLastChange');
}
return $query->select($select);
}
/**
* @param \App\Models\Port $port
* @return array
*/
public function formatItem($port)
{
$status = $port->ifOperStatus == 'down'
? ($port->ifAdminStatus == 'up' ? 'label-danger' : 'label-warning')
: 'label-success';
return [
'status' => $status,
'device' => Url::deviceLink($port->device),
'port' => Url::portLink($port),
'secondsIfLastChange' => ceil($port->device?->uptime - ($port->ifLastChange / 100)),
'ifConnectorPresent' => ($port->ifConnectorPresent == 'true') ? 'yes' : 'no',
'ifSpeed' => $port->ifSpeed,
'ifMtu' => $port->ifMtu,
'ifInOctets_rate' => $port->ifInOctets_rate * 8,
'ifOutOctets_rate' => $port->ifOutOctets_rate * 8,
'ifInUcastPkts_rate' => $port->ifInUcastPkts_rate,
'ifOutUcastPkts_rate' => $port->ifOutUcastPkts_rate,
'ifInErrors_delta' => $port->poll_period ? Number::formatSi($port->ifInErrors_delta / $port->poll_period, 2, 3, 'EPS') : '',
'ifOutErrors_delta' => $port->poll_period ? Number::formatSi($port->ifOutErrors_delta / $port->poll_period, 2, 3, 'EPS') : '',
'ifType' => Rewrite::normalizeIfType($port->ifType),
'ifAlias' => $port->ifAlias,
'actions' => (string) view('port.actions', ['port' => $port]),
];
}
}