mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Fix various issues with loading os definitions (#11640)
* Ping only device doesn't display if os was set to something, ping os wasn't loaded and we try to get overview graphs from it. * Fix snmp_disable device page load error When other os is set. * Revamp os setting loading the only safe way to access is Config::getOsSetting() * Remove getOsSetting fallback behavior Most instances don't use it and it can have unexpected results Config::getOsSetting('blah', 'group') == 'librenms' * refactor and remove unneeded load_os/loadOs calls now since getOsSetting automatically loads it. * restore unix overview graphs, they are different small cleanups * fix
This commit is contained in:
@@ -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'));
|
||||
}
|
||||
}
|
||||
|
115
LibreNMS/Util/OS.php
Normal file
115
LibreNMS/Util/OS.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
/**
|
||||
* OS.php
|
||||
*
|
||||
* OS related functions (may belong in LibreNMS/OS, but here for now)
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @package LibreNMS
|
||||
* @link http://librenms.org
|
||||
* @copyright 2020 Tony Murray
|
||||
* @author Tony Murray <murraytony@gmail.com>
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@@ -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 '<img class="' . $class . '" width="' . $width . '" height="' . $height . '" src="' . url('graph.php') . '?' . implode($sep, $vars) . '">';
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user