Skip to content

Commit b08151a

Browse files
authored
Legacy port function cleanup (librenms#18559)
convert get_port_by_id to use PortCache, replace others
1 parent d66cded commit b08151a

File tree

5 files changed

+31
-38
lines changed

5 files changed

+31
-38
lines changed

includes/common.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,16 @@ function get_port_rrdfile_path($hostname, $port_id, $suffix = '')
150150
return Rrd::name($hostname, Rrd::portName($port_id, $suffix));
151151
}
152152

153-
function get_port_by_ifIndex($device_id, $ifIndex)
154-
{
155-
return dbFetchRow('SELECT * FROM `ports` WHERE `device_id` = ? AND `ifIndex` = ?', [$device_id, $ifIndex]);
156-
}
157-
158-
function get_port_by_id($port_id)
153+
function get_port_by_id($port_id): array|false
159154
{
160155
if (is_numeric($port_id)) {
161-
$port = dbFetchRow('SELECT * FROM `ports` WHERE `port_id` = ?', [$port_id]);
162-
if (is_array($port)) {
163-
return $port;
164-
} else {
165-
return false;
156+
$port = PortCache::get($port_id);
157+
if ($port !== null) {
158+
return $port->toArray();
166159
}
167160
}
161+
162+
return false;
168163
}
169164

170165
function ifclass($ifOperStatus, $ifAdminStatus)
@@ -195,11 +190,6 @@ function gethostbyid($device_id)
195190
return DeviceCache::get((int) $device_id)->hostname;
196191
}
197192

198-
function getifbyid($id)
199-
{
200-
return dbFetchRow('SELECT * FROM `ports` WHERE `port_id` = ?', [$id]);
201-
}
202-
203193
function getidbyname($hostname)
204194
{
205195
return DeviceCache::getByHostname($hostname)->device_id;

includes/discovery/discovery-protocols.inc.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
echo ' Brocade FDP: ';
1212
$fdp_array = SnmpQuery::hideMib()->walk('FOUNDRY-SN-SWITCH-GROUP-MIB::snFdpCacheEntry')->table(2);
1313

14-
foreach ($fdp_array as $key => $fdp_if_array) {
15-
$interface = get_port_by_ifIndex($device['device_id'], $key);
14+
foreach ($fdp_array as $ifIndex => $fdp_if_array) {
15+
$interface = \App\Facades\PortCache::getByIfIndex($ifIndex, $device['device_id']);
1616
d_echo($fdp_if_array);
1717

1818
foreach ($fdp_if_array as $fdp) {
@@ -28,7 +28,7 @@
2828

2929
$remote_port_id = find_port_id($fdp['snFdpCacheDevicePort'], '', $remote_device_id);
3030
discover_link(
31-
$interface['port_id'],
31+
$interface->port_id,
3232
$fdp['snFdpCacheVendorId'],
3333
$remote_port_id,
3434
$fdp['snFdpCacheDeviceId'],
@@ -47,8 +47,8 @@
4747
echo ' CISCO-CDP-MIB: ';
4848
$cdp_array = SnmpQuery::hideMib()->walk('CISCO-CDP-MIB::cdpCache')->table(2);
4949

50-
foreach ($cdp_array as $key => $cdp_if_array) {
51-
$interface = get_port_by_ifIndex($device['device_id'], $key);
50+
foreach ($cdp_array as $ifIndex => $cdp_if_array) {
51+
$interface = PortCache::getByIfIndex($ifIndex, $device['device_id']);
5252

5353
foreach ($cdp_if_array as $cdp) {
5454
d_echo($cdp);
@@ -76,10 +76,10 @@
7676
}
7777
}
7878

79-
if ($interface['port_id'] && $cdp['cdpCacheDeviceId'] && $cdp['cdpCacheDevicePort']) {
79+
if ($interface?->port_id && $cdp['cdpCacheDeviceId'] && $cdp['cdpCacheDevicePort']) {
8080
$remote_port_id = find_port_id($cdp['cdpCacheDevicePort'], '', $remote_device_id);
8181
discover_link(
82-
$interface['port_id'],
82+
$interface->port_id,
8383
'cdp',
8484
$remote_port_id,
8585
$cdp['cdpCacheDeviceId'],
@@ -153,7 +153,7 @@
153153
foreach ($lldp_array as $lldp_array_inner) {
154154
foreach ($lldp_array_inner as $lldp) {
155155
d_echo($lldp);
156-
$interface = get_port_by_ifIndex($device['device_id'], $lldp['lldpRemLocalPortNum'] ?? null);
156+
$interface = PortCache::getByIfIndex($lldp['lldpRemLocalPortNum'] ?? null, $device['device_id']);
157157
$remote_device_id = find_device_id($lldp['lldpRemSysName'] ?? null);
158158

159159
if (LibrenmsConfig::get('autodiscovery.xdp') && isset($lldp['lldpRemSysName']) && ! $remote_device_id &&
@@ -163,10 +163,10 @@
163163
$remote_device_id = discover_new_device($lldp['lldpRemSysName'], $device, 'LLDP', $interface);
164164
}
165165

166-
if (is_array($interface) && $interface['port_id'] && $lldp['lldpRemSysName'] && $lldp['lldpRemPortId']) {
166+
if ($interface?->port_id && $lldp['lldpRemSysName'] && $lldp['lldpRemPortId']) {
167167
$remote_port_id = find_port_id($lldp['lldpRemPortDesc'], $lldp['lldpRemPortId'], $remote_device_id);
168168
discover_link(
169-
$interface['port_id'],
169+
$interface->port_id,
170170
'lldp',
171171
$remote_port_id,
172172
$lldp['lldpRemSysName'],
@@ -187,7 +187,7 @@
187187
foreach ($sub_lldp_1 as $ifIndex => $sub_lldp_2) {
188188
foreach ($sub_lldp_2 as $sub_lldp_3) {
189189
foreach ($sub_lldp_3 as $lldp) {
190-
$interface = get_port_by_ifIndex($device['device_id'], $ifIndex);
190+
$interface = PortCache::getByIfIndex($ifIndex, $device['device_id']);
191191
$remote_device_id = find_device_id($lldp['tmnxLldpRemSysName']);
192192

193193
if (! $remote_device_id &&
@@ -198,10 +198,10 @@
198198
$remote_device_id = discover_new_device($lldp['tmnxLldpRemSysName'], $device, 'LLDP', $interface);
199199
}
200200

201-
if ($interface['port_id'] && $lldp['tmnxLldpRemSysName'] && $lldp['tmnxLldpRemPortId']) {
201+
if ($interface?->port_id && $lldp['tmnxLldpRemSysName'] && $lldp['tmnxLldpRemPortId']) {
202202
$remote_port_id = find_port_id($lldp['tmnxLldpRemPortDesc'], $lldp['tmnxLldpRemPortId'], $remote_device_id);
203203
discover_link(
204-
$interface['port_id'],
204+
$interface->port_id,
205205
'lldp',
206206
$remote_port_id,
207207
$lldp['tmnxLldpRemSysName'],
@@ -229,11 +229,11 @@
229229
continue;
230230
}
231231

232-
$interface = get_port_by_ifIndex($device['device_id'], $IndexId);
233-
if (! $interface['port_id']) {
232+
$interface = PortCache::getByIfIndex($IndexId, $device['device_id']);
233+
if (empty($interface->port_id)) {
234234
$local_ifName = $lldp['lldpNeighborPortId'][$IndexId][1];
235235
$local_port_id = find_port_id('gigabitEthernet ' . $local_ifName, null, $device['device_id']);
236-
$interface = get_port_by_id($local_port_id);
236+
$interface = PortCache::get($local_port_id);
237237
}
238238

239239
$remote_device_id = find_device_id($lldp['lldpNeighborDeviceName'][$IndexId][1]);
@@ -250,9 +250,9 @@
250250
$remote_device_id = discover_new_device($remote_device_name, $device, 'LLDP', $interface);
251251
}
252252

253-
if ($interface['port_id'] && $remote_device_name && $remote_port_descr) {
253+
if ($interface?->port_id && $remote_device_name && $remote_port_descr) {
254254
discover_link(
255-
$interface['port_id'], //our port id from database
255+
$interface->port_id, //our port id from database
256256
'lldp',
257257
$remote_port_id, //remote port id from database if applicable
258258
$remote_device_name, //remote device name from SNMP walk

includes/discovery/functions.inc.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use App\Facades\LibrenmsConfig;
1818
use App\Models\Device;
1919
use App\Models\Eventlog;
20+
use App\Models\Port;
2021
use Illuminate\Support\Facades\Log;
2122
use Illuminate\Support\Str;
2223
use LibreNMS\Device\YamlDiscovery;
@@ -32,7 +33,7 @@
3233
* @param string $hostname
3334
* @param array $device
3435
* @param string $method name of process discoverying this device
35-
* @param array|null $interface Interface this device was discovered on
36+
* @param array|Port|null $interface Interface this device was discovered on
3637
* @return false|int
3738
*
3839
* @throws InvalidIpException
@@ -102,7 +103,9 @@ function discover_new_device($hostname, $device, $method, $interface = null)
102103
if ($result) {
103104
echo '+[' . $remote_device->hostname . '(' . $remote_device->device_id . ')]';
104105

105-
$extra_log = is_array($interface) ? ' (port ' . cleanPort($interface)['label'] . ') ' : '';
106+
$extra_log = is_array($interface)
107+
? ' (port ' . cleanPort($interface)['label'] . ') '
108+
: ($interface instanceof Port ? ' (port ' . $interface->getLabel() . ') ' : '');
106109
Eventlog::log('Device ' . $remote_device->hostname . " ($ip) $extra_log autodiscovered through $method on " . $device['hostname'], $device['device_id'], 'discovery', Severity::Ok);
107110

108111
return $remote_device->device_id;

includes/html/print-event.inc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
}
3636

3737
if ($entry['type'] == 'interface') {
38-
$this_if = cleanPort(getifbyid($entry['reference']));
38+
$this_if = cleanPort(get_port_by_id($entry['reference']));
3939
$entry['link'] = '<b>' . generate_port_link($this_if, Rewrite::shortenIfName(strtolower((string) $this_if['label']))) . '</b>';
4040
} else {
4141
$entry['link'] = 'System';

includes/html/table/eventlog.inc.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
foreach (dbFetchRows($sql, $param) as $eventlog) {
7777
$dev = device_by_id_cache($eventlog['device_id']);
7878
if ($eventlog['type'] == 'interface') {
79-
$this_if = cleanPort(getifbyid($eventlog['reference']));
79+
$this_if = cleanPort(get_port_by_id($eventlog['reference']));
8080
$type = '<b>' . generate_port_link($this_if, Rewrite::shortenIfName(strtolower((string) $this_if['label']))) . '</b>';
8181
} else {
8282
$type = $eventlog['type'];

0 commit comments

Comments
 (0)