Skip to content

Commit

Permalink
Define Port Groups (librenms#12402)
Browse files Browse the repository at this point in the history
* Define Port Groups

* .

* .

* .

* API Calls

* .

* .

* .

* minor changes

* .

* update forms

* remove link

* .

* change column settings

* change migration

* change update position

* db migration fix

* .

* .

* .

* add missing doc reference

* update test data

* update test data

* update test data

* .

* .

* .

* .

* .

* .

* .

* .

* port group association in seperate table

* .

* .

* show all found groups on port

* select multiple Portgroups per Port

* change on migration file

* change query to eloquent

* Code changes

* move port group menu to ports main menu

* port group update to eloquent

* .

* .

* update to new setting way

* add missing merge parameter

* Use select2 and port some things to Laravel
some fixes, hopefully no new added bugs

* schema

* don't use on update restrict unfortunately

* remove unused import and revert changes

Co-authored-by: Tony Murray <[email protected]>
  • Loading branch information
SourceDoctor and murrant authored Apr 6, 2021
1 parent ecb87d9 commit 4b9e480
Show file tree
Hide file tree
Showing 28 changed files with 872 additions and 171 deletions.
57 changes: 57 additions & 0 deletions app/Http/Controllers/PortController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/*
* PortController.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 <[email protected]>
*/

namespace App\Http\Controllers;

use App\Models\Port;

class PortController extends Controller
{
public function update(\Illuminate\Http\Request $request, Port $port)
{
$this->validate($request, [
'groups.*' => 'int',
]);

$updated = false;
$message = '';

if ($request->has('groups')) {
$changes = $port->groups()->sync($request->get('groups'));
$groups_updated = array_sum(array_map(function ($group_ids) {
return count($group_ids);
}, $changes));

if ($groups_updated > 0) {
$message .= trans('port.groups.updated', ['port' => $port->getLabel()]);
$updated = true;
}
}

return $updated
? response(['message' => $message])
: response(['message' => trans('port.groups.none', ['port' => $port->getLabel()])], 400);
}
}
117 changes: 117 additions & 0 deletions app/Http/Controllers/PortGroupController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace App\Http\Controllers;

use App\Models\PortGroup;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Toastr;

class PortGroupController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('port-group.index', [
'port_groups' => PortGroup::orderBy('name')->get(),
]);
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('port-group.create', [
'port_group' => new PortGroup(),
]);
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|string|unique:port_groups',
]);

$portGroup = PortGroup::make($request->only(['name', 'desc']));
$portGroup->save();

Toastr::success(__('Port Group :name created', ['name' => $portGroup->name]));

return redirect()->route('port-groups.index');
}

/**
* Show the form for editing the specified resource.
*
* @param \App\Models\PortGroup $portGroup
* @return \Illuminate\Http\Response
*/
public function edit(PortGroup $portGroup)
{
return view('port-group.edit', [
'port_group' => $portGroup,
]);
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\PortGroup $portGroup
* @return \Illuminate\Http\Response
*/
public function update(Request $request, PortGroup $portGroup)
{
$this->validate($request, [
'name' => [
'required',
'string',
'max:255',
Rule::unique('port_groups', 'name')->where(function ($query) use ($portGroup) {
$query->where('id', '!=', $portGroup->id);
}),
],
'desc' => 'string|max:255',
]);

$portGroup->fill($request->only(['name', 'desc']));

if ($portGroup->save()) {
Toastr::success(__('Port Group :name updated', ['name' => $portGroup->name]));
} else {
Toastr::error(__('Failed to save'));

return redirect()->back()->withInput();
}

return redirect()->route('port-groups.index');
}

/**
* Remove the specified resource from storage.
*
* @param \App\Models\PortGroup $portGroup
* @return \Illuminate\Http\Response
*/
public function destroy(PortGroup $portGroup)
{
$portGroup->delete();

$msg = __('Port Group :name deleted', ['name' => $portGroup->name]);

return response($msg, 200);
}
}
48 changes: 48 additions & 0 deletions app/Http/Controllers/Select/PortGroupController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* PortGroupController.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/>.
*
* @link http://librenms.org
* @copyright 2020 Thomas Berberich
* @author Thomas Berberich <[email protected]>
*/

namespace App\Http\Controllers\Select;

use App\Models\PortGroup;

class PortGroupController extends SelectController
{
protected function searchFields($request)
{
return ['name'];
}

protected function baseQuery($request)
{
return PortGroup::hasAccess($request->user())->select(['id', 'name']);
}

public function formatItem($port_group)
{
return [
'id' => $port_group->id,
'text' => $port_group->name,
];
}
}
87 changes: 87 additions & 0 deletions app/Http/Controllers/Table/EditPortsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/*
* EditPortsController.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 <[email protected]>
*/

namespace App\Http\Controllers\Table;

class EditPortsController extends TableController
{
public function rules()
{
return [
'device_id' => 'required|int',
'device_group' => 'nullable|int',
'eventtype' => 'nullable|string',
];
}

public function searchFields($request)
{
return ['ifName', 'ifAlias', 'ifDescr'];
}

protected function sortFields($request)
{
return ['ifIndex', 'ifName', 'ifAdminStatus', 'ifOperStatus', 'ifSpeed', 'ifAlias'];
}

protected function baseQuery($request)
{
return \App\Models\Port::where('device_id', $request->get('device_id'))
->with('groups');
}

/**
* @param \App\Models\Port $port
* @return array
*/
public function formatItem($port)
{
$is_port_bad = $port->ifAdminStatus != 'down' && $port->ifOperStatus != 'up';
$do_we_care = ($port->ignore || $port->disabled) ? false : $is_port_bad;
$out_of_sync = $do_we_care ? "class='red'" : '';
$tune = $port->device->getAttrib('ifName_tune:' . $port->ifName) == 'true' ? 'checked' : '';

$port_group_options = '';
foreach ($port->groups as $group) {
/** @var \App\Models\PortGroup $group */
$port_group_options .= '<option value="' . $group->id . '" selected>' . $group->name . '</option>';
}

return [
'ifIndex' => $port->ifIndex,
'ifName' => $port->getLabel(),
'ifAdminStatus' => $port->ifAdminStatus,
'ifOperStatus' => '<span id="operstatus_' . $port->port_id . '" ' . $out_of_sync . '>' . $port->ifOperStatus . '</span>',
'disabled' => '<input type="checkbox" class="disable-check" data-size="small" name="disabled_' . $port->port_id . '"' . ($port->disabled ? 'checked' : '') . '>
<input type="hidden" name="olddis_' . $port->port_id . '" value="' . ($port->disabled ? 1 : 0) . '"">',
'ignore' => '<input type="checkbox" class="ignore-check" data-size="small" name="ignore_' . $port->port_id . '"' . ($port->ignore ? 'checked' : '') . '>
<input type="hidden" name="oldign_' . $port->port_id . '" value="' . ($port->ignore ? 1 : 0) . '"">',
'port_tune' => '<input type="checkbox" name="override_config" data-attrib="ifName_tune:' . $port->ifName . '" data-device_id="' . $port->device_id . '" data-size="small" ' . $tune . '>',
'ifAlias' => '<div class="form-group"><input class="form-control input-sm" name="if-alias" data-device_id="' . $port->device_id . '" data-port_id="' . $port->port_id . '" data-ifName="' . $port->ifName . '" value="' . $port->ifAlias . '"><span class="form-control-feedback"><i class="fa" aria-hidden="true"></i></span></div>',
'ifSpeed' => '<div class="form-group has-feedback"><input type="text" pattern="[0-9]*" inputmode="numeric" class="form-control input-sm" name="if-speed" data-device_id="' . $port->device_id . '" data-port_id="' . $port->port_id . '" data-ifName="' . $port->ifName . '" value="' . $port->ifSpeed . '"><span class="form-control-feedback"><i class="fa" aria-hidden="true"></i></span></div>',
'portGroup' => '<div class="form-group has-feedback"><select class="input-sm port_group_select" name="port_group_' . $port->port_id . '[]" data-port_id="' . $port->port_id . '" multiple>' . $port_group_options . '</select></div>',
];
}
}
4 changes: 4 additions & 0 deletions app/Http/Controllers/Widgets/TopInterfacesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class TopInterfacesController extends WidgetController
'time_interval' => 15,
'interface_filter' => null,
'device_group' => null,
'port_group' => null,
];

/**
Expand All @@ -60,6 +61,9 @@ public function getView(Request $request)
}, function ($query) {
$query->has('device');
})
->when($data['port_group'], function ($query) use ($data) {
$query->inPortGroup($data['port_group']);
})
->orderByRaw('SUM(LEAST(ifInOctets_rate, 9223372036854775807) + LEAST(ifOutOctets_rate, 9223372036854775807)) DESC')
->limit($data['interface_count']);

Expand Down
22 changes: 20 additions & 2 deletions app/Http/Controllers/Widgets/WidgetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

use App\Http\Controllers\Controller;
use App\Models\DeviceGroup;
use App\Models\PortGroup;
use App\Models\UserWidget;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -67,9 +68,22 @@ public function __invoke(Request $request)
// This might be invoked in getSettingsView() in an extended class
// So don't run it before since it's cached.
$this->getSettings();
}

if (! $this->show_settings) {
if (! empty($this->settings['device_group']) || ! empty($this->settings['port_group'])) {
$this->title .= ' (';

$title_details = [];
if (! empty($this->settings['device_group'])) {
$title_details[] = DeviceGroup::find($this->settings['device_group'])->name;
}
if (! empty($this->settings['port_group'])) {
$title_details[] = PortGroup::find($this->settings['port_group'])->name;
}

if (! empty($this->settings['device_group'])) {
$this->title .= ' (' . DeviceGroup::find($this->settings['device_group'])->name . ')';
$this->title .= implode(' ; ', $title_details);
$this->title .= ')';
}
$view = $this->getView($request);
}
Expand Down Expand Up @@ -101,6 +115,10 @@ public function getSettings($settingsView = false)
if ($settingsView && isset($this->settings['device_group'])) {
$this->settings['device_group'] = DeviceGroup::find($this->settings['device_group']);
}

if ($settingsView && isset($this->settings['port_group'])) {
$this->settings['port_group'] = PortGroup::find($this->settings['port_group']);
}
}

return $this->settings;
Expand Down
Loading

0 comments on commit 4b9e480

Please sign in to comment.