snmp2mqtt/snmp2mqtt.py
2025-03-16 20:43:48 +01:00

48 lines
1.3 KiB
Python

#!/bin/env python3
import asyncio
from pysnmp.hlapi.asyncio.slim import Slim
from pysnmp.smi.rfc1902 import ObjectIdentity, ObjectType
import logging
logging.basicConfig(
format='(%(levelname)s) %(message)s',
level=logging.DEBUG
)
async def run(req):
for oid in req["oids"]:
with Slim(1) as slim:
errorIndication, errorStatus, errorIndex, varBinds = await slim.get(
req["snmp_community"],
req["ip"],
161,
ObjectType(ObjectIdentity(req["oids"][oid])),
)
if errorIndication:
logging.error(errorIndication)
elif errorStatus:
logging.error(
"{} at {}".format(
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
)
)
else:
for varBind in varBinds:
logging.debug(f"SNMP/{req['mqtt_topic']}/{oid} => {varBind[1]}")
req = {
"mqtt_topic": "mikrotik_hex",
"ip": "192.168.10.2",
"snmp_community": "public",
"oids": {
"stln_In": "1.3.6.1.2.1.2.2.1.10.12",
"stln_Out": "1.3.6.1.2.1.2.2.1.16.12"
}
}
asyncio.run(run(req))