Fix a few Db* to Eloquent requests (#14278)

* Eloquent for 2 requests

* Eloquent for 1 request

* a few in Dell

* Model

* and more with timos

* and more with timos

* and firebrick

* firebrick

* one query instead of many

* Should use collection get

* Update timos.inc.php

* Update dell-os10.inc.php

* avoid changing timos as it breaks something

* new try with timos

* deps for BGP

* revert deps for BGP

* style

* style

* deps for BGP, 2nd try

* typo

* revert aos7

* fix create

* firebricktests

* firebrick

* firebrick

* cipsec-fix

* cipsec

* timos fix 100th time :)

* ./scripts/save-test-data.php -m os,ports,processors,mempools,vrf,sensors,bgp-peers,mpls,ospf  -o timos -v 7705

* remove timos from this PR

Co-authored-by: Tony Murray <murraytony@gmail.com>
This commit is contained in:
PipoCanaja
2022-10-02 16:04:08 +02:00
committed by GitHub
parent e4451714e2
commit daa8c757f6
9 changed files with 85 additions and 50 deletions

View File

@@ -39,6 +39,7 @@ class LegacyModule implements Module
/** @var array */
private $module_deps = [
'arp-table' => ['ports'],
'bgp-peers' => ['ports', 'vrf'],
'cisco-mac-accounting' => ['ports'],
'fdb-table' => ['ports', 'vlans'],
'vlans' => ['ports'],

View File

@@ -35,7 +35,22 @@ class BgpPeer extends DeviceRelatedModel
public $timestamps = false;
protected $table = 'bgpPeers';
protected $primaryKey = 'bgpPeer_id';
protected $fillable = [
'vrf_id',
'bgpPeerIdentifier',
'bgpPeerRemoteAs',
'bgpPeerState',
'bgpPeerAdminStatus',
'bgpLocalAddr',
'bgpPeerRemoteAddr',
'bgpPeerInUpdates',
'bgpPeerOutUpdates',
'bgpPeerInTotalMessages',
'bgpPeerOutTotalMessages',
'bgpPeerFsmEstablishedTime',
'bgpPeerInUpdateElapsedTime',
'astext',
];
// ---- Query scopes ----
public function scopeInAlarm(Builder $query)

View File

@@ -24,6 +24,8 @@
* @author LibreNMS Contributors
*/
use App\Models\BgpPeer;
use App\Models\Vrf;
use LibreNMS\Config;
use LibreNMS\Util\IP;
@@ -39,11 +41,11 @@ if (Config::get('enable_bgp')) {
}
unset($bgpPeersCache);
$vrfs = DeviceCache::getPrimary()->vrfs->pluck('vrf_id', 'vrf_oid');
foreach ($bgpPeers as $vrfInstance => $peer) {
$vrfId = dbFetchCell('SELECT vrf_id from `vrfs` WHERE vrf_oid = ?', [$vrfInstance]);
if (is_null($vrfId)) {
$vrfId = 1; // According to the MIB
}
$vrfId = $vrfs->get($vrfInstance, 1); // According to the MIB
foreach ($peer as $address => $value) {
// resolve AS number by DNS_TXT record
$astext = get_astext($value['os10bgp4V2PeerRemoteAs']);
@@ -52,9 +54,8 @@ if (Config::get('enable_bgp')) {
// Setting it here avoids the code that resets it to null if not found in BGP4-MIB.
$bgpLocalAs = $value['os10bgp4V2PeerLocalAs'];
if (dbFetchCell('SELECT count(*) FROM `bgpPeers` WHERE device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]) == 0) {
if (! DeviceCache::getPrimary()->bgppeers()->where('bgpPeerIdentifier', $address)->where('vrf_id', $vrfId)->exists()) {
$row = [
'device_id' => $device['device_id'],
'vrf_id' => $vrfId,
'bgpPeerIdentifier' => $address,
'bgpPeerRemoteAs' => $value['os10bgp4V2PeerRemoteAs'],
@@ -70,7 +71,7 @@ if (Config::get('enable_bgp')) {
'bgpPeerInUpdateElapsedTime' => 0,
'astext' => $astext,
];
dbInsert($row, 'bgpPeers');
DeviceCache::getPrimary()->bgppeers()->create($row);
if (Config::get('autodiscovery.bgp')) {
$name = gethostbyaddr($address);
@@ -78,7 +79,7 @@ if (Config::get('enable_bgp')) {
}
echo '+';
} else {
dbUpdate(['bgpPeerRemoteAs' => $value['os10bgp4V2PeerRemoteAs'], 'astext' => $astext], 'bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]);
BgpPeer::where('bgpPeerRemoteAs', $value['os10bgp4V2PeerRemoteAs'])->where('astext', $astext)->update(['bgpPeerIdentifier' => $address, 'device_id' => $device['device_id'], 'vrf_id' => $vrfId]);
echo '.';
}
}
@@ -97,8 +98,8 @@ if (Config::get('enable_bgp')) {
}
// clean up peers
if (dbFetchCell('SELECT count(*) FROM `vrfs` WHERE `device_id` = ?', [$device['device_id']]) == 0) {
$peers = dbFetchRows('SELECT `vrf_id`, `bgpPeerIdentifier` FROM `bgpPeers` WHERE `device_id` = ?', [$device['device_id']]);
if (Vrf::where('device_id', $device['device_id'])->count() == 0) {
$peers = BgpPeer::select('vrf_id', 'bgpPeerIdentifier')->where('device_id', $device['device_id']);
} else {
$peers = dbFetchRows('SELECT `B`.`vrf_id` AS `vrf_id`, `bgpPeerIdentifier` FROM `bgpPeers` AS B LEFT JOIN `vrfs` AS V ON `B`.`vrf_id` = `V`.`vrf_id` WHERE `B`.`device_id` = ?', [$device['device_id']]);
}
@@ -107,7 +108,7 @@ if (Config::get('enable_bgp')) {
$address = $peer['bgpPeerIdentifier'];
if (empty($bgpPeers[$vrfInstance][$address])) {
$deleted = dbDelete('bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]);
$deleted = BgpPeer::where('device_id', $device['device_id'])->where('bgpPeerIdentifier', $address)->where('vrf_id', $vrfId)->delete();
echo str_repeat('-', $deleted);
echo PHP_EOL;

View File

@@ -23,6 +23,7 @@
* @author Chris Malton (@cjsoftuk)
*/
use App\Models\Vrf;
use LibreNMS\Config;
use LibreNMS\Util\IP;
@@ -49,23 +50,22 @@ foreach ($bgpPeers as $vrfId => $vrf) {
// Force to null to avoid 0s going to the DB instead of Nulls
$vrfId = null;
} else {
$checkVrf = ' AND vrf_id = ? ';
$vrfs = [
'vrf_oid' => 'firebrick.' . $vrfId,
'vrf_name' => $vrfId,
'device_id' => $device['device_id'],
];
if (! dbFetchCell('SELECT COUNT(*) FROM vrfs WHERE device_id = ? AND `vrf_oid`=?', [$device['device_id'], $vrfs['vrf_oid']])) {
if (! DeviceCache::getPrimary()->vrfs()->where('vrf_oid', $vrfs['vrf_oid'])->exists()) {
//Should we insert a VRF here ? We are not in the VRF module !
dbInsert($vrfs, 'vrfs');
}
}
foreach ($vrf as $address => $value) {
$bgpLocalAs = $value['fbBgpPeerLocalAS'] ?? $bgpLocalAs;
$astext = get_astext($value['fbBgpPeerRemoteAS']);
if (dbFetchCell('SELECT COUNT(*) from `bgpPeers` WHERE device_id = ? AND bgpPeerIdentifier = ? ' . $checkVrf, [$device['device_id'], $address, $vrfId]) < '1') {
if (! DeviceCache::getPrimary()->bgppeers()->where('bgpPeerIdentifier', $address)->where('vrf_id', $vrfId)->exists()) {
$peers = [
'device_id' => $device['device_id'],
'vrf_id' => $vrfId,
'bgpPeerIdentifier' => $address,
'bgpPeerRemoteAs' => $value['fbBgpPeerRemoteAS'],
@@ -81,20 +81,30 @@ foreach ($bgpPeers as $vrfId => $vrf) {
'bgpPeerInUpdateElapsedTime' => 0,
'astext' => $astext,
];
dbInsert($peers, 'bgpPeers');
DeviceCache::getPrimary()->bgppeers()->create($peers);
if (Config::get('autodiscovery.bgp')) {
$name = gethostbyaddr($address);
discover_new_device($name, $device, 'BGP');
}
echo '+';
} else {
dbUpdate(['bgpPeerRemoteAs' => $value['fbBgpPeerRemoteAS'], 'astext' => $astext], 'bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]);
$peers = [
'bgpPeerRemoteAs' => $value['fbBgpPeerRemoteAS'],
'astext' => $astext,
];
DeviceCache::getPrimary()->bgppeers()->update([
'bgpPeerIdentifier' => $address,
'vrf_id' => $vrfId,
],
$peers);
echo '.';
}
}
}
// clean up peers
$peers = dbFetchRows('SELECT `vrf_id`, `bgpPeerIdentifier` FROM `bgpPeers` WHERE `device_id` = ?', [$device['device_id']]);
$peers = DeviceCache::getPrimary()->bgppeers()->select('vrf_id', 'bgpPeerIdentifier');
foreach ($peers as $value) {
$vrfId = $value['vrf_id'];
$address = $value['bgpPeerIdentifier'];
@@ -102,7 +112,7 @@ foreach ($peers as $value) {
// Cleanup code to deal with 0 vs Null in the DB
if ($vrfId === 0) {
// Database says it's table 0 - which is wrong. It should be "null" for global table
$deleted = dbDelete('bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]);
$deleted = DeviceCache::getPrimary()->bgppeers()->where('bgpPeerIdentifier', $address)->where('vrf_id', $vrfId)->delete();
echo str_repeat('-', $deleted);
continue;
} else {
@@ -110,11 +120,7 @@ foreach ($peers as $value) {
}
if (empty($bgpPeers[$testVrfId][$address])) {
if ($vrfId === null) {
$deleted = dbDelete('bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id IS NULL', [$device['device_id'], $address]);
} else {
$deleted = dbDelete('bgpPeers', 'device_id = ? AND bgpPeerIdentifier = ? AND vrf_id = ?', [$device['device_id'], $address, $vrfId]);
}
$deleted = DeviceCache::getPrimary()->bgppeers()->where('bgpPeerIdentifier', $address)->where('vrf_id', $vrfId)->delete();
echo str_repeat('-', $deleted);
}
}

View File

@@ -24,6 +24,7 @@
* @author PipoCanaja
*/
use App\Models\BgpPeer;
use LibreNMS\Config;
use LibreNMS\Util\IP;
@@ -38,7 +39,7 @@ if (Config::get('enable_bgp')) {
// So if we have HUAWEI BGP entries or if we don't have anything from HUAWEI nor BGP4-MIB
if (count($bgpPeersCache) > 0 || count($bgpPeersCache_ietf) == 0) {
$vrfs = dbFetchRows('SELECT vrf_id, vrf_name from `vrfs` WHERE device_id = ?', [$device['device_id']]);
$vrfs = DeviceCache::getPrimary()->vrfs()->select('vrf_id', 'vrf_name');
foreach ($vrfs as $vrf) {
$map_vrf['byId'][$vrf['vrf_id']]['vrf_name'] = $vrf['vrf_name'];
$map_vrf['byName'][$vrf['vrf_name']]['vrf_id'] = $vrf['vrf_id'];
@@ -88,7 +89,7 @@ if (Config::get('enable_bgp')) {
foreach ($vrf as $address => $value) {
$astext = get_astext($value['hwBgpPeerRemoteAs']);
if (dbFetchCell('SELECT COUNT(*) from `bgpPeers` WHERE device_id = ? AND bgpPeerIdentifier = ? ' . $checkVrf, [$device['device_id'], $address, $vrfId]) < '1') {
if (! DeviceCache::getPrimary()->bgppeers()->where('bgpPeerIdentifier', $address)->where('vrf_id', $vrfId)->exists()) {
$peers = [
'device_id' => $device['device_id'],
'vrf_id' => $vrfId,
@@ -137,7 +138,7 @@ if (Config::get('enable_bgp')) {
}
}
// clean up peers
$peers = dbFetchRows('SELECT `vrf_id`, `bgpPeerIdentifier` FROM `bgpPeers` WHERE `device_id` = ?', [$device['device_id']]);
$peers = DeviceCache::getPrimary()->bgppeers()->select('vrf_id', 'bgpPeerIdentifier');
foreach ($peers as $value) {
$vrfId = $value['vrf_id'];
$vrfName = $map_vrf['byId'][$vrfId]['vrf_name'];
@@ -148,7 +149,7 @@ if (Config::get('enable_bgp')) {
if ((empty($vrfId) && empty($bgpPeers[''][$address])) ||
(! empty($vrfId) && ! empty($vrfName) && empty($bgpPeers[$vrfName][$address])) ||
(! empty($vrfId) && empty($vrfName))) {
$deleted = \App\Models\BgpPeer::where('device_id', $device['device_id'])
$deleted = BgpPeer::where('device_id', $device['device_id'])
->where('bgpPeerIdentifier', $address)
->where('vrf_id', $vrfId)
->delete();

View File

@@ -3525,7 +3525,6 @@
{
"peer_port": 0,
"peer_addr": "<private>",
"local_addr": "<private>",
"local_port": 0,
"tunnel_name": "<private>",
"tunnel_status": ""

View File

@@ -10,11 +10,11 @@
"version": "V1.55.126",
"hardware": "FB2901",
"features": null,
"location": "<private>",
"os": "firebrick",
"type": "network",
"serial": null,
"icon": "firebrick.svg",
"location": "<private>"
"icon": "firebrick.svg"
}
]
},

View File

@@ -10,11 +10,11 @@
"version": "V1.56.000D29",
"hardware": "FB6010",
"features": null,
"location": "<private>",
"os": "firebrick",
"type": "network",
"serial": null,
"icon": "firebrick.svg",
"location": "<private>"
"icon": "firebrick.svg"
}
]
},
@@ -1819,6 +1819,7 @@
"bgpPeerLastErrorCode": null,
"bgpPeerLastErrorSubCode": null,
"bgpPeerLastErrorText": null,
"bgpPeerIface": null,
"bgpLocalAddr": "00 00 00 00",
"bgpPeerRemoteAddr": "0A 02 03 04",
"bgpPeerDescr": "",
@@ -1830,8 +1831,7 @@
"bgpPeerInUpdateElapsedTime": 0,
"context_name": null,
"bgpLocalAs": 20712,
"vrfLocalAs": null,
"bgpPeerIface": null
"vrfLocalAs": null
},
{
"astext": "AS20712 Andrews & Arnold Ltd, GB",
@@ -1842,6 +1842,7 @@
"bgpPeerLastErrorCode": null,
"bgpPeerLastErrorSubCode": null,
"bgpPeerLastErrorText": null,
"bgpPeerIface": null,
"bgpLocalAddr": "0A 02 03 06",
"bgpPeerRemoteAddr": "0A 02 03 05",
"bgpPeerDescr": "",
@@ -1853,8 +1854,7 @@
"bgpPeerInUpdateElapsedTime": 0,
"context_name": null,
"bgpLocalAs": 20712,
"vrfLocalAs": null,
"bgpPeerIface": null
"vrfLocalAs": null
},
{
"astext": "AS20712 Andrews & Arnold Ltd, GB",
@@ -1865,6 +1865,7 @@
"bgpPeerLastErrorCode": null,
"bgpPeerLastErrorSubCode": null,
"bgpPeerLastErrorText": null,
"bgpPeerIface": null,
"bgpLocalAddr": "FD 01 08 B0 00 00 00 FB 00 00 00 00 00 00 00 02",
"bgpPeerRemoteAddr": "FD 01 08 B0 00 00 00 FB 00 00 00 00 00 00 00 01",
"bgpPeerDescr": "",
@@ -1876,8 +1877,7 @@
"bgpPeerInUpdateElapsedTime": 0,
"context_name": null,
"bgpLocalAs": 20712,
"vrfLocalAs": null,
"bgpPeerIface": null
"vrfLocalAs": null
}
]
},
@@ -1892,6 +1892,7 @@
"bgpPeerLastErrorCode": null,
"bgpPeerLastErrorSubCode": null,
"bgpPeerLastErrorText": null,
"bgpPeerIface": null,
"bgpLocalAddr": "0.0.0.0",
"bgpPeerRemoteAddr": "10.2.3.4",
"bgpPeerDescr": "",
@@ -1903,8 +1904,7 @@
"bgpPeerInUpdateElapsedTime": 0,
"context_name": null,
"bgpLocalAs": 20712,
"vrfLocalAs": null,
"bgpPeerIface": null
"vrfLocalAs": null
},
{
"astext": "AS20712 Andrews & Arnold Ltd, GB",
@@ -1915,6 +1915,7 @@
"bgpPeerLastErrorCode": null,
"bgpPeerLastErrorSubCode": null,
"bgpPeerLastErrorText": null,
"bgpPeerIface": null,
"bgpLocalAddr": "10.2.3.6",
"bgpPeerRemoteAddr": "10.2.3.5",
"bgpPeerDescr": "",
@@ -1926,8 +1927,7 @@
"bgpPeerInUpdateElapsedTime": 0,
"context_name": null,
"bgpLocalAs": 20712,
"vrfLocalAs": null,
"bgpPeerIface": null
"vrfLocalAs": null
},
{
"astext": "AS20712 Andrews & Arnold Ltd, GB",
@@ -1938,6 +1938,7 @@
"bgpPeerLastErrorCode": null,
"bgpPeerLastErrorSubCode": null,
"bgpPeerLastErrorText": null,
"bgpPeerIface": null,
"bgpLocalAddr": "fd01:08b0:0000:00fb:0000:0000:0000:0002",
"bgpPeerRemoteAddr": "fd01:8b0:0:fb::1",
"bgpPeerDescr": "",
@@ -1949,8 +1950,7 @@
"bgpPeerInUpdateElapsedTime": 0,
"context_name": null,
"bgpLocalAs": 20712,
"vrfLocalAs": null,
"bgpPeerIface": null
"vrfLocalAs": null
}
]
}
@@ -1961,7 +1961,6 @@
{
"peer_port": 0,
"peer_addr": "0.0.0.0",
"local_addr": "127.1.6.2",
"local_port": 0,
"tunnel_name": "House",
"tunnel_status": "waiting"
@@ -1969,12 +1968,25 @@
{
"peer_port": 0,
"peer_addr": "10.2.3.4",
"local_addr": "127.1.6.2",
"local_port": 0,
"tunnel_name": "ToToothless",
"tunnel_status": "waiting"
}
]
}
},
"vrf": {
"discovery": {
"vrfs": [
{
"vrf_oid": "firebrick.44",
"vrf_name": "44",
"bgpLocalAs": null,
"mplsVpnVrfRouteDistinguisher": null,
"mplsVpnVrfDescription": "",
"ifIndices": null
}
]
}
}
}
}

View File

@@ -36,7 +36,7 @@ ports:
order_by: ports.ifIndex, ports.ifDescr, ports.ifName
cipsec-tunnels:
ipsec_tunnels:
excluded_fields: [tunnel_id, device_id]
excluded_fields: [local_addr, tunnel_id, device_id]
order_by: peer_addr
cisco-pw:
pseudowires: