diff --git a/LibreNMS/Config.php b/LibreNMS/Config.php index e6a8d7007f..ebe4d24095 100644 --- a/LibreNMS/Config.php +++ b/LibreNMS/Config.php @@ -81,20 +81,42 @@ class Config /** * Get a setting from the $config['os'] array using the os of the given device - * The sames as Config::get("os.{$device['os']}.$key") + * If that is not set, fallback to the same global config key * - * @param array $device Device array + * @param string $os The os name * @param string $key period separated config variable name * @param mixed $default optional value to return if the setting is not set * @return mixed */ - public static function getOsSetting($device, $key, $default = null) + public static function getOsSetting($os, $key, $default = null) { - if (!isset($device['os'])) { - return $default; + if ($os) { + $os_key = "os.$os.$key"; + + if (self::has($os_key)) { + return self::get($os_key); + } } - return self::get("os.{$device['os']}.$key", $default); + return self::get($key, $default); + } + + /** + * Get the merged array from the global and os settings for the specified key. + * Removes any duplicates. + * When the arrays have keys, os settings take precedence over global settings + * + * @param string $os The os name + * @param string $key period separated config variable name + * @param array $default optional array to return if the setting is not set + * @return array + */ + public static function getCombined($os, $key, $default = array()) + { + return array_unique(array_merge( + (array)self::get($key, $default), + (array)self::getOsSetting($os, $key, $default) + )); } /** diff --git a/doc/Developing/Support-New-OS.md b/doc/Developing/Support-New-OS.md index cc22aff24f..8c97b245cc 100644 --- a/doc/Developing/Support-New-OS.md +++ b/doc/Developing/Support-New-OS.md @@ -10,6 +10,7 @@ During all of these examples we will be using the OS of `pulse` as the example O > - [Adding Wireless Sensor information.](os/Wireless-Sensors.md) > - [Adding custom graphs.](os/Custom-Graphs.md) > - [Adding Unit tests (required).](os/Test-Units.md) +> - [Optional Settings](os/Settings.md) We currently have a script in pre-beta stages that can help speed up the process of deploying a new OS. It has support for add sensors in a basic form (except state sensors). @@ -20,4 +21,4 @@ It will be of the type network and belongs to the vendor, Cisco: `./scripts/new-os.php -h 101 -o test-os -t network -v cisco` The process will then step you through the process with some more questions. Please be warned, this is -currently pre-beta and may cause some issues. Please let us know of any on IRC. \ No newline at end of file +currently pre-beta and may cause some issues. Please let us know of any on IRC. diff --git a/doc/Developing/os/Settings.md b/doc/Developing/os/Settings.md new file mode 100644 index 0000000000..7ae5194626 --- /dev/null +++ b/doc/Developing/os/Settings.md @@ -0,0 +1,25 @@ +source: os/Settings.md +# Optional OS Settings + +This page documents settings that can be set in the os yaml files or in config.php. +All settings listed here are optional. If they are not set, the global default will be used. + +Users can override these settings in their config.php. + +For example, to set an alternate icon for ios: +```php +$config['os']['ios']['icon'] = 'fuzzybunny'; +``` + + +### Storage Settings +See also: [Global Storage Config](../../Support/Configuration.md#storage-configuration) + +```yaml +ignore_mount array: # exact match + - /var/run +ignore_mount_string: # substring + - run +ignore_mount_regexp: # regex + - "/^\/var/" +``` diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index f3a868e31c..fdfd915470 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -995,28 +995,31 @@ function get_toner_capacity($raw_capacity) } /** - * @param string $descr - * @return bool + * Should we ignore this storage device based on teh description? (usually the mount path or drive) + * + * @param string $os The OS of the device + * @param string $descr The description of the storage + * @return boolean */ -function ignore_storage($descr) +function ignore_storage($os, $descr) { - foreach (Config::get('ignore_mount', array()) as $bi) { - if ($bi == $descr) { - d_echo("$bi == $descr \n"); + foreach (Config::getOsSetting($os, 'ignore_mount') as $im) { + if ($im == $descr) { + d_echo("ignored $descr (matched: $im)\n"); return true; } } - foreach (Config::get('ignore_mount_string', array()) as $bi) { - if (str_contains($descr, $bi)) { - d_echo("strpos: $descr, $bi \n"); + foreach (Config::getOsSetting($os, 'ignore_mount_string') as $ims) { + if (str_contains($descr, $ims)) { + d_echo("ignored $descr (matched: $ims)\n"); return true; } } - foreach (Config::get('ignore_mount_regexp', array()) as $bi) { - if (preg_match($bi, $descr)) { - d_echo("preg_match $bi, $descr \n"); + foreach (Config::getOsSetting($os, 'ignore_mount_regexp') as $imr) { + if (preg_match($imr, $descr)) { + d_echo("ignored $descr (matched: $imr)\n"); return true; } } diff --git a/includes/discovery/storage/hrstorage.inc.php b/includes/discovery/storage/hrstorage.inc.php index eac84061b5..669bed6302 100644 --- a/includes/discovery/storage/hrstorage.inc.php +++ b/includes/discovery/storage/hrstorage.inc.php @@ -43,7 +43,7 @@ if (is_array($hrstorage_array)) { continue; } - if (ignore_storage($descr)) { + if (ignore_storage($device['os'], $descr)) { continue; } diff --git a/includes/discovery/storage/ucd-dsktable.inc.php b/includes/discovery/storage/ucd-dsktable.inc.php index 90eceffcad..f247a2d461 100644 --- a/includes/discovery/storage/ucd-dsktable.inc.php +++ b/includes/discovery/storage/ucd-dsktable.inc.php @@ -13,7 +13,7 @@ if (is_array($dsktable_array)) { $dsk['dskAvail'] = ($entry['dskAvail'] * 1024); $dsk['dskUsed'] = $dsk['dskTotal'] - $dsk['dskAvail']; - if (!ignore_storage($dsk['dskPath'])) { + if (ignore_storage($device['os'], $dsk['dskPath']) != 1) { discover_storage($valid_storage, $device, $dsk['dskIndex'], 'dsk', 'ucd-dsktable', $dsk['dskPath'], $dsk['dskTotal'], 1024, $dsk['dskUsed']); } } diff --git a/mkdocs.yml b/mkdocs.yml index 331462a51e..904b1dc34b 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -115,6 +115,7 @@ pages: - Developing/os/Health-Information.md - Developing/os/Wireless-Sensors.md - Developing/os/Custom-Graphs.md + - Developing/os/Settings.md - Extensions/Agent-Setup.md - Extensions/Alerting.md - Extensions/metrics/InfluxDB.md diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 5aab5c37bc..67a2adf6ea 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -80,13 +80,29 @@ class ConfigTest extends \PHPUnit_Framework_TestCase public function testGetOsSetting() { global $config; - $device = array('os' => 'nullos'); $config['os']['nullos']['fancy'] = true; + $config['fallback'] = true; - $this->assertNull(Config::getOsSetting(array(), 'unset'), '$device array missing os should return null'); - $this->assertNull(Config::getOsSetting($device, 'unset'), 'Non-existing settings should return null'); - $this->assertFalse(Config::getOsSetting($device, 'unset', false), 'Non-existing settings should return $default'); - $this->assertTrue(Config::getOsSetting($device, 'fancy'), 'Failed to get setting'); + $this->assertNull(Config::getOsSetting(null, 'unset'), '$os is null, should return null'); + $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'); + } + + public function testGetCombined() + { + global $config; + $config['num'] = array('one', 'two'); + $config['os']['nullos']['num'] = array('two', 'three'); + $config['assoc'] = array('a' => 'same', 'b' => 'same'); + $config['os']['nullos']['assoc'] = array('b' => 'different', 'c' => 'still same'); + + $combined = Config::getCombined('nullos', 'num'); + sort($combined); + $this->assertEquals(array('one', 'three', 'two'), $combined); + + $this->assertSame(array('a' => 'same', 'b' => 'different', 'c' => 'still same'), Config::getCombined('nullos', 'assoc')); } public function testSet()