MAC Vendor OUI use scheduler (#15187)

* MAC Vendor OUI use scheduler
Add command to update `lnms maintenance:fetch-ouis`
Show vendor column in tables if mac_oui.enabled is set to true
Improve scheduler validation handle non-standard install directories and systems without systemd
Add index to table to improve speed and improve mac->vendor lookup speed
Scheduled weekly with random wait to prevent stampeding herd issues for upstream
drop oui update from daily

* MAC Vendor OUI use scheduler
Add command to update `lnms maintenance:fetch-ouis`
Show vendor column in tables if mac_oui.enabled is set to true

* Lint fixes and better prefix detection

* update schema file
This commit is contained in:
Tony Murray
2023-08-03 19:29:30 -05:00
committed by GitHub
parent 5a56e9081e
commit 12f8bb2040
19 changed files with 273 additions and 135 deletions

View File

@@ -26,6 +26,7 @@
namespace LibreNMS\Util;
use App\Models\Device;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use LibreNMS\Config;
@@ -151,20 +152,29 @@ class Rewrite
* Extract the OUI and match it against database values
*
* @param string $mac
* @return string|null
* @return string
*/
public static function readableOUI($mac)
public static function readableOUI($mac): string
{
$oui = substr($mac, 0, 6);
$result = DB::table('vendor_ouis')->where('oui', $oui)->value('vendor');
$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');
if ($result === 'IEEE Registration Authority') {
// Then we may have a shorter prefix, so let's try them one after the other, ordered by probability
$result = DB::table('vendor_ouis')->whereIn('oui', [substr($mac, 0, 9), substr($mac, 0, 7)])->value('vendor');
if (count($results) == 1) {
return Arr::first($results);
}
return $result ?: '';
// Then we may have a shorter prefix, so let's try them one after the other
foreach ($results as $oui => $vendor) {
if (str_starts_with($mac, $oui)) {
return $vendor;
}
}
return '';
}
/**

View File

@@ -23,6 +23,8 @@
namespace LibreNMS\Validations;
use Illuminate\Support\Facades\Cache;
use LibreNMS\Config;
use LibreNMS\ValidationResult;
use LibreNMS\Validator;
class Scheduler extends BaseValidation
@@ -36,8 +38,47 @@ class Scheduler extends BaseValidation
public function validate(Validator $validator): void
{
if (! Cache::has('scheduler_working')) {
$validator->fail('Scheduler is not running',
"cp /opt/librenms/dist/librenms-scheduler.service /opt/librenms/dist/librenms-scheduler.timer /etc/systemd/system/\nsystemctl enable librenms-scheduler.timer\nsystemctl start librenms-scheduler.timer");
$commands = $this->generateCommands($validator);
$validator->result(ValidationResult::fail('Scheduler is not running')->setFix($commands));
}
}
/**
* @param Validator $validator
* @return array
*/
private function generateCommands(Validator $validator): array
{
$commands = [];
$systemctl_bin = Config::locateBinary('systemctl');
$base_dir = rtrim($validator->getBaseDir(), '/');
if (is_executable($systemctl_bin)) {
// systemd exists
if ($base_dir === '/opt/librenms') {
// standard install dir
$commands[] = 'sudo cp /opt/librenms/dist/librenms-scheduler.service /opt/librenms/dist/librenms-scheduler.timer /etc/systemd/system/';
} else {
// non-standard install dir
$commands[] = "sudo sh -c 'sed \"s#/opt/librenms#$base_dir#\" $base_dir/dist/librenms-scheduler.service > /etc/systemd/system/librenms-scheduler.service'";
$commands[] = "sudo sh -c 'sed \"s#/opt/librenms#$base_dir#\" $base_dir/dist/librenms-scheduler.timer > /etc/systemd/system/librenms-scheduler.timer'";
}
$commands[] = 'sudo systemctl enable librenms-scheduler.timer';
$commands[] = 'sudo systemctl start librenms-scheduler.timer';
return $commands;
}
// non-systemd use cron
if ($base_dir === '/opt/librenms') {
$commands[] = 'sudo cp /opt/librenms/dist/librenms-scheduler.cron /etc/cron.d/';
return $commands;
}
// non-standard install dir
$commands[] = "sudo sh -c 'sed \"s#/opt/librenms#$base_dir#\" $base_dir/dist/librenms-scheduler.cron > /etc/cron.d/librenms-scheduler.cron'";
return $commands;
}
}