mirror of
https://github.com/librenms/librenms.git
synced 2024-09-21 18:38:25 +00:00
feature: Allow ignore_mount, ignore_mount_string, ignore_mount_regex per OS (#7304)
* feature: Allow ignore_mount, ignore_mount_string, ignore_mount_regex at OS level * Change the storage settings to override global instead of merge. Add documentation
This commit is contained in:
parent
e0040b2831
commit
98786c0ea7
@ -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)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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).
|
||||
|
25
doc/Developing/os/Settings.md
Normal file
25
doc/Developing/os/Settings.md
Normal file
@ -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/"
|
||||
```
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ if (is_array($hrstorage_array)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ignore_storage($descr)) {
|
||||
if (ignore_storage($device['os'], $descr)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -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']);
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user