Skip to content

Commit

Permalink
FDB Tables improve performance
Browse files Browse the repository at this point in the history
Reduce unnecessary sql queries, by using a relationship
Cache vendor oui lookups
  • Loading branch information
murrant committed Sep 19, 2023
1 parent 35406b4 commit 201759b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 58 deletions.
11 changes: 7 additions & 4 deletions LibreNMS/Util/Rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use App\Models\Device;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use LibreNMS\Config;

Expand Down Expand Up @@ -158,10 +159,12 @@ public static function readableOUI($mac): string
{
$oui = substr($mac, 0, 6);

$results = DB::table('vendor_ouis')
->where('oui', 'like', "$oui%") // possible matches
->orderBy('oui', 'desc') // so we can check longer ones first if we have them
->pluck('vendor', 'oui');
$results = Cache::remember($oui, 21600, function () use ($oui) {
return DB::table('vendor_ouis')
->where('oui', 'like', "$oui%") // possible matches
->orderBy('oui', 'desc') // so we can check longer ones first if we have them
->pluck('vendor', 'oui');
});

if (count($results) == 1) {
return Arr::first($results);
Expand Down
85 changes: 31 additions & 54 deletions app/Http/Controllers/Table/FdbTablesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use App\Models\Vlan;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use LibreNMS\Util\IP;
use LibreNMS\Util\Rewrite;
Expand All @@ -39,7 +40,6 @@
class FdbTablesController extends TableController
{
protected $macCountCache = [];
protected $ipCache = [];

protected function rules()
{
Expand Down Expand Up @@ -67,7 +67,8 @@ protected function filterFields($request)
*/
protected function baseQuery($request)
{
return PortsFdb::hasAccess($request->user())->with(['device', 'port', 'vlan'])->select('ports_fdb.*');
return PortsFdb::hasAccess($request->user())
->with(['device', 'port', 'vlan', 'ipv4Addresses']);
}

/**
Expand Down Expand Up @@ -158,17 +159,15 @@ public function sort($request, $query)
*/
public function formatItem($fdb_entry)
{
$ip_info = $this->findIps($fdb_entry->mac_address);

$item = [
'device' => $fdb_entry->device ? Url::deviceLink($fdb_entry->device) : '',
'mac_address' => Rewrite::readableMac($fdb_entry->mac_address),
'mac_oui' => Rewrite::readableOUI($fdb_entry->mac_address),
'ipv4_address' => $ip_info['ips']->implode(', '),
'ipv4_address' => $fdb_entry->ipv4Addresses->implode(', '),
'interface' => '',
'vlan' => $fdb_entry->vlan ? $fdb_entry->vlan->vlan_vlan : '',
'description' => '',
'dnsname' => $ip_info['dns'],
'dnsname' => $this->resolveDns($fdb_entry->ipv4Addresses),
'first_seen' => 'unknown',
'last_seen' => 'unknown',
];
Expand Down Expand Up @@ -198,9 +197,9 @@ public function formatItem($fdb_entry)

/**
* @param string $ip
* @return \Illuminate\Support\Collection
* @return Collection
*/
protected function findMacs($ip): \Illuminate\Support\Collection
protected function findMacs($ip): Collection
{
$port_id = \Request::get('port_id');
$device_id = \Request::get('device_id');
Expand All @@ -217,9 +216,9 @@ protected function findMacs($ip): \Illuminate\Support\Collection

/**
* @param string $vlan
* @return \Illuminate\Support\Collection
* @return Collection
*/
protected function findVlans($vlan): \Illuminate\Support\Collection
protected function findVlans($vlan): Collection
{
$port_id = \Request::get('port_id');
$device_id = \Request::get('device_id');
Expand All @@ -238,9 +237,9 @@ protected function findVlans($vlan): \Illuminate\Support\Collection

/**
* @param string $ifAlias
* @return \Illuminate\Support\Collection
* @return Collection
*/
protected function findPorts($ifAlias): \Illuminate\Support\Collection
protected function findPorts($ifAlias): Collection
{
$port_id = \Request::get('port_id');
$device_id = \Request::get('device_id');
Expand All @@ -255,38 +254,22 @@ protected function findPorts($ifAlias): \Illuminate\Support\Collection
->pluck('port_id');
}

/**
* @param string $mac_address
* @return array
*/
protected function findIps($mac_address): array
private function resolveDns(Collection $ips): string
{
if (! isset($this->ipCache[$mac_address])) {
$ips = Ipv4Mac::where('mac_address', $mac_address)
->groupBy('ipv4_address')
->pluck('ipv4_address');

$dns = 'N/A';

// only fetch DNS if the column is visible
if (\Request::get('dns') == 'true') {
// don't try too many dns queries, this is the slowest part
foreach ($ips->take(3) as $ip) {
$hostname = gethostbyaddr($ip);
if (! IP::isValid($hostname)) {
$dns = $hostname;
break;
}
$dns = 'N/A';

// only fetch DNS if the column is visible
if (\Request::get('dns') == 'true') {
// don't try too many dns queries, this is the slowest part
foreach ($ips->take(3) as $ip) {
$hostname = gethostbyaddr($ip);
if (! IP::isValid($hostname)) {
return $hostname;
}
}

$this->ipCache[$mac_address] = [
'ips' => $ips,
'dns' => $dns,
];
}

return $this->ipCache[$mac_address];
return $dns;
}

/**
Expand All @@ -308,31 +291,25 @@ protected function getMacCount($port)
* @param string $vendor
* @return array
*/
protected function ouisFromVendor($vendor)
protected function ouisFromVendor(String $vendor): array
{
$matching_ouis = DB::table('vendor_ouis')
return DB::table('vendor_ouis')
->where('vendor', 'LIKE', '%' . $vendor . '%')
->pluck('oui')
->toArray();

return $matching_ouis;
}

/**
* Get all port ids from vendor OUIs
*
* @param array $vendor_ouis
* @return Builder
*/
protected function findPortsByOui($vendor_ouis, $query)
protected function findPortsByOui(array $vendor_ouis, Builder $query): Builder
{
$condition = '';
foreach ($vendor_ouis as $oui) {
$clean_oui = str_replace(':', '', $oui);
$condition .= " ports_fdb.mac_address LIKE '$clean_oui%' OR";
}
$condition = rtrim($condition, ' OR');
$query->whereRaw($condition);
$query->where(function (Builder $query) use ($vendor_ouis) {
foreach ($vendor_ouis as $oui) {
$clean_oui = str_replace(':', '', $oui);
$query->orWhere('ports_fdb.mac_address', 'LIKE', "$clean_oui%");
}
});

return $query; // Return the query builder instance
}
Expand Down
6 changes: 6 additions & 0 deletions app/Models/PortsFdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class PortsFdb extends PortRelatedModel
{
Expand All @@ -21,4 +22,9 @@ public function vlan(): BelongsTo
{
return $this->belongsTo(\App\Models\Vlan::class, 'vlan_id', 'vlan_id');
}

public function ipv4Addresses(): HasMany
{
return $this->hasMany(\App\Models\Ipv4Mac::class, 'mac_address', 'mac_address');
}
}

0 comments on commit 201759b

Please sign in to comment.