From 85d0e3b98e88024400fed229ed3d69eb151a0bb4 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Tue, 3 Apr 2018 09:53:25 -0500 Subject: [PATCH] refactor: Move nested defaults to Config::processConfig() (#8429) * Move nested defaults to Config::processConfig() Fixes log_file when users set log_dir for example. Removed stuff: mibdir dp_autocreate base_url: setting http/https before config.php is loaded... moved collectd setting to it's own config file * add base_url mod back. I thought that since definitions is called before config.php it was useless, but it changes the built in definition between http and https. --- LibreNMS/Config.php | 60 +++++++++++++++++++++++++------ html/includes/collectd/config.php | 5 +++ includes/definitions.inc.php | 41 --------------------- 3 files changed, 55 insertions(+), 51 deletions(-) diff --git a/LibreNMS/Config.php b/LibreNMS/Config.php index 0947ee2037..e957363d85 100644 --- a/LibreNMS/Config.php +++ b/LibreNMS/Config.php @@ -368,18 +368,25 @@ class Config ini_set('session.cookie_secure', 1); } + // If we're on SSL, let's properly detect it + if (isset($_SERVER['HTTPS'])) { + self::set('base_url', preg_replace('/^http:/', 'https:', self::get('base_url'))); + } + + // Define some variables if they aren't set by user definition in config.php + self::setDefault('html_dir', '%s/html', ['install_dir']); + self::setDefault('rrd_dir', '%s/rrd', ['install_dir']); + self::setDefault('mib_dir', '%s/mibs', ['install_dir']); + self::setDefault('log_dir', '%s/logs', ['install_dir']); + self::setDefault('log_file', '%s/%s.log', ['log_dir', 'project_id']); + self::setDefault('plugin_dir', '%s/plugins', ['html_dir']); +// self::setDefault('email_from', '"%s" <%s@' . php_uname('n') . '>', ['project_name', 'email_user']); // FIXME email_from set because alerting config + // deprecated variables - if (self::has('rrdgraph_real_95th')) { - self::set('rrdgraph_real_percentile', self::get('rrdgraph_real_95th')); - } + self::deprecatedVariable('rrdgraph_real_95th', 'rrdgraph_real_percentile'); + self::deprecatedVariable('fping_options.millisec', 'fping_options.interval'); + self::deprecatedVariable('discovery_modules.cisco-vrf', 'discovery_modules.vrf'); - if (self::has('fping_options.millisec')) { - self::set('fping_options.interval', self::get('fping_options.millisec')); - } - - if (self::has('discovery_modules.cisco-vrf')) { - self::set('discovery_modules.vrf', self::get('discovery_modules.cisco-vrf')); - } // make sure we have full path to binaries in case PATH isn't set foreach (array('fping', 'fping6', 'snmpgetnext', 'rrdtool') as $bin) { @@ -388,4 +395,37 @@ class Config } } } + + /** + * Set default values for defaults that depend on other settings, if they are not already loaded + * + * @param string $key + * @param string $value value to set to key or vsprintf() format string for values below + * @param array $format_values array of keys to send to vsprintf() + */ + private static function setDefault($key, $value, $format_values = []) + { + if (!self::has($key)) { + if (is_string($value)) { + $format_values = array_map('self::get', $format_values); + self::set($key, vsprintf($value, $format_values)); + } else { + self::set($key, $value); + } + } + } + + /** + * Copy data from old variables to new ones. + * + * @param $old + * @param $new + */ + private static function deprecatedVariable($old, $new) + { + if (self::has($old)) { + d_echo("Copied deprecated config $old to $new\n"); + self::set($new, self::get($old)); + } + } } diff --git a/html/includes/collectd/config.php b/html/includes/collectd/config.php index 568ba97d83..c81a68c235 100644 --- a/html/includes/collectd/config.php +++ b/html/includes/collectd/config.php @@ -3,6 +3,11 @@ * Configuration file for Collectd graph browser */ +if (isset($config['rrdgraph_def_text'])) { + $config['rrdgraph_def_text'] = str_replace(' ', ' ', $config['rrdgraph_def_text']); + $config['rrd_opts_array'] = explode(' ', trim($config['rrdgraph_def_text'])); +} + // Array of paths when collectd's rrdtool plugin writes RRDs $config['datadirs'] = array($config['collectd_dir']); // Width of graph to be generated by rrdgraph diff --git a/includes/definitions.inc.php b/includes/definitions.inc.php index 0ea4262cbb..beb588bb1d 100644 --- a/includes/definitions.inc.php +++ b/includes/definitions.inc.php @@ -614,26 +614,6 @@ $config['device_types'][$i]['icon'] = 'workstation.png'; // $config['project_name_version'] = $config['project_name']; -if (isset($config['rrdgraph_def_text'])) { - $config['rrdgraph_def_text'] = str_replace(' ', ' ', $config['rrdgraph_def_text']); - $config['rrd_opts_array'] = explode(' ', trim($config['rrdgraph_def_text'])); -} - -if (isset($config['cdp_autocreate'])) { - $config['dp_autocreate'] = $config['cdp_autocreate']; -} - -if (!isset($config['mibdir'])) { - $config['mibdir'] = $config['install_dir'].'/mibs'; -} - -$config['mib_dir'] = $config['mibdir']; - -// If we're on SSL, let's properly detect it -if (isset($_SERVER['HTTPS'])) { - $config['base_url'] = preg_replace('/^http:/', 'https:', $config['base_url']); -} - // Set some times needed by loads of scripts (it's dynamic, so we do it here!) $config['time']['now'] = time(); $config['time']['now'] -= ($config['time']['now'] % 300); @@ -673,24 +653,3 @@ $config['ipmi_unit']['Watts'] = 'power'; $config['ipmi_unit']['Amps'] = 'current'; $config['ipmi_unit']['percent'] = 'load'; $config['ipmi_unit']['discrete'] = ''; - -// Define some variables if they aren't set by user definition in config.php -if (!isset($config['html_dir'])) { - $config['html_dir'] = $config['install_dir'].'/html'; -} - -if (!isset($config['rrd_dir'])) { - $config['rrd_dir'] = $config['install_dir'].'/rrd'; -} - -if (!isset($config['log_dir'])) { - $config['log_dir'] = $config['install_dir'].'/logs'; -} - -if (!isset($config['log_file'])) { - $config['log_file'] = $config['log_dir'].'/'.$config['project_id'].'.log'; -} - -if (!isset($config['plugin_dir'])) { - $config['plugin_dir'] = $config['html_dir'].'/plugins'; -}