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 <bot@styleci.io>
This commit is contained in:
Tony Murray
2023-10-26 11:21:08 -05:00
committed by GitHub
parent 7e0b41a05a
commit db17036f5d

View File

@@ -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();