From cece4d8dda4c792108c18a88413910c9515dd4de Mon Sep 17 00:00:00 2001 From: laf Date: Sat, 17 Jun 2017 16:58:42 +0100 Subject: [PATCH 01/10] feature: Added support for sensors to be discovered from yaml --- includes/discovery/functions.inc.php | 74 ++++++++++++++- includes/discovery/sensors.inc.php | 11 +++ .../discovery/sensors/airflow/netbotz.inc.php | 43 --------- .../sensors/humidity/netbotz.inc.php | 26 ------ .../sensors/pre-cache/netbotz.inc.php | 30 ------ .../discovery/sensors/state/netbotz.inc.php | 91 ------------------- .../sensors/temperature/netbotz.inc.php | 40 -------- 7 files changed, 80 insertions(+), 235 deletions(-) delete mode 100644 includes/discovery/sensors/airflow/netbotz.inc.php delete mode 100644 includes/discovery/sensors/humidity/netbotz.inc.php delete mode 100644 includes/discovery/sensors/pre-cache/netbotz.inc.php delete mode 100644 includes/discovery/sensors/temperature/netbotz.inc.php diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index 469ba5ea312c..154743a612af 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -85,9 +85,23 @@ function discover_new_device($hostname, $device = '', $method = '', $interface = d_echo("$ip not in a matched network - skipping\n"); }//end if } - //end discover_new_device() +/** + * @param $device + */ +function load_discovery(&$device) +{ + global $config; + $yaml_discovery = $config['install_dir'] . '/includes/definitions/discovery/' . $device['os'] . '.yaml'; + if (file_exists($yaml_discovery)) { + $device['dynamic_discovery'] = Symfony\Component\Yaml\Yaml::parse( + file_get_contents($yaml_discovery) + ); + } + unset($yaml_discovery); +} + function discover_device(&$device, $options = null) { global $config, $valid; @@ -119,6 +133,7 @@ function discover_device(&$device, $options = null) } load_os($device); + load_discovery($device); if (is_array($config['os'][$device['os']]['register_mibs'])) { register_mibs($device, $config['os'][$device['os']]['register_mibs'], 'includes/discovery/os/' . $device['os'] . '.inc.php'); } @@ -182,14 +197,11 @@ function discover_device(&$device, $options = null) echo "\n"; $discovered_devices++; } - //end discover_device() -// Discover sensors - +// Discover sensors function discover_sensor(&$valid, $class, $device, $oid, $index, $type, $descr, $divisor = 1, $multiplier = 1, $low_limit = null, $low_warn_limit = null, $warn_limit = null, $high_limit = null, $current = null, $poller_type = 'snmp', $entPhysicalIndex = null, $entPhysicalIndex_measured = null, $user_func = null) { - $low_limit = set_null($low_limit); $low_warn_limit = set_null($low_warn_limit); $warn_limit = set_null($warn_limit); @@ -1032,6 +1044,57 @@ function ignore_storage($descr) return $deny; } +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) { + $tmp_name = $data['oid_name'] ?: $data['oid']; + $raw_data = $pre_cache[$tmp_name]; + foreach ($raw_data as $index => $snmp_data) { + $value = $snmp_data[$data['value']]; + if ($snmp_data[$data['value']]) { + $oid = $data['num_oid'] . $index; + $descr = $snmp_data[$data['descr']]; + if ($sensor_type !== 'state') { + $divisor = $data['divisor'] ?: 1; + $multiplier = $data['multiplier'] ?: 1; + if (is_numeric($divisor)) { + $value = $value / $divisor; + } + if (is_numeric($multiplier)) { + $value = $value * $multiplier; + } + $low_limit = $data['low_limit'] ?: null; + $low_warn_limit = $data['low_warn_limit'] ?: null; + $warn_limit = $data['warn_limit'] ?: null; + $high_limit = $data['high_limit'] ?: null; + } else { + $state_name = $data['descr']; + $state_index_id = create_state_index($state_name); + foreach ($data['states'] as $state) { + $insert = array( + 'state_index_id' => $state_index_id, + 'state_descr' => $state['descr'], + 'state_draw_graph' => $state['graph'], + 'state_value' => $state['value'], + 'state_generic_value' => $state['generic'] + ); + dbInsert($insert, 'state_translations'); + } + } + $tmp_index = str_replace('{{ $index }}', $index, $data['index']); + if ($sensor_type === 'state') { + discover_sensor($valid['sensor'], $sensor_type, $device, $oid, $tmp_index, $state_name, $descr, $divisor, $multiplier, $low_limit, $low_warn_limit, $warn_limit, $high_limit, $value); + create_sensor_to_state_index($device, $state_name, $tmp_index); + } else { + discover_sensor($valid['sensor'], $sensor_type, $device, $oid, $tmp_index, $device['os'], $descr, $divisor, $multiplier, $low_limit, $low_warn_limit, $warn_limit, $high_limit, $value); + } + } + } + } + } +} + /** * @param $types * @param $device @@ -1055,6 +1118,7 @@ function sensors($types, $device, $valid, $pre_cache = array()) include $dir . '/rfc1628.inc.php'; } } + discovery_process($valid, $device, $sensor_type, $pre_cache); d_echo($valid['sensor'][$sensor_type]); check_valid_sensors($device, $sensor_type, $valid['sensor']); echo "\n"; diff --git a/includes/discovery/sensors.inc.php b/includes/discovery/sensors.inc.php index 47b66660919e..e326f73c795c 100644 --- a/includes/discovery/sensors.inc.php +++ b/includes/discovery/sensors.inc.php @@ -12,6 +12,17 @@ d_echo($pre_cache); } +if (isset($device['dynamic_discovery']['modules']['sensors'])) { + foreach ($device['dynamic_discovery']['modules']['sensors'] as $key => $data_array) { + foreach ($data_array as $data) { + foreach ((array)$data['oid'] as $oid) { + $tmp_name = $data['oid_name'] ?: $data['oid']; + $pre_cache[$tmp_name] = snmpwalk_cache_oid($device, $oid, $pre_cache[$tmp_name], $device['dynamic_discovery']['mib'], null, '-OeQUs'); + } + } + } +} + // Run custom sensors require 'includes/discovery/sensors/cisco-entity-sensor.inc.php'; require 'includes/discovery/sensors/entity-sensor.inc.php'; diff --git a/includes/discovery/sensors/airflow/netbotz.inc.php b/includes/discovery/sensors/airflow/netbotz.inc.php deleted file mode 100644 index 80ee1baa5668..000000000000 --- a/includes/discovery/sensors/airflow/netbotz.inc.php +++ /dev/null @@ -1,43 +0,0 @@ -. - * - * @package LibreNMS - * @link http://librenms.org - * @copyright 2017 Neil Lathwood - * @author Neil Lathwood - */ - -d_echo($pre_cache['netbotz_airflow']); - -if (is_array($pre_cache['netbotz_airflow'])) { - echo 'NetBotz '; - foreach ($pre_cache['netbotz_airflow'] as $index => $data) { - if ($data['airFlowSensorValue']) { - $divisor = 10; - $multiplier = 1; - $value = $data['airFlowSensorValue'] / $divisor; - $oid = '.1.3.6.1.4.1.5528.100.4.1.5.1.2.' . $index; - $index = 'airFlowSensorValue.' . $index; - $descr = $data['airFlowSensorLabel']; - if (is_numeric($value)) { - discover_sensor($valid['sensor'], 'airflow', $device, $oid, $index, 'netbotz', $descr, $divisor, $multiplier, null, null, null, null, $value); - } - } - } -} diff --git a/includes/discovery/sensors/humidity/netbotz.inc.php b/includes/discovery/sensors/humidity/netbotz.inc.php deleted file mode 100644 index 98bbd25ad939..000000000000 --- a/includes/discovery/sensors/humidity/netbotz.inc.php +++ /dev/null @@ -1,26 +0,0 @@ -= 0) { - discover_sensor($valid['sensor'], 'humidity', $device, $humidity_oid, $humidity_id, 'netbotz', $descr, '1', '1', null, null, null, null, $humidity); - } - } - - unset($data); -} - -unset($oids); diff --git a/includes/discovery/sensors/pre-cache/netbotz.inc.php b/includes/discovery/sensors/pre-cache/netbotz.inc.php deleted file mode 100644 index de8ec4b9c007..000000000000 --- a/includes/discovery/sensors/pre-cache/netbotz.inc.php +++ /dev/null @@ -1,30 +0,0 @@ -. - * - * @package LibreNMS - * @link http://librenms.org - * @copyright 2017 Neil Lathwood - * @author Neil Lathwood - */ - -$pre_cache['netbotz_airflow'] = snmpwalk_cache_oid($device, 'airFlowSensorTable', array(), 'NETBOTZV2-MIB'); -$pre_cache['netbotz_temperature'] = snmpwalk_cache_oid($device, 'dewPointSensorTable', array(), 'NETBOTZV2-MIB'); -$pre_cache['netbotz_state'] = snmpwalk_cache_oid($device, 'dryContactSensorTable', array(), 'NETBOTZV2-MIB', null, '-OeQUs'); -$pre_cache['netbotz_state'] = snmpwalk_cache_oid($device, 'doorSwitchSensorTable', $pre_cache['netbotz_state'], 'NETBOTZV2-MIB', null, '-OeQUs'); -$pre_cache['netbotz_state'] = snmpwalk_cache_oid($device, 'cameraMotionSensorTable', $pre_cache['netbotz_state'], 'NETBOTZV2-MIB', null, '-OeQUs'); diff --git a/includes/discovery/sensors/state/netbotz.inc.php b/includes/discovery/sensors/state/netbotz.inc.php index 855f374cee9f..552e3fa1d0ee 100644 --- a/includes/discovery/sensors/state/netbotz.inc.php +++ b/includes/discovery/sensors/state/netbotz.inc.php @@ -9,94 +9,3 @@ * option) any later version. Please see LICENSE.txt at the top level of * the source code distribution for details. */ - -$temp = snmpwalk_cache_multi_oid($device, 'otherStateSensorTable', array(), 'NETBOTZ410-MIB'); -$cur_oid = '.1.3.6.1.4.1.5528.100.4.2.10.1.3.'; - -if (is_array($temp)) { - //Create State Index - $state_name = 'otherStateSensorErrorStatus'; - $state_index_id = create_state_index($state_name); - - //Create State Translation - if ($state_index_id !== null) { - $states = array( - array($state_index_id, 'normal', 0, 0, 0), - array($state_index_id, 'info', 0, 1, 1), - array($state_index_id, 'warning', 0, 2, 1), - array($state_index_id, 'error', 0, 3, 2), - array($state_index_id, 'critical', 0, 4, 2), - array($state_index_id, 'failure', 0, 5, 2) - ); - foreach ($states as $value) { - $insert = array( - 'state_index_id' => $value[0], - 'state_descr' => $value[1], - 'state_draw_graph' => $value[2], - 'state_value' => $value[3], - 'state_generic_value' => $value[4] - ); - dbInsert($insert, 'state_translations'); - } - } - - foreach ($temp as $index => $entry) { - $descr = $temp[$index]['otherStateSensorLabel']; - //Discover Sensors - discover_sensor($valid['sensor'], 'state', $device, $cur_oid . $index, $index, $state_name, $descr, '1', '1', null, null, null, null, $temp[$index][' otherStateSensorErrorStatus'], 'snmp', $index); - - //Create Sensor To State Index - create_sensor_to_state_index($device, $state_name, $index); - } -} - - -foreach ($pre_cache['netbotz_state'] as $index => $data) { - if (is_array($data)) { - $tmp_keys = array_keys($data); - $state_name = str_replace('Id', '', $tmp_keys[0]); - $state_index_id = create_state_index($state_name); - if ($state_name === 'cameraMotionSensor') { - $states = array( - array($state_index_id, 'null', 0, -1, 3), - array($state_index_id, 'noMotion', 0, 0, 0), - array($state_index_id, 'motionDetected', 0, 1, 2), - ); - $oid = '.1.3.6.1.4.1.5528.100.4.2.3.1.2.'; - } elseif ($state_name === 'doorSwitchSensor') { - $states = array( - array($state_index_id, 'null', 0, -1, 3), - array($state_index_id, 'open', 0, 0, 0), - array($state_index_id, 'closed', 0, 1, 2), - ); - $oid = '.1.3.6.1.4.1.5528.100.4.2.2.1.2.'; - } elseif ($state_name === 'dryContactSensor') { - $states = array( - array($state_index_id, 'null', 0, -1, 3), - array($state_index_id, 'open', 0, 0, 0), - array($state_index_id, 'closed', 0, 1, 2), - ); - $oid = '.1.3.6.1.4.1.5528.100.4.2.1.1.2.'; - } - if ($state_index_id !== null) { - foreach ($states as $value) { - $insert = array( - 'state_index_id' => $value[0], - 'state_descr' => $value[1], - 'state_draw_graph' => $value[2], - 'state_value' => $value[3], - 'state_generic_value' => $value[4] - ); - dbInsert($insert, 'state_translations'); - } - } - $cur_oid = $oid . $index; - $index = $state_name . '.' . $index; - $descr = $data[$state_name . 'Label']; - $value = $data[$state_name . 'Value']; - if (isset($value)) { - discover_sensor($valid['sensor'], 'state', $device, $cur_oid, $index, $state_name, $descr, 1, 1, null, null, null, null, $value); - create_sensor_to_state_index($device, $state_name, $index); - } - } -} diff --git a/includes/discovery/sensors/temperature/netbotz.inc.php b/includes/discovery/sensors/temperature/netbotz.inc.php deleted file mode 100644 index 4c302937df89..000000000000 --- a/includes/discovery/sensors/temperature/netbotz.inc.php +++ /dev/null @@ -1,40 +0,0 @@ - $data) { - if ($data['dewPointSensorValue']) { - $divisor = 10; - $multiplier = 1; - $value = $data['dewPointSensorValue'] / $divisor; - $oid = '.1.3.6.1.4.1.5528.100.4.1.3.1.2.' . $index; - $index = 'dewPointSensorValue.' . $index; - $descr = $data['dewPointSensorLabel']; - if (is_numeric($value)) { - discover_sensor($valid['sensor'], 'temperature', $device, $oid, $index, 'netbotz', $descr, $divisor, $multiplier, null, null, null, null, $value); - } - } - } -} From 202b3fa5a852ba93fe5c7210ff59a52fb0c0f469 Mon Sep 17 00:00:00 2001 From: laf Date: Sat, 17 Jun 2017 19:54:42 +0100 Subject: [PATCH 02/10] added discovery definitions --- includes/definitions/discovery/netbotz.yaml | 84 +++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 includes/definitions/discovery/netbotz.yaml diff --git a/includes/definitions/discovery/netbotz.yaml b/includes/definitions/discovery/netbotz.yaml new file mode 100644 index 000000000000..e5ec1beceaa9 --- /dev/null +++ b/includes/definitions/discovery/netbotz.yaml @@ -0,0 +1,84 @@ +mib: NETBOTZV2-MIB +modules: + sensors: + airflow: + - + oid_name: netbotz_airflow + oid: airFlowSensorTable + value: airFlowSensorValue + divisor: 10 + num_oid: .1.3.6.1.4.1.5528.100.4.1.5.1.2. + descr: airFlowSensorLabel + index: 'airFlowSensorValue.{{ $index }}' + temperature: + - + oid_name: netbotz_temperature + oid: dewPointSensorTable + value: dewPointSensorValue + divisor: 10 + num_oid: .1.3.6.1.4.1.5528.100.4.1.3.1.2. + descr: dewPointSensorLabel + index: 'dewPointSensorValue.{{ $index }}' + - + oid_name: netbotz_temperature + oid: tempSensorTable + value: tempSensorValue + num_oid: .1.3.6.1.4.1.5528.100.4.1.1.1.8. + descr: tempSensorLabel + index: '{{ $index }}' + humidity: + - + oid_name: netbotz_humidity + oid: humiSensorTable + value: humiSensorValue + num_oid: .1.3.6.1.4.1.5528.100.4.1.2.1.8. + descr: humiSensorLabel + index: '{{ $index }}' + state: + - + oid: dryContactSensorTable + value: dryContactSensorValue + num_oid: .1.3.6.1.4.1.5528.100.4.2.1.1.2. + descr: dryContactSensorLabel + index: 'dryContactSensor.{{ $index }}' + state_name: dryContactSensor + states: + - { descr: 'null', graph: 0, value: -1, generic: 3 } + - { descr: open, graph: 0, value: 0, generic: 0 } + - { descr: closed, graph: 0, value: 1, generic: 2 } + - + oid: doorSwitchSensorTable + value: doorSwitchSensorValue + num_oid: .1.3.6.1.4.1.5528.100.4.2.2.1.2. + descr: doorSwitchSensorLabel + index: 'doorSwitchSensor.{{ $index }}' + state_name: doorSwitchSensor + states: + - { descr: 'null', graph: 0, value: -1, generic: 3 } + - { descr: open, graph: 0, value: 0, generic: 0 } + - { descr: closed, graph: 0, value: 1, generic: 2 } + - + oid: cameraMotionSensorTable + value: cameraMotionSensorValue + num_oid: .1.3.6.1.4.1.5528.100.4.2.3.1.2. + descr: cameraMotionSensorLabel + index: 'cameraMotionSensor.{{ $index }}' + state_name: cameraMotionSensor + states: + - { descr: 'null', graph: 0, value: -1, generic: 3 } + - { descr: noMotion, graph: 0, value: 0, generic: 0 } + - { descr: motionDetected, graph: 0, value: 1, generic: 2 } + - + oid: otherStateSensorTable + value: otherStateSensorValue + num_oid: .1.3.6.1.4.1.5528.100.4.2.10.1.3. + descr: otherStateSensorLabel + index: '{{ $index }}' + state_name: otherStateSensorErrorStatus + states: + - { descr: normal, graph: 0, value: 0, generic: 0 } + - { descr: info, graph: 0, value: 1, generic: 1 } + - { descr: warning, graph: 0, value: 2, generic: 1 } + - { descr: error, graph: 0, value: 3, generic: 2 } + - { descr: critical, graph: 0, value: 4, generic: 2 } + - { descr: failure, graph: 0, value: 5, generic: 2 } From 5c24c7a85850702b90df1007f2418745e254ed01 Mon Sep 17 00:00:00 2001 From: laf Date: Sun, 18 Jun 2017 22:14:37 +0100 Subject: [PATCH 03/10] Updated docs, more code updates + tests --- doc/Developing/Sensor-State-Support.md | 60 +++++++++++++++++++++++++ doc/Developing/os/Health-Information.md | 53 ++++++++++++++++++++++ includes/discovery/functions.inc.php | 27 ++++++----- tests/YamlTest.php | 28 +++++++++++- 4 files changed, 156 insertions(+), 12 deletions(-) diff --git a/doc/Developing/Sensor-State-Support.md b/doc/Developing/Sensor-State-Support.md index d27170168a50..43c413d29186 100644 --- a/doc/Developing/Sensor-State-Support.md +++ b/doc/Developing/Sensor-State-Support.md @@ -48,6 +48,66 @@ We also map these values to the actual state sensor(state_index) where these val *Is as you might have guessed, where the sensor_id is mapped to a state_index_id.* ### Example + +For YAML based state discovery: + +```yaml +mib: NETBOTZV2-MIB +modules: + sensors: + state: + - + oid: dryContactSensorTable + value: dryContactSensorValue + num_oid: .1.3.6.1.4.1.5528.100.4.2.1.1.2. + descr: dryContactSensorLabel + index: 'dryContactSensor.{{ $index }}' + state_name: dryContactSensor + states: + - { descr: 'null', graph: 0, value: -1, generic: 3 } + - { descr: open, graph: 0, value: 0, generic: 0 } + - { descr: closed, graph: 0, value: 1, generic: 2 } + - + oid: doorSwitchSensorTable + value: doorSwitchSensorValue + num_oid: .1.3.6.1.4.1.5528.100.4.2.2.1.2. + descr: doorSwitchSensorLabel + index: 'doorSwitchSensor.{{ $index }}' + state_name: doorSwitchSensor + states: + - { descr: 'null', graph: 0, value: -1, generic: 3 } + - { descr: open, graph: 0, value: 0, generic: 0 } + - { descr: closed, graph: 0, value: 1, generic: 2 } + - + oid: cameraMotionSensorTable + value: cameraMotionSensorValue + num_oid: .1.3.6.1.4.1.5528.100.4.2.3.1.2. + descr: cameraMotionSensorLabel + index: 'cameraMotionSensor.{{ $index }}' + state_name: cameraMotionSensor + states: + - { descr: 'null', graph: 0, value: -1, generic: 3 } + - { descr: noMotion, graph: 0, value: 0, generic: 0 } + - { descr: motionDetected, graph: 0, value: 1, generic: 2 } + - + oid: otherStateSensorTable + value: otherStateSensorValue + num_oid: .1.3.6.1.4.1.5528.100.4.2.10.1.3. + descr: otherStateSensorLabel + index: '{{ $index }}' + state_name: otherStateSensorErrorStatus + states: + - { descr: normal, graph: 0, value: 0, generic: 0 } + - { descr: info, graph: 0, value: 1, generic: 1 } + - { descr: warning, graph: 0, value: 2, generic: 1 } + - { descr: error, graph: 0, value: 3, generic: 2 } + - { descr: critical, graph: 0, value: 4, generic: 2 } + - { descr: failure, graph: 0, value: 5, generic: 2 } + +``` + +For advanced state discovery: + This example will be based on a Cisco power supply sensor and is all it takes to have sensor state support for Cisco power supplys in Cisco switches. The file should be located in /includes/discovery/sensors/state/cisco.inc.php. diff --git a/doc/Developing/os/Health-Information.md b/doc/Developing/os/Health-Information.md index 9bdcb007964b..74248e0007e2 100644 --- a/doc/Developing/os/Health-Information.md +++ b/doc/Developing/os/Health-Information.md @@ -1,5 +1,7 @@ source: Developing/os/Health-Information.md +#### Sensors + This document will guide you through adding health / sensor information for your new device. Currently we have support for the following health metrics along with the values we expect to see the data in: @@ -17,10 +19,61 @@ Currently we have support for the following health metrics along with the values | power | W | | runtime | Min | | signal | dBm | +| snr | SNR | | state | # | | temperature | C | | voltage | V | +#### Simple health discovery + +We have support for defining health / sensor discovery using YAML files so that you don't need to know how to write PHP. + +All yaml files are located in `includes/definitions/discovery/$os.yaml`. Defining the information hear is not always +possible and is heavily reliant on vendors being sensible with the MIBs they generate. Only snmp walks are supported +and you must provide a sane table that can be traversed and contains all of the data you need. We will use netbotz as +an example here. + +`includes/definitions/discovery/netbotz.yaml` + +```yaml +mib: NETBOTZV2-MIB +modules: + sensors: + airflow: + - + oid_name: netbotz_airflow + oid: airFlowSensorTable + value: airFlowSensorValue + divisor: 10 + num_oid: .1.3.6.1.4.1.5528.100.4.1.5.1.2. + descr: airFlowSensorLabel + index: 'airFlowSensorValue.{{ $index }}' +``` + +At the top you can define one or more mibs to be used in the lookup of data: + +`mib: NETBOTZV2-MIB` + +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. + - `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`. + - `low_limit` (optional): This is the critical low threshold that `value` should be (used in alerting). + - `low_warn_limit` (optional): This is the warning low threshold that `value` should be (used in alerting). + - `warn_limit` (optional): This is the warning high threshold that `value` should be (used in alerting). + - `high_limit` (optional): This is the critical high threshold that `value` should be (used in alerting). + - `descr` (required): This is the key within the table that contains the description of this sensor. + - `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. + +If you aren't able to use yaml to perform the sensor discovery, you will most likely need to use Advanced health discovery. + +#### Advanced health discovery + +If you can't use the yaml files as above, then you will need to create the discovery code in php. + The directory structure for sensor information is `includes/discovery/sensors/$class/$os.inc.php`. The format of all of the sensors follows the same code format which is to call the `discover_sensor()` function - with the exception of state which requires additional code. diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index 154743a612af..63b6ebb1178f 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -1054,20 +1054,24 @@ function discovery_process(&$valid, $device, $sensor_type, $pre_cache) $value = $snmp_data[$data['value']]; if ($snmp_data[$data['value']]) { $oid = $data['num_oid'] . $index; - $descr = $snmp_data[$data['descr']]; + if (isset($snmp_data[$data['descr']])) { + $descr = $snmp_data[$data['descr']]; + } else { + $descr = $data['descr']; + } + $divisor = $data['divisor'] ?: 1; + $multiplier = $data['multiplier'] ?: 1; + $low_limit = $data['low_limit'] ?: 'null'; + $low_warn_limit = $data['low_warn_limit'] ?: 'null'; + $warn_limit = $data['warn_limit'] ?: 'null'; + $high_limit = $data['high_limit'] ?: 'null'; if ($sensor_type !== 'state') { - $divisor = $data['divisor'] ?: 1; - $multiplier = $data['multiplier'] ?: 1; if (is_numeric($divisor)) { $value = $value / $divisor; } if (is_numeric($multiplier)) { $value = $value * $multiplier; } - $low_limit = $data['low_limit'] ?: null; - $low_warn_limit = $data['low_warn_limit'] ?: null; - $warn_limit = $data['warn_limit'] ?: null; - $high_limit = $data['high_limit'] ?: null; } else { $state_name = $data['descr']; $state_index_id = create_state_index($state_name); @@ -1082,12 +1086,13 @@ function discovery_process(&$valid, $device, $sensor_type, $pre_cache) dbInsert($insert, 'state_translations'); } } - $tmp_index = str_replace('{{ $index }}', $index, $data['index']); + $tmp_index = $data['index'] ?: $index; + $uindex = str_replace('{{ $index }}', $index, $tmp_index); if ($sensor_type === 'state') { - discover_sensor($valid['sensor'], $sensor_type, $device, $oid, $tmp_index, $state_name, $descr, $divisor, $multiplier, $low_limit, $low_warn_limit, $warn_limit, $high_limit, $value); - create_sensor_to_state_index($device, $state_name, $tmp_index); + discover_sensor($valid['sensor'], $sensor_type, $device, $oid, $uindex, $state_name, $descr, $divisor, $multiplier, $low_limit, $low_warn_limit, $warn_limit, $high_limit, $value); + create_sensor_to_state_index($device, $state_name, $uindex); } else { - discover_sensor($valid['sensor'], $sensor_type, $device, $oid, $tmp_index, $device['os'], $descr, $divisor, $multiplier, $low_limit, $low_warn_limit, $warn_limit, $high_limit, $value); + discover_sensor($valid['sensor'], $sensor_type, $device, $oid, $uindex, $device['os'], $descr, $divisor, $multiplier, $low_limit, $low_warn_limit, $warn_limit, $high_limit, $value); } } } diff --git a/tests/YamlTest.php b/tests/YamlTest.php index 2d7a14613675..0eb61e1e6b24 100644 --- a/tests/YamlTest.php +++ b/tests/YamlTest.php @@ -32,7 +32,7 @@ class YamlTest extends \PHPUnit_Framework_TestCase { - public function testYaml() + public function testOSYaml() { global $config; @@ -49,4 +49,30 @@ public function testYaml() $this->assertArrayHasKey('text', $data, $file); } } + + public function testDiscoveryYaml() + { + global $config; + + $pattern = $config['install_dir'] . '/includes/definitions/discovery/*.yaml'; + foreach (glob($pattern) as $file) { + try { + $data = Yaml::parse(file_get_contents($file)); + } catch (ParseException $e) { + throw new PHPUnitException("$file Could not be parsed"); + } + + foreach ($data['modules'] as $module => $sub_modules) { + foreach ($sub_modules as $sub_module) { + foreach ($sub_module as $sensor) { + $this->assertArrayHasKey('oid', $sensor, $file); + $this->assertArrayHasKey('num_oid', $sensor, $file); + $this->assertArrayHasKey('value', $sensor, $file); + } + } + } + + } + } + } From 04343774363e65cad9ee7fc74d225b29124fc8d4 Mon Sep 17 00:00:00 2001 From: laf Date: Sun, 18 Jun 2017 22:21:04 +0100 Subject: [PATCH 04/10] removed the oid_name use --- includes/definitions/discovery/netbotz.yaml | 4 ---- includes/discovery/functions.inc.php | 2 +- includes/discovery/sensors.inc.php | 2 +- tests/YamlTest.php | 2 -- 4 files changed, 2 insertions(+), 8 deletions(-) diff --git a/includes/definitions/discovery/netbotz.yaml b/includes/definitions/discovery/netbotz.yaml index e5ec1beceaa9..cab712f3f402 100644 --- a/includes/definitions/discovery/netbotz.yaml +++ b/includes/definitions/discovery/netbotz.yaml @@ -3,7 +3,6 @@ modules: sensors: airflow: - - oid_name: netbotz_airflow oid: airFlowSensorTable value: airFlowSensorValue divisor: 10 @@ -12,7 +11,6 @@ modules: index: 'airFlowSensorValue.{{ $index }}' temperature: - - oid_name: netbotz_temperature oid: dewPointSensorTable value: dewPointSensorValue divisor: 10 @@ -20,7 +18,6 @@ modules: descr: dewPointSensorLabel index: 'dewPointSensorValue.{{ $index }}' - - oid_name: netbotz_temperature oid: tempSensorTable value: tempSensorValue num_oid: .1.3.6.1.4.1.5528.100.4.1.1.1.8. @@ -28,7 +25,6 @@ modules: index: '{{ $index }}' humidity: - - oid_name: netbotz_humidity oid: humiSensorTable value: humiSensorValue num_oid: .1.3.6.1.4.1.5528.100.4.1.2.1.8. diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index 63b6ebb1178f..71320fc08f1d 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -1048,7 +1048,7 @@ 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) { - $tmp_name = $data['oid_name'] ?: $data['oid']; + $tmp_name = $data['oid']; $raw_data = $pre_cache[$tmp_name]; foreach ($raw_data as $index => $snmp_data) { $value = $snmp_data[$data['value']]; diff --git a/includes/discovery/sensors.inc.php b/includes/discovery/sensors.inc.php index e326f73c795c..b3d640b70705 100644 --- a/includes/discovery/sensors.inc.php +++ b/includes/discovery/sensors.inc.php @@ -16,7 +16,7 @@ foreach ($device['dynamic_discovery']['modules']['sensors'] as $key => $data_array) { foreach ($data_array as $data) { foreach ((array)$data['oid'] as $oid) { - $tmp_name = $data['oid_name'] ?: $data['oid']; + $tmp_name = $data['oid']; $pre_cache[$tmp_name] = snmpwalk_cache_oid($device, $oid, $pre_cache[$tmp_name], $device['dynamic_discovery']['mib'], null, '-OeQUs'); } } diff --git a/tests/YamlTest.php b/tests/YamlTest.php index 0eb61e1e6b24..e0d371915fc4 100644 --- a/tests/YamlTest.php +++ b/tests/YamlTest.php @@ -71,8 +71,6 @@ public function testDiscoveryYaml() } } } - } } - } From 438c72418aaa13fa90c9268fd07e748ee7343d13 Mon Sep 17 00:00:00 2001 From: laf Date: Mon, 19 Jun 2017 07:54:22 +0100 Subject: [PATCH 05/10] pre-set variable from scrut output --- includes/discovery/functions.inc.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index 71320fc08f1d..4c899fd83d9d 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -1065,6 +1065,7 @@ function discovery_process(&$valid, $device, $sensor_type, $pre_cache) $low_warn_limit = $data['low_warn_limit'] ?: 'null'; $warn_limit = $data['warn_limit'] ?: 'null'; $high_limit = $data['high_limit'] ?: 'null'; + $state_name = ''; if ($sensor_type !== 'state') { if (is_numeric($divisor)) { $value = $value / $divisor; From 67dfde332fed32ec524c2f91a266d0aada8da0ec Mon Sep 17 00:00:00 2001 From: laf Date: Mon, 19 Jun 2017 13:42:37 +0100 Subject: [PATCH 06/10] small updates as per comments in pr --- includes/discovery/sensors.inc.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/includes/discovery/sensors.inc.php b/includes/discovery/sensors.inc.php index b3d640b70705..3badf5af7348 100644 --- a/includes/discovery/sensors.inc.php +++ b/includes/discovery/sensors.inc.php @@ -17,7 +17,9 @@ foreach ($data_array as $data) { foreach ((array)$data['oid'] as $oid) { $tmp_name = $data['oid']; - $pre_cache[$tmp_name] = snmpwalk_cache_oid($device, $oid, $pre_cache[$tmp_name], $device['dynamic_discovery']['mib'], null, '-OeQUs'); + if (is_array($pre_cache[$tmp_name])) { + $pre_cache[$tmp_name] = snmpwalk_cache_oid($device, $oid, array(), $device['dynamic_discovery']['mib'], null, '-OeQUs'); + } } } } From 74f5928630a793d14ff21c1b81a8398864cd91b7 Mon Sep 17 00:00:00 2001 From: laf Date: Tue, 20 Jun 2017 20:42:32 +0100 Subject: [PATCH 07/10] Allow index in descr --- includes/discovery/functions.inc.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index 4c899fd83d9d..959445fd6c11 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -1052,12 +1052,12 @@ function discovery_process(&$valid, $device, $sensor_type, $pre_cache) $raw_data = $pre_cache[$tmp_name]; foreach ($raw_data as $index => $snmp_data) { $value = $snmp_data[$data['value']]; - if ($snmp_data[$data['value']]) { + if ($value) { $oid = $data['num_oid'] . $index; if (isset($snmp_data[$data['descr']])) { $descr = $snmp_data[$data['descr']]; } else { - $descr = $data['descr']; + $descr = str_replace('{{ $index }}', $index, $data['descr']);; } $divisor = $data['divisor'] ?: 1; $multiplier = $data['multiplier'] ?: 1; From 08f2c8e6882cbc47f11dd6331ed2c48ae70d1d28 Mon Sep 17 00:00:00 2001 From: laf Date: Thu, 22 Jun 2017 22:46:10 +0100 Subject: [PATCH 08/10] Added ability to skip values --- doc/Developing/os/Health-Information.md | 1 + includes/definitions/discovery/netbotz.yaml | 2 +- includes/discovery/functions.inc.php | 11 +++++++++-- includes/discovery/sensors.inc.php | 4 ++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/doc/Developing/os/Health-Information.md b/doc/Developing/os/Health-Information.md index 74248e0007e2..a14bfff2a3d1 100644 --- a/doc/Developing/os/Health-Information.md +++ b/doc/Developing/os/Health-Information.md @@ -67,6 +67,7 @@ The only sensor we have defined here is airflow. The available options are as fo - `high_limit` (optional): This is the critical high threshold that `value` should be (used in alerting). - `descr` (required): This is the key within the table that contains the description of this sensor. - `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. If you aren't able to use yaml to perform the sensor discovery, you will most likely need to use Advanced health discovery. diff --git a/includes/definitions/discovery/netbotz.yaml b/includes/definitions/discovery/netbotz.yaml index cab712f3f402..1de4a3d70175 100644 --- a/includes/definitions/discovery/netbotz.yaml +++ b/includes/definitions/discovery/netbotz.yaml @@ -19,7 +19,7 @@ modules: index: 'dewPointSensorValue.{{ $index }}' - oid: tempSensorTable - value: tempSensorValue + value: tempSensorValueInt num_oid: .1.3.6.1.4.1.5528.100.4.1.1.1.8. descr: tempSensorLabel index: '{{ $index }}' diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index 959445fd6c11..f848a3dd9b3b 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -1051,13 +1051,20 @@ function discovery_process(&$valid, $device, $sensor_type, $pre_cache) $tmp_name = $data['oid']; $raw_data = $pre_cache[$tmp_name]; foreach ($raw_data as $index => $snmp_data) { + $skip = false; $value = $snmp_data[$data['value']]; - if ($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 && $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']);; + $descr = str_replace('{{ $index }}', $index, $data['descr']); } $divisor = $data['divisor'] ?: 1; $multiplier = $data['multiplier'] ?: 1; diff --git a/includes/discovery/sensors.inc.php b/includes/discovery/sensors.inc.php index 3badf5af7348..08af8c4e83a6 100644 --- a/includes/discovery/sensors.inc.php +++ b/includes/discovery/sensors.inc.php @@ -16,8 +16,8 @@ foreach ($device['dynamic_discovery']['modules']['sensors'] as $key => $data_array) { foreach ($data_array as $data) { foreach ((array)$data['oid'] as $oid) { - $tmp_name = $data['oid']; - if (is_array($pre_cache[$tmp_name])) { + $tmp_name = $oid; + if (!isset($pre_cache[$tmp_name])) { $pre_cache[$tmp_name] = snmpwalk_cache_oid($device, $oid, array(), $device['dynamic_discovery']['mib'], null, '-OeQUs'); } } From 2b8ff70fa7885a8e8f426f5b36562171d7ac37de Mon Sep 17 00:00:00 2001 From: laf Date: Mon, 26 Jun 2017 19:41:42 +0100 Subject: [PATCH 09/10] Check for numeric values --- includes/discovery/functions.inc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index f848a3dd9b3b..70138d908bd0 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -1059,7 +1059,7 @@ function discovery_process(&$valid, $device, $sensor_type, $pre_cache) $skip = true; } } - if ($skip === false && $value) { + if ($skip === false && is_numeric($value)) { $oid = $data['num_oid'] . $index; if (isset($snmp_data[$data['descr']])) { $descr = $snmp_data[$data['descr']]; From 5401bfd175c05c6a118821957067ad5b2fbe7834 Mon Sep 17 00:00:00 2001 From: laf Date: Mon, 26 Jun 2017 20:06:32 +0100 Subject: [PATCH 10/10] small update of oid name --- doc/Developing/Sensor-State-Support.md | 2 +- includes/definitions/discovery/netbotz.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/Developing/Sensor-State-Support.md b/doc/Developing/Sensor-State-Support.md index 43c413d29186..c76c55a0130a 100644 --- a/doc/Developing/Sensor-State-Support.md +++ b/doc/Developing/Sensor-State-Support.md @@ -91,7 +91,7 @@ modules: - { descr: motionDetected, graph: 0, value: 1, generic: 2 } - oid: otherStateSensorTable - value: otherStateSensorValue + value: otherStateSensorErrorStatus num_oid: .1.3.6.1.4.1.5528.100.4.2.10.1.3. descr: otherStateSensorLabel index: '{{ $index }}' diff --git a/includes/definitions/discovery/netbotz.yaml b/includes/definitions/discovery/netbotz.yaml index 1de4a3d70175..7e7c10ddecd7 100644 --- a/includes/definitions/discovery/netbotz.yaml +++ b/includes/definitions/discovery/netbotz.yaml @@ -66,7 +66,7 @@ modules: - { descr: motionDetected, graph: 0, value: 1, generic: 2 } - oid: otherStateSensorTable - value: otherStateSensorValue + value: otherStateSensorErrorStatus num_oid: .1.3.6.1.4.1.5528.100.4.2.10.1.3. descr: otherStateSensorLabel index: '{{ $index }}'