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.
This commit is contained in:
23
snmp2mqtt.py
23
snmp2mqtt.py
@@ -278,10 +278,29 @@ async def get_snmp(req):
|
|||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
for varBind in varBinds:
|
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
|
# Cast to the right type
|
||||||
value = oid['type'](varBind[1])
|
value = oid['type'](value)
|
||||||
|
|
||||||
# Apply operation if defined
|
# Apply operation if defined
|
||||||
if 'operation' in oid:
|
if 'operation' in oid:
|
||||||
|
|||||||
Reference in New Issue
Block a user