Use Config helper (#10339)

remove usage of global variable
This commit is contained in:
Tony Murray
2019-06-23 00:29:12 -05:00
committed by GitHub
parent 342acf50f1
commit f3ba8947f7
367 changed files with 1589 additions and 1857 deletions

View File

@@ -200,10 +200,8 @@ function get_sensor_rrd($device, $sensor)
function get_sensor_rrd_name($device, $sensor)
{
global $config;
# For IPMI, sensors tend to change order, and there is no index, so we prefer to use the description as key here.
if ($config['os'][$device['os']]['sensor_descr'] || $sensor['poller_type'] == "ipmi") {
if (Config::getOsSetting($device['os'], 'sensor_descr') || $sensor['poller_type'] == "ipmi") {
return array('sensor', $sensor['sensor_class'], $sensor['sensor_type'], $sensor['sensor_descr']);
} else {
return array('sensor', $sensor['sensor_class'], $sensor['sensor_type'], $sensor['sensor_index']);
@@ -758,13 +756,11 @@ function is_client_authorized($clientip)
*/
function get_graph_subtypes($type, $device = null)
{
global $config;
$type = basename($type);
$types = array();
// find the subtypes defined in files
if ($handle = opendir($config['install_dir'] . "/includes/html/graphs/$type/")) {
if ($handle = opendir(Config::get('install_dir') . "/includes/html/graphs/$type/")) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "auth.inc.php" && strstr($file, ".inc.php")) {
$types[] = str_replace(".inc.php", "", $file);
@@ -777,8 +773,8 @@ function get_graph_subtypes($type, $device = null)
// find the MIB subtypes
$graphs = get_device_graphs($device);
foreach ($config['graph_types'] as $type => $unused1) {
foreach ($config['graph_types'][$type] as $subtype => $unused2) {
foreach (Config::get('graph_types') as $type => $unused1) {
foreach (Config::get("graph_types.$type") as $subtype => $unused2) {
if (is_mib_graph($type, $subtype) && in_array($subtype, $graphs)) {
$types[] = $subtype;
}
@@ -798,9 +794,8 @@ function get_device_graphs($device)
function get_smokeping_files($device)
{
global $config;
$smokeping_files = array();
if (isset($config['smokeping']['dir'])) {
if (Config::has('smokeping.dir')) {
$smokeping_dir = generate_smokeping_file($device);
if ($handle = opendir($smokeping_dir)) {
while (false !== ($file = readdir($handle))) {
@@ -814,8 +809,8 @@ function get_smokeping_files($device)
} else {
$target = str_replace('.rrd', '', $file);
$target = str_replace('_', '.', $target);
$smokeping_files['in'][$target][$config['own_hostname']] = $file;
$smokeping_files['out'][$config['own_hostname']][$target] = $file;
$smokeping_files['in'][$target][Config::get('own_hostname')] = $file;
$smokeping_files['out'][Config::get('own_hostname')][$target] = $file;
}
}
}
@@ -828,11 +823,10 @@ function get_smokeping_files($device)
function generate_smokeping_file($device, $file = '')
{
global $config;
if ($config['smokeping']['integration'] === true) {
return $config['smokeping']['dir'] .'/'. $device['type'] .'/' . $file;
if (Config::get('smokeping.integration') === true) {
return Config::get('smokeping.dir') . '/' . $device['type'] . '/' . $file;
} else {
return $config['smokeping']['dir'] . '/' . $file;
return Config::get('smokeping.dir') . '/' . $file;
}
} // generate_smokeping_file
@@ -861,7 +855,7 @@ function is_mib_poller_enabled($device)
{
$val = get_dev_attrib($device, 'poll_mib');
if ($val == null) {
return is_module_enabled('poller', 'mib');
return Config::get("poller_modules.mib", false);
}
return $val;
} // is_mib_poller_enabled
@@ -1032,20 +1026,6 @@ function can_ping_device($attribs)
} // end can_ping_device
/*
* @return true if the requested module type & name is globally enabled
*/
function is_module_enabled($type, $module)
{
global $config;
if (isset($config[$type.'_modules'][$module])) {
return $config[$type.'_modules'][$module] == 1;
} else {
return false;
}
} // is_module_enabled
/*
* @return true if every string in $arr begins with $str
*/
@@ -1089,7 +1069,7 @@ function print_mib_poller_disabled()
{
echo '<h4>MIB polling is not enabled</h4>
<p>
Set <code>$config[\'poller_modules\'][\'mib\'] = 1;</code> in <code>config.php</code> or enable for this device specifically to enable.
Set \'poller_modules.mib\' in your config or enable for this device specifically to enable.
</p>';
} // print_mib_poller_disabled
@@ -1135,17 +1115,16 @@ function parse_location($location)
*/
function version_info($remote = false)
{
global $config;
$version = \LibreNMS\Util\Version::get();
$output = [
'local_ver' => $version->local(),
];
if (Git::repoPresent() && Git::binaryExists()) {
if ($remote === true && $config['update_channel'] == 'master') {
if ($remote === true && Config::get('update_channel') == 'master') {
$api = curl_init();
set_curl_proxy($api);
curl_setopt($api, CURLOPT_USERAGENT, 'LibreNMS');
curl_setopt($api, CURLOPT_URL, $config['github_api'].'commits/master');
curl_setopt($api, CURLOPT_URL, Config::get('github_api') . 'commits/master');
curl_setopt($api, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($api, CURLOPT_TIMEOUT, 5);
curl_setopt($api, CURLOPT_TIMEOUT_MS, 5000);
@@ -1161,10 +1140,10 @@ function version_info($remote = false)
$output['php_ver'] = phpversion();
$output['mysql_ver'] = dbIsConnected() ? dbFetchCell('SELECT version()') : '?';
$output['rrdtool_ver'] = str_replace('1.7.01.7.0', '1.7.0', implode(' ', array_slice(explode(' ', shell_exec(
($config['rrdtool'] ?: 'rrdtool') . ' --version |head -n1'
Config::get('rrdtool', 'rrdtool') . ' --version |head -n1'
)), 1, 1)));
$output['netsnmp_ver'] = str_replace('version: ', '', rtrim(shell_exec(
($config['snmpget'] ?: 'snmpget').' --version 2>&1'
Config::get('snmpget', 'snmpget') . ' --version 2>&1'
)));
return $output;
@@ -1534,40 +1513,35 @@ function display($value, $purifier_config = [])
*/
function load_os(&$device)
{
global $config;
if (!isset($device['os'])) {
d_echo("No OS to load\n");
return;
}
if (!isset($config['os'][$device['os']]['definition_loaded'])) {
if (!Config::get("os.{$device['os']}.definition_loaded")) {
$tmp_os = Symfony\Component\Yaml\Yaml::parse(
file_get_contents($config['install_dir'] . '/includes/definitions/' . $device['os'] . '.yaml')
file_get_contents(Config::get('install_dir') . '/includes/definitions/' . $device['os'] . '.yaml')
);
if (isset($config['os'][$device['os']])) {
$config['os'][$device['os']] = array_replace_recursive($tmp_os, $config['os'][$device['os']]);
} else {
$config['os'][$device['os']] = $tmp_os;
}
Config::set("os.{$device['os']}", array_replace_recursive($tmp_os, Config::get("os.{$device['os']}", [])));
}
// Set type to a predefined type for the OS if it's not already set
if ((!isset($device['attribs']['override_device_type']) && $device['attribs']['override_device_type'] != 1) && $config['os'][$device['os']]['type'] != $device['type']) {
log_event('Device type changed ' . $device['type'] . ' => ' . $config['os'][$device['os']]['type'], $device, 'system', 3);
$device['type'] = $config['os'][$device['os']]['type'];
dbUpdate(array('type' => $device['type']), 'devices', 'device_id=?', array($device['device_id']));
d_echo("Device type changed to " . $device['type'] . "!\n");
$loaded_os_type = Config::get("os.{$device['os']}.type");
if ((!isset($device['attribs']['override_device_type']) && $device['attribs']['override_device_type'] != 1) && $loaded_os_type != $device['type']) {
log_event('Device type changed ' . $device['type'] . ' => ' . $loaded_os_type, $device, 'system', 3);
$device['type'] = $loaded_os_type;
dbUpdate(['type' => $loaded_os_type], 'devices', 'device_id=?', [$device['device_id']]);
d_echo("Device type changed to $loaded_os_type!\n");
}
if ($config['os'][$device['os']]['group']) {
$device['os_group'] = $config['os'][$device['os']]['group'];
if ($os_group = Config::get("os.{$device['os']}.group")) {
$device['os_group'] = $os_group;
} else {
unset($device['os_group']);
}
$config['os'][$device['os']]['definition_loaded'] = true;
Config::set("os.{$device['os']}.definition_loaded", true);
}
/**
@@ -1579,10 +1553,10 @@ function load_os(&$device)
*/
function load_all_os($existing = false, $cached = true)
{
global $config;
$cache_file = $config['install_dir'] . '/cache/os_defs.cache';
$install_dir = Config::get('install_dir');
$cache_file = $install_dir . '/cache/os_defs.cache';
if ($cached && is_file($cache_file) && (time() - filemtime($cache_file) < $config['os_def_cache_time'])) {
if ($cached && is_file($cache_file) && (time() - filemtime($cache_file) < Config::get('os_def_cache_time'))) {
// Cached
$os_defs = unserialize(file_get_contents($cache_file));
@@ -1591,26 +1565,22 @@ function load_all_os($existing = false, $cached = true)
$os_defs = array_diff_key($os_defs, dbFetchColumn('SELECT DISTINCT(`os`) FROM `devices`'));
}
$config['os'] = array_replace_recursive($os_defs, $config['os']);
Config::set('os', array_replace_recursive($os_defs, Config::get('os')));
} else {
// load from yaml
if ($existing) {
$os_list = array_map(function ($os) use ($config) {
return $config['install_dir'] . '/includes/definitions/'. $os . '.yaml';
$os_list = array_map(function ($os) use ($install_dir) {
return $install_dir . '/includes/definitions/' . $os . '.yaml';
}, dbFetchColumn('SELECT DISTINCT(`os`) FROM `devices`'));
} else {
$os_list = glob($config['install_dir'].'/includes/definitions/*.yaml');
$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));
if (isset($config['os'][$tmp['os']])) {
$config['os'][$tmp['os']] = array_replace_recursive($tmp, $config['os'][$tmp['os']]);
} else {
$config['os'][$tmp['os']] = $tmp;
}
Config::set("os.{$tmp['os']}", array_replace_recursive($tmp, Config::get("os.{$tmp['os']}", [])));
}
}
}
@@ -1631,7 +1601,7 @@ function update_os_cache($force = false)
d_echo('Updating os_def.cache... ');
// remove previously cached os settings and replace with user settings
$config = array('os' => array()); // local $config variable, not global
$config = ['os' => []]; // local $config variable, not global
include "$install_dir/config.php";
Config::set('os', $config['os']);
@@ -1709,13 +1679,11 @@ function set_numeric($value, $default = 0)
function get_vm_parent_id($device)
{
global $config;
if (empty($device['hostname'])) {
return false;
}
return dbFetchCell("SELECT `device_id` FROM `vminfo` WHERE `vmwVmDisplayName` = ? OR `vmwVmDisplayName` = ?", array($device['hostname'],$device['hostname'].'.'.$config['mydomain']));
return dbFetchCell("SELECT `device_id` FROM `vminfo` WHERE `vmwVmDisplayName` = ? OR `vmwVmDisplayName` = ?", [$device['hostname'], $device['hostname'] . '.' . Config::get('mydomain')]);
}
/**