diff --git a/LibreNMS/Config.php b/LibreNMS/Config.php index fb4c67bd92..d48608464a 100644 --- a/LibreNMS/Config.php +++ b/LibreNMS/Config.php @@ -157,10 +157,31 @@ class Config * * @param mixed $key period separated config variable name * @param mixed $value + * @param bool $persist set the setting in the database so it persists across runs + * @param string $default default (only set when initially created) + * @param string $descr webui description (only set when initially created) + * @param string $group webui group (only set when initially created) + * @param string $sub_group webui subgroup (only set when initially created) */ - public static function set($key, $value) + public static function set($key, $value, $persist = false, $default ='', $descr='', $group='', $sub_group='') { global $config; + + if ($persist) { + $res = dbUpdate(array('config_value' => $value), 'config', '`config_name`=?', array($key)); + if (!$res && !dbFetchCell('SELECT 1 FROM `config` WHERE `config_name`=?', array($key))) { + $insert = array( + 'config_name' => $key, + 'config_value' => $value, + 'config_default' => $default, + 'config_descr' => $descr, + 'config_group' => $group, + 'config_sub_group' => $sub_group, + ); + dbInsert($insert, 'config'); + } + } + $keys = explode('.', $key); $curr = &$config; diff --git a/includes/defaults.inc.php b/includes/defaults.inc.php index c15d76145f..3644426e8a 100644 --- a/includes/defaults.inc.php +++ b/includes/defaults.inc.php @@ -42,8 +42,8 @@ $config['db_socket'] = null; $config['own_hostname'] = 'localhost'; // Location of executables -$config['fping'] = 'fping'; -$config['fping6'] = 'fping6'; +//$config['fping'] = '/usr/sbin/fping'; +//$config['fping6'] = '/usr/sbin/fping6'; $config['fping_options']['retries'] = 3; $config['fping_options']['timeout'] = 500; $config['fping_options']['count'] = 3; diff --git a/includes/functions.php b/includes/functions.php index 064836e98f..1e2124dc3a 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -2318,3 +2318,23 @@ function return_num($entry) return $num_response[0]; } } + +/** + * Locate the actual path of a binary + * + * @param $binary + * @return mixed + */ +function locate_binary($binary) +{ + if (!str_contains($binary, '/')) { + $output = `whereis -b $binary`; + $target = trim(substr($output, strpos($output, ':') + 1)); + + if (file_exists($target)) { + return $target; + } + } + + return $binary; +} diff --git a/includes/process_config.inc.php b/includes/process_config.inc.php index 64b7176fa3..a8122f055f 100644 --- a/includes/process_config.inc.php +++ b/includes/process_config.inc.php @@ -23,6 +23,8 @@ * @author Neil Lathwood */ +use LibreNMS\Config; + if (empty($config['email_from'])) { $config['email_from'] = '"' . $config['project_name'] . '" <' . $config['email_user'] . '@' . php_uname('n') . '>'; } @@ -42,3 +44,10 @@ if ($config['secure_cookies']) { if ($config['rrdgraph_real_95th']) { $config['rrdgraph_real_percentile'] = $config['rrdgraph_real_95th']; } + +// make sure we have full path to binaries in case PATH isn't set +foreach (array('fping', 'fping6') as $bin) { + if (!is_executable(Config::get($bin))) { + Config::set($bin, locate_binary($bin), true, $bin, "Path to $bin", 'external', 'paths'); + } +} diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 23f2729cc4..5adf510c68 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -119,6 +119,29 @@ class ConfigTest extends \PHPUnit_Framework_TestCase $this->assertEquals("I'll be there", $config['you']['and']['me']); } + public function testSetPersist() + { + if (getenv('DBTEST')) { + dbConnect(); + dbBeginTransaction(); + } else { + $this->markTestSkipped('Database tests not enabled. Set DBTEST=1 to enable.'); + } + + $key = 'testing.persist'; + + dbDelete('config', '`config_name`=?', array($key)); // FIXME dbInsert breaks transactions + $this->assertNull(dbFetchCell('SELECT `config_value` FROM `config` WHERE `config_name`=?', array($key)), "$key should not be set, clean database"); + Config::set($key, 'one', true); + $this->assertEquals('one', dbFetchCell('SELECT `config_value` FROM `config` WHERE `config_name`=?', array($key))); + Config::set($key, 'two', true); + $this->assertEquals('two', dbFetchCell('SELECT `config_value` FROM `config` WHERE `config_name`=?', array($key))); + + if (getenv('DBTEST')) { + dbRollbackTransaction(); + } + } + public function testHas() { Config::set('long.key.setting', 'no one cares');