From 68a4ebf6019bd5e4ef1a633812e8af64f636e648 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Sun, 25 Jan 2026 12:30:26 -0600 Subject: [PATCH] fix: correct misclassified MikroTik voltage sensors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes voltage sensors that were incorrectly classified as temperature sensors due to MikroTik devices reporting wrong mtxrGaugeUnit values. Problem: - Sensors with names like "psu1-voltage" and "psu2-voltage" were classified as temperature sensors with divisor=1 - This caused voltage readings to display as 469V instead of 46.9V - The RouterOS profile already has name-based override logic for new discoveries, but existing sensors needed fixing Solution: - Migration identifies sensors by: - OID pattern: 1.3.6.1.4.1.14988.1.1.3.100.1.3.% - Type mismatch: sensor_type = 'temperature' but name contains 'voltage' - Incorrect divisor: sensor_divisor = 1 - Updates sensor metadata: type, unit, divisor - Corrects historical sensor_reading values by dividing by 10 Future discoveries are already protected by the name-based override logic in lib/towerops/snmp/profiles/vendors/routeros.ex:620-630 🤖 Generated with Claude Code Co-Authored-By: Claude Sonnet 4.5 --- ...misclassified_mikrotik_voltage_sensors.exs | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 priv/repo/migrations/20260125182836_fix_misclassified_mikrotik_voltage_sensors.exs diff --git a/priv/repo/migrations/20260125182836_fix_misclassified_mikrotik_voltage_sensors.exs b/priv/repo/migrations/20260125182836_fix_misclassified_mikrotik_voltage_sensors.exs new file mode 100644 index 00000000..3ae800f4 --- /dev/null +++ b/priv/repo/migrations/20260125182836_fix_misclassified_mikrotik_voltage_sensors.exs @@ -0,0 +1,63 @@ +defmodule Towerops.Repo.Migrations.FixMisclassifiedMikrotikVoltageSensors do + use Ecto.Migration + + def up do + # Fix voltage sensors that were incorrectly classified as temperature + # These sensors have "voltage" in their name but were classified as temperature + # This happens when MikroTik devices report incorrect mtxrGaugeUnit values + + # Update sensors: set correct type, unit, and divisor + execute """ + UPDATE snmp_sensors + SET sensor_type = 'voltage', + sensor_unit = 'V', + sensor_divisor = 10 + WHERE sensor_oid LIKE '1.3.6.1.4.1.14988.1.1.3.100.1.3.%' + AND sensor_type = 'temperature' + AND LOWER(sensor_descr) LIKE '%voltage%' + AND sensor_divisor = 1 + """ + + # Divide existing sensor_reading values by 10 to correct historical data + execute """ + UPDATE snmp_sensor_readings + SET value = value / 10 + WHERE sensor_id IN ( + SELECT id + FROM snmp_sensors + WHERE sensor_oid LIKE '1.3.6.1.4.1.14988.1.1.3.100.1.3.%' + AND sensor_type = 'voltage' + AND LOWER(sensor_descr) LIKE '%voltage%' + AND sensor_divisor = 10 + ) + """ + end + + def down do + # Revert changes + execute """ + UPDATE snmp_sensors + SET sensor_type = 'temperature', + sensor_unit = '', + sensor_divisor = 1 + WHERE sensor_oid LIKE '1.3.6.1.4.1.14988.1.1.3.100.1.3.%' + AND sensor_type = 'voltage' + AND LOWER(sensor_descr) LIKE '%voltage%' + AND sensor_divisor = 10 + """ + + # Multiply sensor readings back by 10 + execute """ + UPDATE snmp_sensor_readings + SET value = value * 10 + WHERE sensor_id IN ( + SELECT id + FROM snmp_sensors + WHERE sensor_oid LIKE '1.3.6.1.4.1.14988.1.1.3.100.1.3.%' + AND sensor_type = 'temperature' + AND LOWER(sensor_descr) LIKE '%voltage%' + AND sensor_divisor = 1 + ) + """ + end +end