From a08b8333ccbd749bb904f0b60d11e41794c99900 Mon Sep 17 00:00:00 2001 From: PipoCanaja <38363551+PipoCanaja@users.noreply.github.com> Date: Thu, 18 Apr 2019 03:15:31 +0200 Subject: [PATCH] Bug - Nasty user_func vs divisor-multiplier issue (#10122) * Nasty user_func vs divisor-multiplier issue * tests --- includes/discovery/functions.inc.php | 9 +- tests/data/cyberpower.json | 562 ++------------------------- 2 files changed, 30 insertions(+), 541 deletions(-) diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index 991beb678d..f07b98d546 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -1098,10 +1098,6 @@ function discovery_process(&$valid, $device, $sensor_type, $pre_cache) $sensor_name = $device['os']; - if (isset($user_function) && function_exists($user_function)) { - $value = $user_function($value); - } - if ($sensor_type === 'state') { $sensor_name = $data['state_name'] ?: $data['oid']; create_state_index($sensor_name, $data['states']); @@ -1110,6 +1106,11 @@ function discovery_process(&$valid, $device, $sensor_type, $pre_cache) $value = ($value / $divisor) * $multiplier; } + //user_func must be applied after divisor/multiplier + if (isset($user_function) && function_exists($user_function)) { + $value = $user_function($value); + } + $uindex = str_replace('{{ $index }}', $index, isset($data['index']) ? $data['index'] : $index); discover_sensor($valid['sensor'], $sensor_type, $device, $oid, $uindex, $sensor_name, $descr, $divisor, $multiplier, $low_limit, $low_warn_limit, $warn_limit, $high_limit, $value, 'snmp', $entPhysicalIndex, $entPhysicalIndex_measured, $user_function, $group); diff --git a/tests/data/cyberpower.json b/tests/data/cyberpower.json index 6cc2052413..34ef2933fc 100644 --- a/tests/data/cyberpower.json +++ b/tests/data/cyberpower.json @@ -10,11 +10,11 @@ "version": null, "hardware": null, "features": null, - "location": null, "os": "cyberpower", "type": "power", "serial": null, - "icon": "cyberpower.svg" + "icon": "cyberpower.svg", + "location": null } ] }, @@ -28,11 +28,11 @@ "version": "S1A17", "hardware": "OL2200RTXL2U", "features": null, - "location": "", "os": "cyberpower", "type": "power", "serial": null, - "icon": "cyberpower.svg" + "icon": "cyberpower.svg", + "location": "" } ] } @@ -48,7 +48,7 @@ "sensor_index": "upsAdvanceBatteryCapacity.0", "sensor_type": "cyberpower", "sensor_descr": "Battery Capacity", - "group": null, + "group": "", "sensor_divisor": 1, "sensor_multiplier": 1, "sensor_current": 100, @@ -72,7 +72,7 @@ "sensor_index": "upsAdvanceOutputCurrent.0", "sensor_type": "cyberpower", "sensor_descr": "Output", - "group": null, + "group": "", "sensor_divisor": 1, "sensor_multiplier": 1, "sensor_current": 65, @@ -96,7 +96,7 @@ "sensor_index": "upsAdvanceInputFrequency.0", "sensor_type": "cyberpower", "sensor_descr": "Input", - "group": null, + "group": "", "sensor_divisor": 10, "sensor_multiplier": 1, "sensor_current": 60, @@ -120,7 +120,7 @@ "sensor_index": "upsAdvanceOutputFrequency.0", "sensor_type": "cyberpower", "sensor_descr": "Output", - "group": null, + "group": "", "sensor_divisor": 10, "sensor_multiplier": 1, "sensor_current": 60, @@ -144,7 +144,7 @@ "sensor_index": "envirHumidity.0", "sensor_type": "cyberpower", "sensor_descr": "Environment Sensor Humidity", - "group": null, + "group": "", "sensor_divisor": 1, "sensor_multiplier": 1, "sensor_current": 56, @@ -168,7 +168,7 @@ "sensor_index": "upsAdvanceBatteryRunTimeRemaining.0", "sensor_type": "cyberpower", "sensor_descr": "Battery Runtime", - "group": null, + "group": "", "sensor_divisor": 6000, "sensor_multiplier": 1, "sensor_current": 29, @@ -192,7 +192,7 @@ "sensor_index": "upsAdvanceBatteryReplaceIndicator.0", "sensor_type": "upsAdvanceBatteryReplaceIndicator", "sensor_descr": "Battery Replace Indicator", - "group": null, + "group": "", "sensor_divisor": 1, "sensor_multiplier": 1, "sensor_current": 1, @@ -216,7 +216,7 @@ "sensor_index": "upsAdvanceInputLineFailCause.0", "sensor_type": "upsAdvanceInputLineFailCause", "sensor_descr": "Input Line Cause", - "group": null, + "group": "", "sensor_divisor": 1, "sensor_multiplier": 1, "sensor_current": 1, @@ -240,7 +240,7 @@ "sensor_index": "upsAdvanceInputStatus.0", "sensor_type": "upsAdvanceInputStatus", "sensor_descr": "Input Status", - "group": null, + "group": "", "sensor_divisor": 1, "sensor_multiplier": 1, "sensor_current": 1, @@ -264,7 +264,7 @@ "sensor_index": "upsBaseBatteryStatus.0", "sensor_type": "upsBaseBatteryStatus", "sensor_descr": "Battery Status", - "group": null, + "group": "", "sensor_divisor": 1, "sensor_multiplier": 1, "sensor_current": 2, @@ -288,7 +288,7 @@ "sensor_index": "upsBaseOutputStatus.0", "sensor_type": "upsBaseOutputStatus", "sensor_descr": "Output Status", - "group": null, + "group": "", "sensor_divisor": 1, "sensor_multiplier": 1, "sensor_current": 2, @@ -312,13 +312,13 @@ "sensor_index": "envirTemperature.0", "sensor_type": "cyberpower", "sensor_descr": "Environment Sensor Temperature", - "group": null, + "group": "", "sensor_divisor": 10, "sensor_multiplier": 1, - "sensor_current": 36.167, - "sensor_limit": 56.167, + "sensor_current": 20.170000000000002, + "sensor_limit": 40.170000000000002, "sensor_limit_warn": null, - "sensor_limit_low": 26.167, + "sensor_limit_low": 10.17, "sensor_limit_low_warn": null, "sensor_alert": 1, "sensor_custom": "No", @@ -336,7 +336,7 @@ "sensor_index": "upsAdvanceBatteryTemperature.0", "sensor_type": "cyberpower", "sensor_descr": "Battery Temperature", - "group": null, + "group": "", "sensor_divisor": 1, "sensor_multiplier": 1, "sensor_current": 36, @@ -360,11 +360,11 @@ "sensor_index": "upsAdvanceInputLineVoltage.0", "sensor_type": "cyberpower", "sensor_descr": "Input Line Voltage", - "group": null, + "group": "", "sensor_divisor": 10, "sensor_multiplier": 1, "sensor_current": 119.5, - "sensor_limit": 137.425, + "sensor_limit": 137.42500000000001, "sensor_limit_warn": null, "sensor_limit_low": 101.575, "sensor_limit_low_warn": null, @@ -384,11 +384,11 @@ "sensor_index": "upsAdvanceOutputVoltage.0", "sensor_type": "cyberpower", "sensor_descr": "Output Voltage", - "group": null, + "group": "", "sensor_divisor": 10, "sensor_multiplier": 1, "sensor_current": 120.2, - "sensor_limit": 138.23, + "sensor_limit": 138.22999999999999, "sensor_limit_warn": null, "sensor_limit_low": 102.17, "sensor_limit_low_warn": null, @@ -551,518 +551,6 @@ } ] }, - "poller": { - "sensors": [ - { - "sensor_deleted": 0, - "sensor_class": "charge", - "poller_type": "snmp", - "sensor_oid": ".1.3.6.1.4.1.3808.1.1.1.2.2.1.0", - "sensor_index": "upsAdvanceBatteryCapacity.0", - "sensor_type": "cyberpower", - "sensor_descr": "Battery Capacity", - "group": null, - "sensor_divisor": 1, - "sensor_multiplier": 1, - "sensor_current": 100, - "sensor_limit": null, - "sensor_limit_warn": null, - "sensor_limit_low": null, - "sensor_limit_low_warn": null, - "sensor_alert": 1, - "sensor_custom": "No", - "entPhysicalIndex": null, - "entPhysicalIndex_measured": null, - "sensor_prev": null, - "user_func": null, - "state_name": null - }, - { - "sensor_deleted": 0, - "sensor_class": "current", - "poller_type": "snmp", - "sensor_oid": ".1.3.6.1.4.1.3808.1.1.1.4.2.4.0", - "sensor_index": "upsAdvanceOutputCurrent.0", - "sensor_type": "cyberpower", - "sensor_descr": "Output", - "group": null, - "sensor_divisor": 1, - "sensor_multiplier": 1, - "sensor_current": 65, - "sensor_limit": 97.5, - "sensor_limit_warn": null, - "sensor_limit_low": null, - "sensor_limit_low_warn": null, - "sensor_alert": 1, - "sensor_custom": "No", - "entPhysicalIndex": null, - "entPhysicalIndex_measured": null, - "sensor_prev": null, - "user_func": null, - "state_name": null - }, - { - "sensor_deleted": 0, - "sensor_class": "frequency", - "poller_type": "snmp", - "sensor_oid": ".1.3.6.1.4.1.3808.1.1.1.3.2.4.0", - "sensor_index": "upsAdvanceInputFrequency.0", - "sensor_type": "cyberpower", - "sensor_descr": "Input", - "group": null, - "sensor_divisor": 10, - "sensor_multiplier": 1, - "sensor_current": 60, - "sensor_limit": 63, - "sensor_limit_warn": null, - "sensor_limit_low": 57, - "sensor_limit_low_warn": null, - "sensor_alert": 1, - "sensor_custom": "No", - "entPhysicalIndex": null, - "entPhysicalIndex_measured": null, - "sensor_prev": null, - "user_func": null, - "state_name": null - }, - { - "sensor_deleted": 0, - "sensor_class": "frequency", - "poller_type": "snmp", - "sensor_oid": ".1.3.6.1.4.1.3808.1.1.1.4.2.2.0", - "sensor_index": "upsAdvanceOutputFrequency.0", - "sensor_type": "cyberpower", - "sensor_descr": "Output", - "group": null, - "sensor_divisor": 10, - "sensor_multiplier": 1, - "sensor_current": 60, - "sensor_limit": 63, - "sensor_limit_warn": null, - "sensor_limit_low": 57, - "sensor_limit_low_warn": null, - "sensor_alert": 1, - "sensor_custom": "No", - "entPhysicalIndex": null, - "entPhysicalIndex_measured": null, - "sensor_prev": null, - "user_func": null, - "state_name": null - }, - { - "sensor_deleted": 0, - "sensor_class": "humidity", - "poller_type": "snmp", - "sensor_oid": ".1.3.6.1.4.1.3808.1.1.4.3.1.0", - "sensor_index": "envirHumidity.0", - "sensor_type": "cyberpower", - "sensor_descr": "Environment Sensor Humidity", - "group": null, - "sensor_divisor": 1, - "sensor_multiplier": 1, - "sensor_current": 56, - "sensor_limit": 70, - "sensor_limit_warn": null, - "sensor_limit_low": 30, - "sensor_limit_low_warn": null, - "sensor_alert": 1, - "sensor_custom": "No", - "entPhysicalIndex": null, - "entPhysicalIndex_measured": null, - "sensor_prev": null, - "user_func": null, - "state_name": null - }, - { - "sensor_deleted": 0, - "sensor_class": "runtime", - "poller_type": "snmp", - "sensor_oid": ".1.3.6.1.4.1.3808.1.1.1.2.2.4.0", - "sensor_index": "upsAdvanceBatteryRunTimeRemaining.0", - "sensor_type": "cyberpower", - "sensor_descr": "Battery Runtime", - "group": null, - "sensor_divisor": 6000, - "sensor_multiplier": 1, - "sensor_current": 29, - "sensor_limit": null, - "sensor_limit_warn": null, - "sensor_limit_low": null, - "sensor_limit_low_warn": null, - "sensor_alert": 1, - "sensor_custom": "No", - "entPhysicalIndex": null, - "entPhysicalIndex_measured": null, - "sensor_prev": null, - "user_func": null, - "state_name": null - }, - { - "sensor_deleted": 0, - "sensor_class": "state", - "poller_type": "snmp", - "sensor_oid": ".1.3.6.1.4.1.3808.1.1.1.2.2.5.0", - "sensor_index": "upsAdvanceBatteryReplaceIndicator.0", - "sensor_type": "upsAdvanceBatteryReplaceIndicator", - "sensor_descr": "Battery Replace Indicator", - "group": null, - "sensor_divisor": 1, - "sensor_multiplier": 1, - "sensor_current": 1, - "sensor_limit": null, - "sensor_limit_warn": null, - "sensor_limit_low": null, - "sensor_limit_low_warn": null, - "sensor_alert": 1, - "sensor_custom": "No", - "entPhysicalIndex": null, - "entPhysicalIndex_measured": null, - "sensor_prev": null, - "user_func": null, - "state_name": "upsAdvanceBatteryReplaceIndicator" - }, - { - "sensor_deleted": 0, - "sensor_class": "state", - "poller_type": "snmp", - "sensor_oid": ".1.3.6.1.4.1.3808.1.1.1.3.2.5.0", - "sensor_index": "upsAdvanceInputLineFailCause.0", - "sensor_type": "upsAdvanceInputLineFailCause", - "sensor_descr": "Input Line Cause", - "group": null, - "sensor_divisor": 1, - "sensor_multiplier": 1, - "sensor_current": 1, - "sensor_limit": null, - "sensor_limit_warn": null, - "sensor_limit_low": null, - "sensor_limit_low_warn": null, - "sensor_alert": 1, - "sensor_custom": "No", - "entPhysicalIndex": null, - "entPhysicalIndex_measured": null, - "sensor_prev": null, - "user_func": null, - "state_name": "upsAdvanceInputLineFailCause" - }, - { - "sensor_deleted": 0, - "sensor_class": "state", - "poller_type": "snmp", - "sensor_oid": ".1.3.6.1.4.1.3808.1.1.1.3.2.6.0", - "sensor_index": "upsAdvanceInputStatus.0", - "sensor_type": "upsAdvanceInputStatus", - "sensor_descr": "Input Status", - "group": null, - "sensor_divisor": 1, - "sensor_multiplier": 1, - "sensor_current": 1, - "sensor_limit": null, - "sensor_limit_warn": null, - "sensor_limit_low": null, - "sensor_limit_low_warn": null, - "sensor_alert": 1, - "sensor_custom": "No", - "entPhysicalIndex": null, - "entPhysicalIndex_measured": null, - "sensor_prev": null, - "user_func": null, - "state_name": "upsAdvanceInputStatus" - }, - { - "sensor_deleted": 0, - "sensor_class": "state", - "poller_type": "snmp", - "sensor_oid": ".1.3.6.1.4.1.3808.1.1.1.2.1.1.0", - "sensor_index": "upsBaseBatteryStatus.0", - "sensor_type": "upsBaseBatteryStatus", - "sensor_descr": "Battery Status", - "group": null, - "sensor_divisor": 1, - "sensor_multiplier": 1, - "sensor_current": 2, - "sensor_limit": null, - "sensor_limit_warn": null, - "sensor_limit_low": null, - "sensor_limit_low_warn": null, - "sensor_alert": 1, - "sensor_custom": "No", - "entPhysicalIndex": null, - "entPhysicalIndex_measured": null, - "sensor_prev": null, - "user_func": null, - "state_name": "upsBaseBatteryStatus" - }, - { - "sensor_deleted": 0, - "sensor_class": "state", - "poller_type": "snmp", - "sensor_oid": ".1.3.6.1.4.1.3808.1.1.1.4.1.1.0", - "sensor_index": "upsBaseOutputStatus.0", - "sensor_type": "upsBaseOutputStatus", - "sensor_descr": "Output Status", - "group": null, - "sensor_divisor": 1, - "sensor_multiplier": 1, - "sensor_current": 2, - "sensor_limit": null, - "sensor_limit_warn": null, - "sensor_limit_low": null, - "sensor_limit_low_warn": null, - "sensor_alert": 1, - "sensor_custom": "No", - "entPhysicalIndex": null, - "entPhysicalIndex_measured": null, - "sensor_prev": null, - "user_func": null, - "state_name": "upsBaseOutputStatus" - }, - { - "sensor_deleted": 0, - "sensor_class": "temperature", - "poller_type": "snmp", - "sensor_oid": ".1.3.6.1.4.1.3808.1.1.4.2.1.0", - "sensor_index": "envirTemperature.0", - "sensor_type": "cyberpower", - "sensor_descr": "Environment Sensor Temperature", - "group": null, - "sensor_divisor": 10, - "sensor_multiplier": 1, - "sensor_current": 20.17, - "sensor_limit": 56.167, - "sensor_limit_warn": null, - "sensor_limit_low": 26.167, - "sensor_limit_low_warn": null, - "sensor_alert": 1, - "sensor_custom": "No", - "entPhysicalIndex": null, - "entPhysicalIndex_measured": null, - "sensor_prev": 36.167, - "user_func": "fahrenheit_to_celsius", - "state_name": null - }, - { - "sensor_deleted": 0, - "sensor_class": "temperature", - "poller_type": "snmp", - "sensor_oid": ".1.3.6.1.4.1.3808.1.1.1.2.2.3.0", - "sensor_index": "upsAdvanceBatteryTemperature.0", - "sensor_type": "cyberpower", - "sensor_descr": "Battery Temperature", - "group": null, - "sensor_divisor": 1, - "sensor_multiplier": 1, - "sensor_current": 36, - "sensor_limit": 56, - "sensor_limit_warn": null, - "sensor_limit_low": 26, - "sensor_limit_low_warn": null, - "sensor_alert": 1, - "sensor_custom": "No", - "entPhysicalIndex": null, - "entPhysicalIndex_measured": null, - "sensor_prev": null, - "user_func": null, - "state_name": null - }, - { - "sensor_deleted": 0, - "sensor_class": "voltage", - "poller_type": "snmp", - "sensor_oid": ".1.3.6.1.4.1.3808.1.1.1.3.2.1.0", - "sensor_index": "upsAdvanceInputLineVoltage.0", - "sensor_type": "cyberpower", - "sensor_descr": "Input Line Voltage", - "group": null, - "sensor_divisor": 10, - "sensor_multiplier": 1, - "sensor_current": 119.5, - "sensor_limit": 137.425, - "sensor_limit_warn": null, - "sensor_limit_low": 101.575, - "sensor_limit_low_warn": null, - "sensor_alert": 1, - "sensor_custom": "No", - "entPhysicalIndex": null, - "entPhysicalIndex_measured": null, - "sensor_prev": null, - "user_func": null, - "state_name": null - }, - { - "sensor_deleted": 0, - "sensor_class": "voltage", - "poller_type": "snmp", - "sensor_oid": ".1.3.6.1.4.1.3808.1.1.1.4.2.1.0", - "sensor_index": "upsAdvanceOutputVoltage.0", - "sensor_type": "cyberpower", - "sensor_descr": "Output Voltage", - "group": null, - "sensor_divisor": 10, - "sensor_multiplier": 1, - "sensor_current": 120.2, - "sensor_limit": 138.23, - "sensor_limit_warn": null, - "sensor_limit_low": 102.17, - "sensor_limit_low_warn": null, - "sensor_alert": 1, - "sensor_custom": "No", - "entPhysicalIndex": null, - "entPhysicalIndex_measured": null, - "sensor_prev": null, - "user_func": null, - "state_name": null - } - ], - "state_indexes": [ - { - "state_name": "upsAdvanceBatteryReplaceIndicator", - "state_descr": "No", - "state_draw_graph": 1, - "state_value": 1, - "state_generic_value": 0 - }, - { - "state_name": "upsAdvanceBatteryReplaceIndicator", - "state_descr": "Replace", - "state_draw_graph": 1, - "state_value": 2, - "state_generic_value": 2 - }, - { - "state_name": "upsAdvanceInputLineFailCause", - "state_descr": "No Transfer", - "state_draw_graph": 1, - "state_value": 1, - "state_generic_value": 0 - }, - { - "state_name": "upsAdvanceInputLineFailCause", - "state_descr": "High Voltage", - "state_draw_graph": 1, - "state_value": 2, - "state_generic_value": 2 - }, - { - "state_name": "upsAdvanceInputLineFailCause", - "state_descr": "Brown Out", - "state_draw_graph": 1, - "state_value": 3, - "state_generic_value": 2 - }, - { - "state_name": "upsAdvanceInputLineFailCause", - "state_descr": "Self Test", - "state_draw_graph": 1, - "state_value": 4, - "state_generic_value": 1 - }, - { - "state_name": "upsAdvanceInputStatus", - "state_descr": "Normal", - "state_draw_graph": 1, - "state_value": 1, - "state_generic_value": 0 - }, - { - "state_name": "upsAdvanceInputStatus", - "state_descr": "Over Voltage", - "state_draw_graph": 1, - "state_value": 2, - "state_generic_value": 2 - }, - { - "state_name": "upsAdvanceInputStatus", - "state_descr": "Under Voltage", - "state_draw_graph": 1, - "state_value": 3, - "state_generic_value": 2 - }, - { - "state_name": "upsAdvanceInputStatus", - "state_descr": "Frequency Failure", - "state_draw_graph": 1, - "state_value": 4, - "state_generic_value": 2 - }, - { - "state_name": "upsAdvanceInputStatus", - "state_descr": "Blackout", - "state_draw_graph": 1, - "state_value": 5, - "state_generic_value": 2 - }, - { - "state_name": "upsBaseBatteryStatus", - "state_descr": "Known", - "state_draw_graph": 1, - "state_value": 1, - "state_generic_value": 3 - }, - { - "state_name": "upsBaseBatteryStatus", - "state_descr": "Normal", - "state_draw_graph": 1, - "state_value": 2, - "state_generic_value": 0 - }, - { - "state_name": "upsBaseBatteryStatus", - "state_descr": "Low", - "state_draw_graph": 1, - "state_value": 3, - "state_generic_value": 2 - }, - { - "state_name": "upsBaseOutputStatus", - "state_descr": "Unknown", - "state_draw_graph": 1, - "state_value": 1, - "state_generic_value": 3 - }, - { - "state_name": "upsBaseOutputStatus", - "state_descr": "Online", - "state_draw_graph": 1, - "state_value": 2, - "state_generic_value": 0 - }, - { - "state_name": "upsBaseOutputStatus", - "state_descr": "On Battery", - "state_draw_graph": 1, - "state_value": 3, - "state_generic_value": 2 - }, - { - "state_name": "upsBaseOutputStatus", - "state_descr": "On Boost", - "state_draw_graph": 1, - "state_value": 4, - "state_generic_value": 1 - }, - { - "state_name": "upsBaseOutputStatus", - "state_descr": "On Sleep", - "state_draw_graph": 1, - "state_value": 5, - "state_generic_value": 1 - }, - { - "state_name": "upsBaseOutputStatus", - "state_descr": "Off", - "state_draw_graph": 1, - "state_value": 6, - "state_generic_value": 2 - }, - { - "state_name": "upsBaseOutputStatus", - "state_descr": "Rebooting", - "state_draw_graph": 1, - "state_value": 7, - "state_generic_value": 1 - } - ] - } + "poller": "matches discovery" } }