From db17036f5d6c45d363456f0e7e855de9d96501ab Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Thu, 26 Oct 2023 11:21:08 -0500 Subject: [PATCH] Ospf metrics fix (#15508) * Ospf metrics fix If the device gave an invalid IP for metrics, it would fail. Fix this issue, and optimize the sql queries so less are needed. * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot --- LibreNMS/Modules/Ospf.php | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/LibreNMS/Modules/Ospf.php b/LibreNMS/Modules/Ospf.php index b7852a2bf5..587f112b8e 100644 --- a/LibreNMS/Modules/Ospf.php +++ b/LibreNMS/Modules/Ospf.php @@ -193,20 +193,25 @@ class Ospf implements Module echo ' TOS Metrics: '; // Pull data from device + $ospf_ports_by_ip = $ospf_ports->keyBy('ospfIfIpAddress'); $ospf_tos_metrics = SnmpQuery::context($context_name) ->hideMib()->enumStrings() ->walk('OSPF-MIB::ospfIfMetricTable') - ->mapTable(function ($ospf_tos, $ip) use ($context_name, $os) { - $ospf_tos['ospf_port_id'] = OspfPort::query() - ->where('ospfIfIpAddress', $ip) - ->where('context_name', $context_name) - ->value('ospf_port_id'); + ->mapTable(function ($ospf_tos, $ip) use ($ospf_ports_by_ip) { + $port = $ospf_ports_by_ip->get($ip); - return OspfPort::updateOrCreate([ - 'device_id' => $os->getDeviceId(), - 'ospf_port_id' => $ospf_tos['ospf_port_id'], - 'context_name' => $context_name, - ], $ospf_tos); + if (! $port) { + // didn't find port by IP, try harder + $port = $ospf_ports_by_ip->where(fn ($p) => str_starts_with($p->ospf_port_id, $ip))->first(); + } + + if ($port) { + $port->fill($ospf_tos)->save(); + } else { + \Log::error("No port found when fetching metrics for $ip"); + } + + return $port; }); echo $ospf_tos_metrics->count();