Compare commits
	
		
			2 Commits
		
	
	
		
			e4e79a34a9
			...
			fix-snmp-e
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						
						
							
						
						5b670c4708
	
				 | 
					
					
						|||
| 
						
						
							
						
						85a14f4fa0
	
				 | 
					
					
						
@@ -2,7 +2,8 @@
 | 
				
			|||||||
# Install with: pip install -r requirements.txt
 | 
					# Install with: pip install -r requirements.txt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# SNMP library for asynchronous SNMP operations
 | 
					# SNMP library for asynchronous SNMP operations
 | 
				
			||||||
pysnmp>=6.0.0
 | 
					# Note: pysnmp 7.x uses a new API structure (no more Slim class)
 | 
				
			||||||
 | 
					pysnmp>=7.0.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# MQTT client library for connecting to MQTT brokers
 | 
					# MQTT client library for connecting to MQTT brokers
 | 
				
			||||||
paho-mqtt>=1.6.0
 | 
					paho-mqtt>=1.6.0
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										43
									
								
								snmp2mqtt.py
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								snmp2mqtt.py
									
									
									
									
									
								
							@@ -1,7 +1,9 @@
 | 
				
			|||||||
#!/bin/env python3
 | 
					#!/bin/env python3
 | 
				
			||||||
import asyncio
 | 
					import asyncio
 | 
				
			||||||
from pysnmp.hlapi.asyncio.slim import Slim
 | 
					from pysnmp.hlapi.asyncio import (
 | 
				
			||||||
from pysnmp.smi.rfc1902 import ObjectIdentity, ObjectType
 | 
					    get_cmd, CommunityData, UdpTransportTarget, ContextData, 
 | 
				
			||||||
 | 
					    SnmpEngine, ObjectIdentity, ObjectType
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
import random
 | 
					import random
 | 
				
			||||||
from paho.mqtt import client as mqtt_client
 | 
					from paho.mqtt import client as mqtt_client
 | 
				
			||||||
@@ -204,25 +206,34 @@ def publish(topic, client, data, retain, qos):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async def get_snmp(req):
 | 
					async def get_snmp(req):
 | 
				
			||||||
 | 
					    """Asynchronously retrieve SNMP data from device using new pysnmp API"""
 | 
				
			||||||
    data = {}
 | 
					    data = {}
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Create SNMP engine and transport target
 | 
				
			||||||
 | 
					    snmpEngine = SnmpEngine()
 | 
				
			||||||
 | 
					    authData = CommunityData(req["snmp_community"])
 | 
				
			||||||
 | 
					    transportTarget = await UdpTransportTarget.create((req["ip"], 161))
 | 
				
			||||||
 | 
					    contextData = ContextData()
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
    for oid in req["oids"]:
 | 
					    for oid in req["oids"]:
 | 
				
			||||||
        with Slim(1) as slim:
 | 
					        try:
 | 
				
			||||||
            errorIndication, errorStatus, errorIndex, varBinds = await slim.get(
 | 
					            # Perform async SNMP GET operation
 | 
				
			||||||
                req["snmp_community"],
 | 
					            errorIndication, errorStatus, errorIndex, varBinds = await get_cmd(
 | 
				
			||||||
                req["ip"],
 | 
					                snmpEngine,
 | 
				
			||||||
                161,
 | 
					                authData,
 | 
				
			||||||
                ObjectType(ObjectIdentity(oid["oid"])),
 | 
					                transportTarget,
 | 
				
			||||||
 | 
					                contextData,
 | 
				
			||||||
 | 
					                ObjectType(ObjectIdentity(oid["oid"]))
 | 
				
			||||||
            )
 | 
					            )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if errorIndication:
 | 
					            if errorIndication:
 | 
				
			||||||
                logging.error(errorIndication)
 | 
					                logging.error(f"{req['device_name']} SNMP error indication: {errorIndication}")
 | 
				
			||||||
 | 
					                continue
 | 
				
			||||||
            elif errorStatus:
 | 
					            elif errorStatus:
 | 
				
			||||||
                logging.error(
 | 
					                logging.error(
 | 
				
			||||||
                    "{} at {}".format(
 | 
					                    f"{req['device_name']} SNMP error status: {errorStatus.prettyPrint()} at {errorIndex and varBinds[int(errorIndex) - 1][0] or '?'}"
 | 
				
			||||||
                        errorStatus.prettyPrint(),
 | 
					 | 
				
			||||||
                        errorIndex and varBinds[int(errorIndex) - 1][0] or "?",
 | 
					 | 
				
			||||||
                    )
 | 
					 | 
				
			||||||
                )
 | 
					                )
 | 
				
			||||||
 | 
					                continue
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                for varBind in varBinds:
 | 
					                for varBind in varBinds:
 | 
				
			||||||
                    logging.debug(f"{req['device_name']} {oid['name']} => {oid['type'](varBind[1])}")
 | 
					                    logging.debug(f"{req['device_name']} {oid['name']} => {oid['type'](varBind[1])}")
 | 
				
			||||||
@@ -233,7 +244,11 @@ async def get_snmp(req):
 | 
				
			|||||||
                            data.update({oid["name"]: "OFF"})
 | 
					                            data.update({oid["name"]: "OFF"})
 | 
				
			||||||
                    else:
 | 
					                    else:
 | 
				
			||||||
                        data.update({oid["name"]: oid["type"](varBind[1])})
 | 
					                        data.update({oid["name"]: oid["type"](varBind[1])})
 | 
				
			||||||
    logging.debug(f"JSON : {json.dumps(data)}")
 | 
					        except Exception as e:
 | 
				
			||||||
 | 
					            logging.error(f"{req['device_name']} Exception getting OID {oid['oid']}: {e}")
 | 
				
			||||||
 | 
					            continue
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    logging.debug(f"{req['device_name']} JSON : {json.dumps(data)}")
 | 
				
			||||||
    return data
 | 
					    return data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user