From f44df8b1fe17da6637ef4e731f1213a5dbc3ccf9 Mon Sep 17 00:00:00 2001 From: Antoine Van Elstraete Date: Tue, 9 Jun 2026 22:48:27 +0200 Subject: [PATCH] Fix memory_used OID parsing for Mikrotik devices Handle OctetString values returned as bytes by converting them to int using little-endian byte order (4 bytes) for Mikrotik devices. --- snmp2mqtt.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/snmp2mqtt.py b/snmp2mqtt.py index 2bc6fdd..12d2f7d 100755 --- a/snmp2mqtt.py +++ b/snmp2mqtt.py @@ -278,10 +278,29 @@ async def get_snmp(req): continue else: for varBind in varBinds: - logging.debug(f"{req['device_name']} {oid['name']} => {oid['type'](varBind[1])}") + raw_value = varBind[1] + + # Handle different SNMP value types + if oid['type'] == int and hasattr(raw_value, '__bytes__'): + # Convert OctetString/bytes to int + # For Mikrotik devices, memory values might be in the first 4 bytes + raw_bytes = bytes(raw_value) + # Try to interpret as integer, handling different byte orders + try: + if len(raw_bytes) >= 4: + # Try little-endian first (common for Mikrotik) + value = int.from_bytes(raw_bytes[:4], byteorder='little', signed=False) + else: + value = int.from_bytes(raw_bytes, byteorder='big', signed=False) + except (ValueError, TypeError): + value = raw_value + else: + value = raw_value + + logging.debug(f"{req['device_name']} {oid['name']} => {oid['type'](value)}") # Cast to the right type - value = oid['type'](varBind[1]) + value = oid['type'](value) # Apply operation if defined if 'operation' in oid: