Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

119 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2022-01-30 16:28:18 -06:00
<?php
/*
* Stp.php
*
* Spanning Tree
*
* 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 LibreNMS\Modules;
2022-09-07 16:53:16 -05:00
use App\Models\Device;
2022-01-30 16:28:18 -06:00
use App\Models\PortStp;
use App\Observers\ModuleModelObserver;
2024-08-21 05:20:45 -05:00
use Illuminate\Support\Facades\Log;
2022-01-30 16:28:18 -06:00
use LibreNMS\DB\SyncsModels;
2023-10-04 10:32:59 -05:00
use LibreNMS\Interfaces\Data\DataStorageInterface;
2022-01-30 16:28:18 -06:00
use LibreNMS\Interfaces\Module;
use LibreNMS\OS;
2023-10-04 10:32:59 -05:00
use LibreNMS\Polling\ModuleStatus;
2022-01-30 16:28:18 -06:00
class Stp implements Module
{
use SyncsModels;
2022-09-07 16:53:16 -05:00
/**
* @inheritDoc
*/
public function dependencies(): array
{
return ['ports', 'vlans'];
}
2023-10-04 10:32:59 -05:00
public function shouldDiscover(OS $os, ModuleStatus $status): bool
{
2023-10-16 05:03:32 -07:00
return $status->isEnabledAndDeviceUp($os->getDevice());
2023-10-04 10:32:59 -05:00
}
2022-01-30 16:28:18 -06:00
public function discover(OS $os): void
{
$device = $os->getDevice();
2022-09-09 14:08:06 +02:00
$instances = $os->discoverStpInstances();
2024-08-21 05:20:45 -05:00
Log::info('Instances: ');
2022-09-09 14:08:06 +02:00
ModuleModelObserver::observe(\App\Models\Stp::class);
$this->syncModels($device, 'stpInstances', $instances);
2022-01-30 16:28:18 -06:00
2022-09-09 14:08:06 +02:00
$ports = $os->discoverStpPorts($instances);
2024-08-21 05:20:45 -05:00
Log::info('Ports: ');
2022-09-09 14:08:06 +02:00
ModuleModelObserver::observe(PortStp::class);
$this->syncModels($device, 'stpPorts', $ports);
2022-01-30 16:28:18 -06:00
}
2023-10-04 10:32:59 -05:00
public function shouldPoll(OS $os, ModuleStatus $status): bool
{
2023-10-16 05:03:32 -07:00
return $status->isEnabledAndDeviceUp($os->getDevice());
2023-10-04 10:32:59 -05:00
}
public function poll(OS $os, DataStorageInterface $datastore): void
2022-01-30 16:28:18 -06:00
{
$device = $os->getDevice();
2024-08-21 05:20:45 -05:00
Log::info('Instances: ');
2022-09-09 14:08:06 +02:00
$instances = $device->stpInstances;
$instances = $os->pollStpInstances($instances);
ModuleModelObserver::observe(\App\Models\Stp::class);
$this->syncModels($device, 'stpInstances', $instances);
2022-01-30 16:28:18 -06:00
2024-08-21 05:20:45 -05:00
Log::info('Ports: ');
2022-09-09 14:08:06 +02:00
$ports = $device->stpPorts;
ModuleModelObserver::observe(PortStp::class);
$this->syncModels($device, 'stpPorts', $ports);
2022-01-30 16:28:18 -06:00
}
2024-09-09 02:09:19 -05:00
public function dataExists(Device $device): bool
2022-09-07 16:53:16 -05:00
{
2024-09-09 02:09:19 -05:00
return $device->stpInstances()->exists() || $device->stpPorts()->exists();
}
public function cleanup(Device $device): int
{
$deleted = $device->stpInstances()->delete();
$deleted += $device->stpPorts()->delete();
return $deleted;
2022-09-07 16:53:16 -05:00
}
/**
* @inheritDoc
*/
public function dump(Device $device, string $type): ?array
2022-01-30 16:28:18 -06:00
{
2022-09-07 16:53:16 -05:00
return [
'stp' => $device->stpInstances()->orderBy('bridgeAddress')
->get()->map->makeHidden(['stp_id', 'device_id']),
'ports_stp' => $device->portsStp()->orderBy('port_index')
->leftJoin('ports', 'ports_stp.port_id', 'ports.port_id')
->select(['ports_stp.*', 'ifIndex'])
->get()->map->makeHidden(['port_stp_id', 'device_id', 'port_id']),
];
2022-01-30 16:28:18 -06:00
}
}