Skip to content

Commit aad3dc2

Browse files
authored
Added new Port Security polling module, API and WebUI info (librenms#18296)
* Added new Port Security polling module, API and WebUI info * Remove some additional table heading * Debug fix, api fix and ci fixes * CI fixes * CI fixes * CI fixes * CI fixes * CI fixes * Updated schema file * Added test data * Updated test data and default options * Update os_schema.json to accept new poller module * Derp, wrong section * Converted main search page to laravel * CI Fix
1 parent 609d238 commit aad3dc2

34 files changed

+24071
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/**
4+
* PortSecurityStatus.php
5+
*
6+
* Enumerates Port Security Status with color mappings
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
* @link https://www.librenms.org
22+
*/
23+
24+
namespace LibreNMS\Enum;
25+
26+
class PortSecurityStatus
27+
{
28+
const SECURE_UP = 'secureup';
29+
const SECURE_DOWN = 'securedown';
30+
const SHUTDOWN = 'shutdown';
31+
32+
const COLORS = [
33+
self::SECURE_UP => 'green',
34+
self::SECURE_DOWN => 'orange',
35+
self::SHUTDOWN => 'red',
36+
];
37+
38+
/**
39+
* Get the icon class for a given port security status
40+
*
41+
* @param string $status
42+
* @return string
43+
*/
44+
public static function getIconClass(string $status): string
45+
{
46+
$color = self::COLORS[$status] ?? null;
47+
48+
if ($color === null) {
49+
return 'fa-shield';
50+
}
51+
52+
$colorMap = [
53+
'green' => 'tw:text-green-600',
54+
'orange' => 'tw:text-orange-600',
55+
'red' => 'tw:text-red-600',
56+
];
57+
58+
return 'fa-shield ' . $colorMap[$color];
59+
}
60+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* VminfoDiscovery.php
5+
*
6+
* -Description-
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
* @package LibreNMS
22+
* @link http://librenms.org
23+
* @copyright 2024 Michael Adams
24+
* @author Michael Adams <[email protected]>
25+
*/
26+
27+
namespace LibreNMS\Interfaces\Discovery;
28+
29+
use App\Models\PortSecurity;
30+
use Illuminate\Support\Collection;
31+
32+
interface PortSecurityDiscovery
33+
{
34+
/**
35+
* Discover port-security info
36+
*
37+
* @return Collection<PortSecurity>
38+
*/
39+
public function discoverPortSecurity(): Collection;
40+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* PortSecurityPolling.php
5+
*
6+
* -Description-
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
* @package LibreNMS
22+
* @link http://librenms.org
23+
* @copyright 2023 Tony Murray
24+
* @author Tony Murray <[email protected]>
25+
*/
26+
27+
namespace LibreNMS\Interfaces\Polling;
28+
29+
use App\Models\PortSecurity;
30+
use Illuminate\Support\Collection;
31+
32+
interface PortSecurityPolling
33+
{
34+
/**
35+
* Poll the ports for port-security data
36+
*
37+
* @param mixed $os
38+
* @param mixed $device
39+
* @return Collection<PortSecurity>
40+
*/
41+
public function pollPortSecurity($os, $device): Collection;
42+
}

LibreNMS/Modules/PortSecurity.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
/*
4+
* PortSecurity.php
5+
*
6+
* -Description-
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*
21+
* @package LibreNMS
22+
* @link http://librenms.org
23+
* @copyright 2023 Michael Adams
24+
* @author Michael Adams <[email protected]>
25+
*/
26+
27+
namespace LibreNMS\Modules;
28+
29+
use App\Models\Device;
30+
use App\Observers\ModuleModelObserver;
31+
use LibreNMS\DB\SyncsModels;
32+
use LibreNMS\Interfaces\Data\DataStorageInterface;
33+
use LibreNMS\Interfaces\Discovery\PortSecurityDiscovery;
34+
use LibreNMS\Interfaces\Module;
35+
use LibreNMS\Interfaces\Polling\PortSecurityPolling;
36+
use LibreNMS\OS;
37+
use LibreNMS\Polling\ModuleStatus;
38+
39+
class PortSecurity implements Module
40+
{
41+
use SyncsModels;
42+
43+
/**
44+
* @inheritDoc
45+
*/
46+
public function dependencies(): array
47+
{
48+
return [];
49+
}
50+
51+
public function shouldDiscover(OS $os, ModuleStatus $status): bool
52+
{
53+
return $status->isEnabledAndDeviceUp($os->getDevice()) && $os instanceof PortSecurityDiscovery;
54+
}
55+
56+
/**
57+
* @inheritDoc
58+
*/
59+
public function discover(OS $os): void
60+
{
61+
$this->poll($os, app('Datastore'));
62+
}
63+
64+
public function shouldPoll(OS $os, ModuleStatus $status): bool
65+
{
66+
return $status->isEnabledAndDeviceUp($os->getDevice()) && $os instanceof PortSecurityPolling;
67+
}
68+
69+
/**
70+
* Poll data for this module and update the DB
71+
*
72+
* @param \LibreNMS\OS $os
73+
*/
74+
public function poll(OS $os, DataStorageInterface $datastore): void
75+
{
76+
if ($os instanceof PortSecurityPolling) {
77+
$device = $os->getDevice();
78+
$portsec = $os->pollPortSecurity($os, $device);
79+
ModuleModelObserver::observe(\App\Models\PortSecurity::class);
80+
$this->syncModels($device, 'portSecurity', $portsec);
81+
}
82+
}
83+
84+
public function dataExists(Device $device): bool
85+
{
86+
return $device->portSecurity()->exists();
87+
}
88+
89+
/**
90+
* @inheritDoc
91+
*/
92+
public function cleanup(Device $device): int
93+
{
94+
return $device->portSecurity()->delete();
95+
}
96+
97+
/**
98+
* @inheritDoc
99+
*/
100+
public function dump(Device $device, string $type): ?array
101+
{
102+
return [
103+
'PortSecurity' => $device->portSecurity()->orderBy('port_id')
104+
->get()->map->makeHidden(['id', 'device_id']),
105+
];
106+
}
107+
}

LibreNMS/OS/Ios.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434
use LibreNMS\Interfaces\Discovery\Sensors\WirelessRsrqDiscovery;
3535
use LibreNMS\Interfaces\Discovery\Sensors\WirelessRssiDiscovery;
3636
use LibreNMS\Interfaces\Discovery\Sensors\WirelessSnrDiscovery;
37+
use LibreNMS\Interfaces\Polling\PortSecurityPolling;
3738
use LibreNMS\OS\Shared\Cisco;
3839
use LibreNMS\OS\Traits\CiscoCellular;
40+
use LibreNMS\OS\Traits\CiscoPortSecurity;
3941

4042
class Ios extends Cisco implements
4143
WirelessCellDiscovery,
@@ -44,9 +46,11 @@ class Ios extends Cisco implements
4446
WirelessRssiDiscovery,
4547
WirelessRsrqDiscovery,
4648
WirelessRsrpDiscovery,
47-
WirelessSnrDiscovery
49+
WirelessSnrDiscovery,
50+
PortSecurityPolling
4851
{
4952
use CiscoCellular;
53+
use CiscoPortSecurity;
5054

5155
/**
5256
* @return WirelessSensor[] Sensors

LibreNMS/OS/Iosxe.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use LibreNMS\Interfaces\Polling\IsIsPolling;
4444
use LibreNMS\Interfaces\Polling\OSPolling;
4545
use LibreNMS\OS\Traits\CiscoCellular;
46+
use LibreNMS\OS\Traits\CiscoPortSecurity;
4647
use LibreNMS\Util\IP;
4748
use SnmpQuery;
4849

@@ -59,6 +60,7 @@ class Iosxe extends Ciscowlc implements
5960
{
6061
use SyncsModels;
6162
use CiscoCellular;
63+
use CiscoPortSecurity;
6264

6365
/**
6466
* Array of shortened ISIS codes

0 commit comments

Comments
 (0)