From 513fbf06e200c0b4544af0cc14deeb3fba55c3da Mon Sep 17 00:00:00 2001 From: Jellyfrog Date: Fri, 18 Feb 2022 16:17:40 +0100 Subject: [PATCH] Add MIB to OS helper to tests (#13795) --- LibreNMS/Util/FileCategorizer.php | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/LibreNMS/Util/FileCategorizer.php b/LibreNMS/Util/FileCategorizer.php index e5790b4095..19e84cb704 100644 --- a/LibreNMS/Util/FileCategorizer.php +++ b/LibreNMS/Util/FileCategorizer.php @@ -26,6 +26,7 @@ namespace LibreNMS\Util; use Illuminate\Support\Str; +use Symfony\Component\Process\Process; class FileCategorizer extends Categorizer { @@ -79,6 +80,8 @@ class FileCategorizer extends Categorizer public function categorize() { + // This can't be a normal addCategory() function since it returns multiple results + $this->osFromMibs(); parent::categorize(); // split out os @@ -99,6 +102,47 @@ class FileCategorizer extends Categorizer return file_exists("includes/definitions/$os.yaml") ? $os : null; } + private function osFromMibs(): void + { + $mibs = []; + + foreach ($this->items as $file) { + if (Str::startsWith($file, 'mibs/')) { + $mibs[] = basename($file, '.mib'); + } + } + + if (empty($mibs)) { + return; + } + + $grep = new Process( + [ + 'grep', + '--fixed-strings', + '--recursive', + '--files-with-matches', + '--file=-', + '--', + 'includes/definitions/', + 'includes/discovery/', + 'includes/polling/', + 'LibreNMS/OS/', + ], + null, + null, + implode("\n", $mibs) + ); + + $grep->run(); + + foreach (explode("\n", trim($grep->getOutput())) as $item) { + if (($os_name = $this->osFromFile($item)) !== null) { + $this->categorized['os-files'][] = ['os' => $os_name, 'file' => $item]; + } + } + } + private function osFromFile($file) { if (Str::startsWith($file, 'includes/definitions/')) {