feature: Added ability specify options for sensors yaml discovery (#6985)

* feature: Added ability specify options for sensors yaml discovery

* travis fix for new line + skip_values_* -> skip_value_*
This commit is contained in:
Neil Lathwood
2017-07-10 22:27:46 +01:00
committed by Tony Murray
parent 431d15aad9
commit d30e5660f9
6 changed files with 194 additions and 138 deletions

View File

@@ -40,6 +40,9 @@ mib: NETBOTZV2-MIB
modules:
sensors:
airflow:
options:
skip_values_lt: 0
data:
-
oid: airFlowSensorTable
value: airFlowSensorValue
@@ -53,10 +56,12 @@ At the top you can define one or more mibs to be used in the lookup of data:
`mib: NETBOTZV2-MIB`
For `data:` you have the following options:
The only sensor we have defined here is airflow. The available options are as follows:
- `oid` (required): This is the name of the table you want to do the snmp walk on.
- `value` (required): This is the key within the table that contains the value.
- `value` (optional): This is the key within the table that contains the value. If not provided will use `oid`
- `num_oid` (required): This is the numerical OID that contains `value`. This should always be without the appended `index`.
- `divisor` (optional): This is the divisor to use against the returned `value`.
- `multiplier` (optional): This is the multiplier to use against the returned `value`.
@@ -67,6 +72,16 @@ The only sensor we have defined here is airflow. The available options are as fo
- `descr` (required): The visible label for this sensor. It can be a key with in the table or a static string, optionally using `{{ index }}`
- `index` (optional): This is the index value we use to uniquely identify this sensor. `{{ $index }}` will be replaced by the `index` from the snmp walk.
- `skip_values` (optional): This is an array of values we should skip over.
- `skip_value_lt` (optional): If sensor value is less than this, skip the discovery.
- `skip_value_gt` (optional): If sensor value is greater than this, skip the discovery.
For `options:` you have the following available:
- `divisor`: This is the divisor to use against the returned `value`.
- `multiplier`: This is the multiplier to use against the returned `value`.
- `skip_values`: This is an array of values we should skip over.
- `skip_value_lt`: If sensor value is less than this, skip the discovery.
- `skip_value_gt`: If sensor value is greater than this, skip the discovery.
If you aren't able to use yaml to perform the sensor discovery, you will most likely need to use Advanced health discovery.

View File

@@ -2,6 +2,7 @@ mib: PowerNet-MIB
modules:
sensors:
state:
data:
-
oid: upsBasicOutputStatus
value: upsBasicOutputStatus

View File

@@ -2,6 +2,7 @@ mib: NETBOTZV2-MIB
modules:
sensors:
airflow:
data:
-
oid: airFlowSensorTable
value: airFlowSensorValue
@@ -10,6 +11,7 @@ modules:
descr: airFlowSensorLabel
index: 'airFlowSensorValue.{{ $index }}'
temperature:
data:
-
oid: dewPointSensorTable
value: dewPointSensorValue
@@ -24,6 +26,7 @@ modules:
descr: tempSensorLabel
index: '{{ $index }}'
humidity:
data:
-
oid: humiSensorTable
value: humiSensorValue
@@ -31,6 +34,7 @@ modules:
descr: humiSensorLabel
index: '{{ $index }}'
state:
data:
-
oid: dryContactSensorTable
value: dryContactSensorValue

View File

@@ -2,6 +2,7 @@ mib: RITTAL-CMC-III-MIB
modules:
sensors:
state:
data:
-
oid: cmcIIIUnitStatus
value: cmcIIIUnitStatus

View File

@@ -1049,30 +1049,65 @@ function ignore_storage($descr)
return $deny;
}
/**
* @param $value
* @param $data
* @param $group
* @return bool
*/
function can_skip_sensor($value, $data, $group)
{
$skip_values = array_replace((array)$group['skip_values'], (array)$data['skip_values']);
foreach ($skip_values as $skip_value) {
if ($value == $skip_value) {
return true;
}
}
$skip_values_lt = array_replace((array)$group['skip_value_lt'], (array)$data['skip_value_lt']);
foreach ($skip_value_lt as $skip_value) {
if ($value < $skip_value) {
return true;
}
}
$skip_values_gt = array_reduce((array)$group['skip_value_gt'], (array)$data['skip_value_gt']);
foreach ($skip_value_gt as $skip_value) {
if ($value > $skip_value) {
return true;
}
}
return false;
}
/**
* @param $valid
* @param $device
* @param $sensor_type
* @param $pre_cache
*/
function discovery_process(&$valid, $device, $sensor_type, $pre_cache)
{
if ($device['dynamic_discovery']['modules']['sensors'][$sensor_type]) {
foreach ($device['dynamic_discovery']['modules']['sensors'][$sensor_type] as $data) {
$sensor_options = array();
if (isset($device['dynamic_discovery']['modules']['sensors'][$sensor_type]['options'])) {
$sensor_options = $device['dynamic_discovery']['modules']['sensors'][$sensor_type]['options'];
}
foreach ($device['dynamic_discovery']['modules']['sensors'][$sensor_type]['data'] as $data) {
$tmp_name = $data['oid'];
$raw_data = $pre_cache[$tmp_name];
foreach ($raw_data as $index => $snmp_data) {
$skip = false;
$value = $snmp_data[$data['value']];
foreach ((array)$data['skip_values'] as $skip_value) {
echo "Here $value and $skip_value END\n";
if ($value == $skip_value) {
$skip = true;
}
}
if ($skip === false && is_numeric($value)) {
$value = $snmp_data[$data['value']] ?: $snmp_data[$data['oid']];
if (can_skip_sensor($value, $data, $sensor_options) === false && is_numeric($value)) {
$oid = $data['num_oid'] . $index;
if (isset($snmp_data[$data['descr']])) {
$descr = $snmp_data[$data['descr']];
} else {
$descr = str_replace('{{ $index }}', $index, $data['descr']);
}
$divisor = $data['divisor'] ?: 1;
$multiplier = $data['multiplier'] ?: 1;
$divisor = $data['divisor'] ?: $sensor_options['divisor'] ?: 1;
$multiplier = $data['multiplier'] ?: $sensor_options['multiplier'] ?: 1;
$low_limit = $data['low_limit'] ?: 'null';
$low_warn_limit = $data['low_warn_limit'] ?: 'null';
$warn_limit = $data['warn_limit'] ?: 'null';
@@ -1086,7 +1121,7 @@ function discovery_process(&$valid, $device, $sensor_type, $pre_cache)
$value = $value * $multiplier;
}
} else {
$state_name = $data['state_name'];
$state_name = $data['state_name'] ?: $data['oid'];
$state_index_id = create_state_index($state_name);
if ($state_index_id != null) {
foreach ($data['states'] as $state) {

View File

@@ -64,10 +64,10 @@ class YamlTest extends \PHPUnit_Framework_TestCase
foreach ($data['modules'] as $module => $sub_modules) {
foreach ($sub_modules as $sub_module) {
foreach ($sub_module as $sensor) {
foreach ($sub_module['data'] as $sensor) {
$this->assertArrayHasKey('oid', $sensor, $file);
$this->assertArrayHasKey('num_oid', $sensor, $file);
$this->assertArrayHasKey('value', $sensor, $file);
$this->assertArrayHasKey('descr', $sensor, $file);
}
}
}