diff --git a/LibreNMS/Cache/Device.php b/LibreNMS/Cache/Device.php index a5cfbca988..227c9bd64c 100644 --- a/LibreNMS/Cache/Device.php +++ b/LibreNMS/Cache/Device.php @@ -111,7 +111,6 @@ class Device return new \App\Models\Device; } - $device->loadOs(); $this->devices[$device->device_id] = $device; return $device; } diff --git a/LibreNMS/Config.php b/LibreNMS/Config.php index ad8b50c9c9..0a056f6845 100644 --- a/LibreNMS/Config.php +++ b/LibreNMS/Config.php @@ -170,7 +170,6 @@ class Config /** * Get a setting from the $config['os'] array using the os of the given device - * If that is not set, fallback to the same global config key * * @param string $os The os name * @param string $key period separated config variable name @@ -180,21 +179,19 @@ class Config public static function getOsSetting($os, $key, $default = null) { if ($os) { + \LibreNMS\Util\OS::loadDefinition($os); + if (isset(self::$config['os'][$os][$key])) { return self::$config['os'][$os][$key]; } - if (!Str::contains($key, '.')) { - return self::get($key, $default); - } - $os_key = "os.$os.$key"; if (self::has($os_key)) { return self::get($os_key); } } - return self::get($key, $default); + return $default; } /** @@ -210,7 +207,7 @@ class Config public static function getCombined($os, $key, $default = array()) { if (!self::has($key)) { - return self::get("os.$os.$key", $default); + return self::getOsSetting($os, $key, $default); } if (!isset(self::$config['os'][$os][$key])) { diff --git a/LibreNMS/Util/Graph.php b/LibreNMS/Util/Graph.php index e6f8d087b9..dac4feff9d 100644 --- a/LibreNMS/Util/Graph.php +++ b/LibreNMS/Util/Graph.php @@ -81,4 +81,18 @@ class Graph { return Config::get("graph_types.$type.$subtype.section") == 'mib'; } + + public static function getOverviewGraphsForDevice($device) + { + if ($device->snmp_disable) { + return Config::getOsSetting('ping', 'over'); + } + + if ($graphs = Config::getOsSetting($device->os, 'over')) { + return $graphs; + } + + $os_group = Config::getOsSetting($device->os, 'group'); + return Config::get("os_group.$os_group.over", Config::get('os.default.over')); + } } diff --git a/LibreNMS/Util/OS.php b/LibreNMS/Util/OS.php new file mode 100644 index 0000000000..e344e6f244 --- /dev/null +++ b/LibreNMS/Util/OS.php @@ -0,0 +1,115 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2020 Tony Murray + * @author Tony Murray + */ + +namespace LibreNMS\Util; + +use App\Models\Device; +use LibreNMS\Config; +use LibreNMS\DB\Eloquent; +use Log; +use Symfony\Component\Yaml\Yaml; + +class OS +{ + /** + * Load os from yaml into config if not already loaded, preserving user os config + * @param string $os + */ + public static function loadDefinition($os) + { + if (!Config::get("os.$os.definition_loaded")) { + $yaml_file = base_path("/includes/definitions/$os.yaml"); + if (file_exists($yaml_file)) { + $os_def = Yaml::parse(file_get_contents($yaml_file)); + + Config::set("os.$os", array_replace_recursive($os_def, Config::get("os.$os", []))); + Config::set("os.$os.definition_loaded", true); + } + } + } + + /** + * Load all OS, optionally load just the OS used by existing devices + * Default cache time is 1 day. Controlled by os_def_cache_time. + * + * @param bool $existing Only load OS that have existing OS in the database + * @param bool $cached Load os definitions from the cache file + */ + public static function loadAllDefinitions($existing = false, $cached = true) + { + $install_dir = \LibreNMS\Config::get('install_dir'); + $cache_file = $install_dir . '/cache/os_defs.cache'; + if ($cached && is_file($cache_file) && (time() - filemtime($cache_file) < \LibreNMS\Config::get('os_def_cache_time'))) { + // Cached + $os_defs = unserialize(file_get_contents($cache_file)); + if ($existing) { + // remove unneeded os + $exists = Device::query()->distinct()->pluck('os')->flip()->all(); + $os_defs = array_intersect_key($os_defs, $exists); + } + \LibreNMS\Config::set('os', array_replace_recursive($os_defs, \LibreNMS\Config::get('os'))); + } else { + // load from yaml + if ($existing && Eloquent::isConnected()) { + $os_list = Device::query()->distinct()->pluck('os'); + } else { + $os_list = glob($install_dir . '/includes/definitions/*.yaml'); + } + foreach ($os_list as $file) { + $os = basename($file, '.yaml'); + self::loadDefinition($os); + } + } + } + + /** + * Update the OS cache file cache/os_defs.cache + * @param bool $force + * @return bool true if the cache was updated + */ + public static function updateCache($force = false) + { + $install_dir = Config::get('install_dir'); + $cache_file = "$install_dir/cache/os_defs.cache"; + $cache_keep_time = Config::get('os_def_cache_time', 86400) - 7200; // 2hr buffer + + if ($force === true || !is_file($cache_file) || time() - filemtime($cache_file) > $cache_keep_time) { + Log::debug('Updating os_def.cache'); + + // remove previously cached os settings and replace with user settings + $config = ['os' => []]; // local $config variable, not global + include "$install_dir/config.php"; // FIXME load db settings too or don't load config.php + Config::set('os', $config['os']); + + // load the os defs fresh from cache (merges with existing OS settings) + self::loadAllDefinitions(false, false); + + file_put_contents($cache_file, serialize(Config::get('os'))); + return true; + } + + return false; + } +} diff --git a/LibreNMS/Util/Url.php b/LibreNMS/Util/Url.php index fd830da13d..bad135e7ec 100644 --- a/LibreNMS/Util/Url.php +++ b/LibreNMS/Util/Url.php @@ -73,7 +73,7 @@ class Url } $class = self::deviceLinkDisplayClass($device); - $graphs = self::getOverviewGraphsForDevice($device); + $graphs = Graph::getOverviewGraphsForDevice($device); $url = Url::deviceUrl($device, $vars); // beginning of overlib box contains large hostname followed by hardware & OS details @@ -421,26 +421,6 @@ class Url return ''; } - private static function getOverviewGraphsForDevice($device) - { - if ($device->snmp_disable) { - return Config::getOsSetting('ping', 'over'); - } - - if ($graphs = Config::getOsSetting($device->os, 'over')) { - return $graphs; - } - - if ($os_group = Config::getOsSetting($device->os, 'os_group')) { - $name = key($os_group); - if (isset($os_group[$name]['over'])) { - return $os_group[$name]['over']; - } - } - - return Config::getOsSetting('default', 'over'); - } - /** * @param Device $device * @return string @@ -551,7 +531,7 @@ class Url $vars = []; foreach ($parts as $part) { - list($key, $value) = explode('=', $part); + [$key, $value] = explode('=', $part); $vars[$key] = $value; } diff --git a/app/Http/Controllers/DeviceController.php b/app/Http/Controllers/DeviceController.php index 7b6d16293b..44bbec8348 100644 --- a/app/Http/Controllers/DeviceController.php +++ b/app/Http/Controllers/DeviceController.php @@ -12,6 +12,7 @@ use Gate; use Illuminate\Http\Request; use Illuminate\Support\Arr; use LibreNMS\Config; +use LibreNMS\Util\Graph; use LibreNMS\Util\Url; class DeviceController extends Controller @@ -130,7 +131,7 @@ class DeviceController extends Controller ]; $graphs = []; - foreach ($this->getDeviceGraphs($device) as $graph) { + foreach (Graph::getOverviewGraphsForDevice($device) as $graph) { $graph_array['type'] = $graph['graph']; $graph_array['popup_title'] = __($graph['text']); $graphs[] = $graph_array; @@ -139,18 +140,6 @@ class DeviceController extends Controller return $graphs; } - private function getDeviceGraphs(Device $device) - { - if ($device->snmp_disable) { - return Config::get('os.ping.over'); - } elseif (Config::has("os.$device->os.over")) { - return Config::get("os.$device->os.over"); - } - - $os_group = Config::getOsSetting($device->os, 'group'); - return Config::get("os.$os_group.over", Config::get('os.default.over')); - } - private function deviceLinkMenu(Device $device) { $device_links = []; diff --git a/app/Http/Controllers/OverviewController.php b/app/Http/Controllers/OverviewController.php index 7844f01181..9bb155f3a9 100644 --- a/app/Http/Controllers/OverviewController.php +++ b/app/Http/Controllers/OverviewController.php @@ -154,8 +154,7 @@ class OverviewController extends Controller ->get(); $devices_uptime = $devices_uptime->reject(function ($device) { - $device->loadOs(); // TODO: needed? - return Config::get("os.{$device->os}.bad_uptime") == true; + return Config::getOsSetting($device->os, 'bad_uptime') == true; }); } diff --git a/app/Http/Controllers/Select/DeviceFieldController.php b/app/Http/Controllers/Select/DeviceFieldController.php index 2fad9d2d17..d9962f6509 100644 --- a/app/Http/Controllers/Select/DeviceFieldController.php +++ b/app/Http/Controllers/Select/DeviceFieldController.php @@ -82,7 +82,6 @@ class DeviceFieldController extends SelectController $text = $device[$field]; if ($field == 'os') { - $device->loadOs(); $text = Config::getOsSetting($text, 'text'); } elseif ($field == 'type') { $text = ucfirst($text); diff --git a/app/Http/Controllers/Table/DeviceController.php b/app/Http/Controllers/Table/DeviceController.php index e870eee8b3..c1867a2ee4 100644 --- a/app/Http/Controllers/Table/DeviceController.php +++ b/app/Http/Controllers/Table/DeviceController.php @@ -202,7 +202,6 @@ class DeviceController extends TableController */ private function getOsText($device) { - $device->loadOs(); $os_text = Config::getOsSetting($device->os, 'text'); if ($this->isDetailed()) { diff --git a/app/Models/Device.php b/app/Models/Device.php index 49a919c3d8..fab10a333d 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -8,7 +8,6 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Query\JoinClause; use Illuminate\Support\Str; -use LibreNMS\DB\Eloquent; use LibreNMS\Exceptions\InvalidIpException; use LibreNMS\Util\IP; use LibreNMS\Util\IPv4; @@ -30,17 +29,6 @@ class Device extends BaseModel 'status' => 'boolean', ]; - /** - * Initialize this class - */ - public static function boot() - { - parent::boot(); - if (!app()->runningInConsole()) { - self::loadAllOs(true); - } - } - // ---- Helper Functions ---- public static function findByHostname($hostname) @@ -171,56 +159,6 @@ class Device extends BaseModel return $query->exists(); } - public function loadOs($force = false) - { - $yaml_file = base_path('/includes/definitions/' . $this->os . '.yaml'); - - if ((!\LibreNMS\Config::getOsSetting($this->os, 'definition_loaded') || $force) && file_exists($yaml_file)) { - $os = \Symfony\Component\Yaml\Yaml::parse(file_get_contents($yaml_file)); - - \LibreNMS\Config::set("os.$this->os", array_replace_recursive($os, \LibreNMS\Config::get("os.$this->os", []))); - \LibreNMS\Config::set("os.$this->os.definition_loaded", true); - } - } - - /** - * Load all OS, optionally load just the OS used by existing devices - * Default cache time is 1 day. Controlled by os_def_cache_time. - * - * @param bool $existing Only load OS that have existing OS in the database - * @param bool $cached Load os definitions from the cache file - */ - public static function loadAllOs($existing = false, $cached = true) - { - $install_dir = \LibreNMS\Config::get('install_dir'); - $cache_file = $install_dir . '/cache/os_defs.cache'; - if ($cached && is_file($cache_file) && (time() - filemtime($cache_file) < \LibreNMS\Config::get('os_def_cache_time'))) { - // Cached - $os_defs = unserialize(file_get_contents($cache_file)); - if ($existing) { - // remove unneeded os - $os_defs = array_diff_key($os_defs, self::distinct('os')->get('os')->toArray()); - } - \LibreNMS\Config::set('os', array_replace_recursive($os_defs, \LibreNMS\Config::get('os'))); - } else { - // load from yaml - if ($existing && Eloquent::isConnected()) { - $os_list = []; - foreach (self::distinct('os')->get('os')->toArray() as $os) { - $os_list[] = $install_dir . '/includes/definitions/' . $os['os'] . '.yaml'; - } - } else { - $os_list = glob($install_dir . '/includes/definitions/*.yaml'); - } - foreach ($os_list as $file) { - if (is_readable($file)) { - $tmp = \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file)); - \LibreNMS\Config::set("os.{$tmp['os']}", array_replace_recursive($tmp, \LibreNMS\Config::get("os.{$tmp['os']}", []))); - } - } - } - } - /** * Get the shortened display name of this device. * Length is always overridden by shorthost_target_length. @@ -408,7 +346,6 @@ class Device extends BaseModel public function getIconAttribute($icon) { - $this->loadOs(); return Str::start(Url::findOsImage($this->os, $this->features, $icon), 'images/os/'); } diff --git a/discovery.php b/discovery.php index 59d4f64959..6572ad3dcb 100755 --- a/discovery.php +++ b/discovery.php @@ -83,7 +83,7 @@ EOH; if (isset($options['v'])) { $vdebug = true; } - update_os_cache(true); // Force update of OS Cache + \LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache } if (!$where) { diff --git a/html/ajax_ossuggest.php b/html/ajax_ossuggest.php index 38800d8162..4542611392 100644 --- a/html/ajax_ossuggest.php +++ b/html/ajax_ossuggest.php @@ -54,7 +54,7 @@ function levsortos($base, $obj, $keys) header('Content-type: application/json'); if (isset($_GET['term'])) { - load_all_os(); + \LibreNMS\Util\OS::loadAllDefinitions(false, true); $_GET['term'] = clean($_GET['term']); $sortos = levsortos($_GET['term'], \LibreNMS\Config::get('os'), ["text", "os"]); $sortos = array_slice($sortos, 0, 20); diff --git a/includes/common.php b/includes/common.php index 49867a4938..b46d056e39 100644 --- a/includes/common.php +++ b/includes/common.php @@ -16,12 +16,12 @@ * the source code distribution for details. */ -use App\Models\Device; use LibreNMS\Config; use LibreNMS\Exceptions\InvalidIpException; use LibreNMS\Util\Git; use LibreNMS\Util\IP; use LibreNMS\Util\Laravel; +use LibreNMS\Util\OS; function generate_priority_label($priority) { @@ -1000,7 +1000,7 @@ function version_info($remote = false) curl_setopt($api, CURLOPT_CONNECTTIMEOUT, 5); $output['github'] = json_decode(curl_exec($api), true); } - list($local_sha, $local_date) = explode('|', rtrim(`git show --pretty='%H|%ct' -s HEAD`)); + [$local_sha, $local_date] = explode('|', rtrim(`git show --pretty='%H|%ct' -s HEAD`)); $output['local_sha'] = $local_sha; $output['local_date'] = $local_date; $output['local_branch'] = rtrim(`git rev-parse --abbrev-ref HEAD`); @@ -1217,7 +1217,7 @@ function ResolveGlues($tables, $target, $x = 0, $hist = array(), $last = array() if (sizeof($glues) == 1 && $glues[0]['COLUMN_NAME'] != $target) { //Search for new candidates to expand $ntables = array(); - list($tmp) = explode('_', $glues[0]['COLUMN_NAME'], 2); + [$tmp] = explode('_', $glues[0]['COLUMN_NAME'], 2); $ntables[] = $tmp; $ntables[] = $tmp.'s'; $tmp = dbFetchRows('SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_NAME LIKE "'.substr($table, 0, -1).'_%" && TABLE_NAME != "'.$table.'"'); @@ -1233,7 +1233,7 @@ function ResolveGlues($tables, $target, $x = 0, $hist = array(), $last = array() if ($glue['COLUMN_NAME'] == $target) { return array_merge($last, array($table.'.'.$target)); } else { - list($tmp) = explode('_', $glue['COLUMN_NAME']); + [$tmp] = explode('_', $glue['COLUMN_NAME']); $tmp .= 's'; if (!in_array($tmp, $tables) && !in_array($tmp, $hist)) { //Expand table @@ -1361,13 +1361,7 @@ function load_os(&$device) return; } - if (!Config::get("os.{$device['os']}.definition_loaded")) { - $tmp_os = Symfony\Component\Yaml\Yaml::parse( - file_get_contents(Config::get('install_dir') . '/includes/definitions/' . $device['os'] . '.yaml') - ); - - Config::set("os.{$device['os']}", array_replace_recursive($tmp_os, Config::get("os.{$device['os']}", []))); - } + \LibreNMS\Util\OS::loadDefinition($device['os']); // Set type to a predefined type for the OS if it's not already set $loaded_os_type = Config::get("os.{$device['os']}.type"); @@ -1383,50 +1377,6 @@ function load_os(&$device) } else { unset($device['os_group']); } - - Config::set("os.{$device['os']}.definition_loaded", true); -} - -/** - * Load all OS, optionally load just the OS used by existing devices - * Default cache time is 1 day. Controlled by os_def_cache_time. - * - * @param bool $existing Only load OS that have existing OS in the database - * @param bool $cached Load os definitions from the cache file - */ -function load_all_os($existing = false, $cached = true) -{ - Device::loadAllOs($existing, $cached); -} - -/** - * * Update the OS cache file cache/os_defs.cache - * @param bool $force - * @return bool true if the cache was updated - */ -function update_os_cache($force = false) -{ - $install_dir = Config::get('install_dir'); - $cache_file = "$install_dir/cache/os_defs.cache"; - $cache_keep_time = Config::get('os_def_cache_time', 86400) - 7200; // 2hr buffer - - if ($force === true || !is_file($cache_file) || time() - filemtime($cache_file) > $cache_keep_time) { - d_echo('Updating os_def.cache... '); - - // remove previously cached os settings and replace with user settings - $config = ['os' => []]; // local $config variable, not global - include "$install_dir/config.php"; - Config::set('os', $config['os']); - - // load the os defs fresh from cache (merges with existing OS settings) - load_all_os(false, false); - - file_put_contents($cache_file, serialize(Config::get('os'))); - d_echo("Done\n"); - return true; - } - - return false; } /** diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index 45aaa42799..63bbc42f57 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -960,21 +960,21 @@ function get_toner_capacity($raw_capacity) */ function ignore_storage($os, $descr) { - foreach (Config::getOsSetting($os, 'ignore_mount') as $im) { + foreach (Config::getCombined($os, 'ignore_mount', []) as $im) { if ($im == $descr) { d_echo("ignored $descr (matched: $im)\n"); return true; } } - foreach (Config::getOsSetting($os, 'ignore_mount_string') as $ims) { + foreach (Config::getCombined($os, 'ignore_mount_string', []) as $ims) { if (Str::contains($descr, $ims)) { d_echo("ignored $descr (matched: $ims)\n"); return true; } } - foreach (Config::getOsSetting($os, 'ignore_mount_regexp') as $imr) { + foreach (Config::getCombined($os, 'ignore_mount_regexp', []) as $imr) { if (preg_match($imr, $descr)) { d_echo("ignored $descr (matched: $imr)\n"); return true; diff --git a/includes/functions.php b/includes/functions.php index ff7de988ef..37aa470ca7 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -126,7 +126,7 @@ function parse_modules($type, $options) foreach (explode(',', $options['m']) as $module) { // parse submodules (only supported by some modules) if (Str::contains($module, '/')) { - list($module, $submodule) = explode('/', $module, 2); + [$module, $submodule] = explode('/', $module, 2); $existing_submodules = Config::get("{$type}_submodules.$module", []); $existing_submodules[] = $submodule; Config::set("{$type}_submodules.$module", $existing_submodules); @@ -1049,7 +1049,7 @@ function is_port_valid($port, $device) } // ifDescr should not be empty unless it is explicitly allowed - if (!Config::getOsSetting($device['os'], 'empty_ifdescr', false)) { + if (!Config::getOsSetting($device['os'], 'empty_ifdescr', Config::get('empty_ifdescr', false))) { d_echo("ignored: empty ifDescr\n"); return false; } @@ -1061,7 +1061,7 @@ function is_port_valid($port, $device) $ifType = $port['ifType']; $ifOperStatus = $port['ifOperStatus']; - if (str_i_contains($ifDescr, Config::getOsSetting($device['os'], 'good_if'))) { + if (str_i_contains($ifDescr, Config::getOsSetting($device['os'], 'good_if', Config::get('good_if')))) { return true; } @@ -1495,7 +1495,7 @@ function fping($host, $count = 3, $interval = 1000, $timeout = 500, $address_fam $output = $process->getErrorOutput(); preg_match('#= (\d+)/(\d+)/(\d+)%(, min/avg/max = ([\d.]+)/([\d.]+)/([\d.]+))?$#', $output, $parsed); - list(, $xmt, $rcv, $loss, , $min, $avg, $max) = array_pad($parsed, 8, 0); + [, $xmt, $rcv, $loss, , $min, $avg, $max] = array_pad($parsed, 8, 0); if ($loss < 0) { $xmt = 1; diff --git a/includes/html/dev-overview-data.inc.php b/includes/html/dev-overview-data.inc.php index 4feffcd769..66280b90fd 100644 --- a/includes/html/dev-overview-data.inc.php +++ b/includes/html/dev-overview-data.inc.php @@ -31,7 +31,7 @@ if ($device['features']) { $device['features'] = '('.$device['features'].')'; } -$device['os_text'] = Config::get("os.{$device['os']}.text"); +$device['os_text'] = Config::getOsSetting($device['os'], 'text'); echo '
System Name
diff --git a/includes/html/device-header.inc.php b/includes/html/device-header.inc.php deleted file mode 100644 index 725cb026a9..0000000000 --- a/includes/html/device-header.inc.php +++ /dev/null @@ -1,66 +0,0 @@ -'; - -$host_id = get_vm_parent_id($device); -if ($host_id > 0) { - echo ' -'; -} - -if (AlertUtil::isMaintenance($device['device_id'])) { - echo (''); -} - -echo ' -'.generate_device_link($device).'
-'.generate_link($device['location'], array('page' => 'devices', 'location' => $device['location'])) .' -
'; -//
'; - - -if ($device['snmp_disable']) { - $graphs = Config::get('os.ping.over'); -} elseif (Config::has("os.{$device['os']}.over")) { - $graphs = Config::get("os.{$device['os']}.over"); -} elseif (isset($device['os_group']) && Config::has("os.{$device['os_group']}.over")) { - $graphs = Config::get("os.{$device['os_group']}.over"); -} else { - $graphs = Config::get('os.default.over'); -} - -$graph_array = array(); -$graph_array['height'] = '100'; -$graph_array['width'] = '310'; -$graph_array['to'] = Config::get('time.now'); -$graph_array['device'] = $device['device_id']; -$graph_array['type'] = 'device_bits'; -$graph_array['from'] = Config::get('time.day'); -$graph_array['legend'] = 'no'; -$graph_array['popup_title'] = $descr; - -$graph_array['height'] = '45'; -$graph_array['width'] = '150'; -$graph_array['bg'] = 'FFFFFF00'; - -if (device_permitted($device['device_id']) || Config::get('allow_unauth_graphs')) { - echo '
'; - foreach ($graphs as $entry) { - if ($entry['graph']) { - $graph_array['type'] = $entry['graph']; - - echo "
"; - print_graph_popup($graph_array); - echo "
" . $entry['text'] . '
'; - echo '
'; - } - } - echo '
'; -} -echo '
'; - -unset($graph_array); diff --git a/includes/html/functions.inc.php b/includes/html/functions.inc.php index 132e18209b..ed3166bc1a 100644 --- a/includes/html/functions.inc.php +++ b/includes/html/functions.inc.php @@ -177,15 +177,7 @@ function generate_device_link($device, $text = null, $vars = array(), $start = 0 $text = format_hostname($device, $text); - if ($device['snmp_disable']) { - $graphs = Config::get('os.ping.over'); - } elseif (Config::has("os.{$device['os']}.over")) { - $graphs = Config::get("os.{$device['os']}.over"); - } elseif (isset($device['os_group']) && Config::has("os.{$device['os_group']}.over")) { - $graphs = Config::get("os.{$device['os_group']}.over"); - } else { - $graphs = Config::get('os.default.over'); - } + $graphs = \LibreNMS\Util\Graph::getOverviewGraphsForDevice(DeviceCache::get($device['device_id'])); $url = generate_device_url($device, $vars); @@ -196,7 +188,7 @@ function generate_device_link($device, $text = null, $vars = array(), $start = 0 } if ($device['os']) { - $contents .= ' - ' . Config::get("os.{$device['os']}.text"); + $contents .= ' - ' . Config::getOsSetting($device['os'], 'text'); } if ($device['version']) { @@ -1126,7 +1118,7 @@ function search_oxidized_config($search_in_conf_textbox) ) ); $context = stream_context_create($opts); - + $nodes = json_decode(file_get_contents($oxidized_search_url, false, $context), true); // Look up Oxidized node names to LibreNMS devices for a link foreach ($nodes as &$n) { @@ -1205,7 +1197,7 @@ function get_oxidized_nodes_list() //user cannot see this device, so let's skip it. continue; } - + echo " " . $device['device_id'] . " " . $object['name'] . " diff --git a/includes/html/graphs/device/processor.inc.php b/includes/html/graphs/device/processor.inc.php index d3e18cd73f..6bf6dec19c 100644 --- a/includes/html/graphs/device/processor.inc.php +++ b/includes/html/graphs/device/processor.inc.php @@ -2,7 +2,7 @@ $procs = dbFetchRows('SELECT * FROM `processors` where `device_id` = ?', array($device['device_id'])); -if (\LibreNMS\Config::get("os.{$device['os']}.processor_stacked")) { +if (\LibreNMS\Config::getOsSetting($device['os'], 'processor_stacked')) { include 'includes/html/graphs/device/processor_stack.inc.php'; } else { include 'includes/html/graphs/device/processor_separate.inc.php'; diff --git a/includes/html/pages/devices.inc.php b/includes/html/pages/devices.inc.php index 112baf9b47..452b9b6c05 100644 --- a/includes/html/pages/devices.inc.php +++ b/includes/html/pages/devices.inc.php @@ -272,8 +272,6 @@ if ($format == "graph") { $os_selected = '""'; if (isset($vars['os'])) { - $device = ['os' => $vars['os']]; - load_os($device); $os_selected = json_encode(['id' => $vars['os'], 'text' => \LibreNMS\Config::getOsSetting($vars['os'], 'text', $vars['os'])]); } diff --git a/includes/init.php b/includes/init.php index d107e5a242..68dca3fa5d 100644 --- a/includes/init.php +++ b/includes/init.php @@ -134,11 +134,11 @@ try { exit(); } -if (module_selected('discovery', $init_modules) && !update_os_cache()) { - // load_all_os() is called by update_os_cache() if updated, no need to call twice - load_all_os(); +if (module_selected('discovery', $init_modules) && !\LibreNMS\Util\OS::updateCache(false)) { + // OS::loadAllDefinitions() is called by update_os_cache() if updated, no need to call twice + \LibreNMS\Util\OS::loadAllDefinitions(false, true); } elseif (module_selected('web', $init_modules)) { - load_all_os(!module_selected('nodb', $init_modules)); + \LibreNMS\Util\OS::loadAllDefinitions(!module_selected('nodb', $init_modules), true); } if (module_selected('web', $init_modules)) { diff --git a/includes/snmp.inc.php b/includes/snmp.inc.php index e4f469a8ea..4cd1ae6b5e 100644 --- a/includes/snmp.inc.php +++ b/includes/snmp.inc.php @@ -1387,7 +1387,7 @@ function snmpwalk_array_num($device, $oid, $indexes = 1) function get_device_max_repeaters($device) { return $device['attribs']['snmp_max_repeaters'] ?? - Config::getOsSetting($device['os'], 'snmp.max_repeaters', false); + Config::getOsSetting($device['os'], 'snmp.max_repeaters', Config::get('snmp.max_repeaters', false)); } /** diff --git a/poller.php b/poller.php index c979dfd04c..c158987866 100755 --- a/poller.php +++ b/poller.php @@ -115,7 +115,7 @@ EOH; if (isset($options['v'])) { $vdebug = true; } - update_os_cache(true); // Force update of OS Cache + \LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache } // If we've specified modules with -m, use them diff --git a/scripts/collect-snmp-data.php b/scripts/collect-snmp-data.php index 0d218ea790..558ab294be 100755 --- a/scripts/collect-snmp-data.php +++ b/scripts/collect-snmp-data.php @@ -124,7 +124,7 @@ try { echo "Capturing Data: "; - update_os_cache(true); // Force update of OS Cache + \LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache $capture->captureFromDevice($device['device_id'], true, $prefer_new_snmprec); } catch (InvalidModuleException $e) { echo $e->getMessage() . PHP_EOL; diff --git a/scripts/save-test-data.php b/scripts/save-test-data.php index 06cd9aa7f6..c4ddad9e52 100755 --- a/scripts/save-test-data.php +++ b/scripts/save-test-data.php @@ -128,7 +128,7 @@ try { } echo PHP_EOL; - update_os_cache(true); // Force update of OS Cache + \LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache $tester = new ModuleTestHelper($modules, $target_os, $target_variant); if (!$no_save && !empty($output_file)) { $tester->setJsonSavePath($output_file); diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 1f8873256d..7bc487495f 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -100,7 +100,7 @@ class ConfigTest extends TestCase $this->assertNull(Config::getOsSetting('nullos', 'unset'), 'Non-existing settings should return null'); $this->assertFalse(Config::getOsSetting('nullos', 'unset', false), 'Non-existing settings should return $default'); $this->assertTrue(Config::getOsSetting('nullos', 'fancy'), 'Failed to get setting'); - $this->assertTrue(Config::getOsSetting('nullos', 'fallback'), 'Failed to fallback to global setting'); + $this->assertNull(Config::getOsSetting('nullos', 'fallback'), 'Incorrectly loaded global setting'); } public function testGetCombined() diff --git a/tests/OSDiscoveryTest.php b/tests/OSDiscoveryTest.php index b71639fb7e..18502d9673 100644 --- a/tests/OSDiscoveryTest.php +++ b/tests/OSDiscoveryTest.php @@ -27,6 +27,9 @@ namespace LibreNMS\Tests; use Illuminate\Support\Str; use LibreNMS\Config; +use LibreNMS\Util\OS; +use const false; +use const true; class OSDiscoveryTest extends TestCase { @@ -145,7 +148,7 @@ class OSDiscoveryTest extends TestCase // make sure all OS are loaded $config_os = array_keys(Config::get('os')); if (count($config_os) < count(glob(Config::get('install_dir').'/includes/definitions/*.yaml'))) { - load_all_os(); + OS::loadAllDefinitions(false, true); $config_os = array_keys(Config::get('os')); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 5ab62c9e22..9c42f8c259 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -42,8 +42,6 @@ chdir($install_dir); ini_set('display_errors', 1); //error_reporting(E_ALL & ~E_WARNING); -update_os_cache(true); // Force update of OS Cache - $snmpsim = new Snmpsim('127.1.6.2', 1162, null); if (getenv('SNMPSIM')) { $snmpsim->fork(); @@ -77,6 +75,5 @@ if (getenv('DBTEST')) { unset($db_config); } -// reload the config including database config -Config::reload(); -load_all_os(); +Config::reload(); // reload the config including database config +\LibreNMS\Util\OS::updateCache(true); // Force update of OS Cache