Add MIB to OS helper to tests (#13795)

This commit is contained in:
Jellyfrog 2022-02-18 16:17:40 +01:00 committed by GitHub
parent 0c08670acc
commit 513fbf06e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,6 +26,7 @@
namespace LibreNMS\Util; namespace LibreNMS\Util;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Symfony\Component\Process\Process;
class FileCategorizer extends Categorizer class FileCategorizer extends Categorizer
{ {
@ -79,6 +80,8 @@ class FileCategorizer extends Categorizer
public function categorize() public function categorize()
{ {
// This can't be a normal addCategory() function since it returns multiple results
$this->osFromMibs();
parent::categorize(); parent::categorize();
// split out os // split out os
@ -99,6 +102,47 @@ class FileCategorizer extends Categorizer
return file_exists("includes/definitions/$os.yaml") ? $os : null; 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) private function osFromFile($file)
{ {
if (Str::startsWith($file, 'includes/definitions/')) { if (Str::startsWith($file, 'includes/definitions/')) {