You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python3.9 transportDispatcher cannot receive the alarm information sent from snmptrap V3 and does not report an error, but it can receive the alarm information from snmpinform
#428
Open
nishaoshan opened this issue
Oct 19, 2022
· 3 comments
Python3.9 pysnmp4.4.12
transportDispatcher cannot receive the alarm information sent from snmptrap and does not report an error, but it can receive the alarm information from snmpinform
#!/usr/bin/env python# -*- coding: utf-8 -*-importjsonimporttimeimportsysimportloggingfrompysnmp.debugimportDebug, setLoggerfrompysnmp.entityimportengine, configfrompysnmp.carrier.asyncore.dgramimportudp, udp6frompysnmp.entity.rfc3413importntfrcvfrom .trap_assetimportget_device_id, add_assetfrom .trap_configimportload_configfrom .trap_saveimporttrap_insertfromcommon.call_extimportis_device_online,is_audit_productfromcommon.sg_alarmimportALARM_T_CPU, ALARM_T_MEM, ALARM_T_DISK, ALARM_T_CPU_TEMP, ALARM_T_FAN_SPEED, \
ALARM_T_ABNORMAL_EV,ALARM_T_START_EV, ALARM_T_NAT_EV, ALARM_T_INF_EV, ALARM_T_LOG_ROLLBACK, \
ALARM_T_CONNECTION_STATE, ALARM_ASSET_ALARM, ALARM_T_FALL_LOST, ALARM_T_CONCURRENCYALARM_CLEAR=32classTrapConfig(object):
status=1listen_port=162support_v2=1community='public'support_v3=1user_name=''auth_proto='MD5'auth_key=''priv_proto='DES'priv_key=''g_trapConf=TrapConfig()
debug_log=logging.getLogger(__name__)
# Callback function for receiving notificationsdefcbFun(snmpEngine, stateReference, contextEngineId, contextName,
varBinds, cbCtx):
log=cbCtxsqlDict= { "log_time":0,
"log_module":'trap',
"log_level":0,
"log_type":0,
"log_from":None,
"log_flag":0,
"cn_msg":None,
"en_msg":None
}
# Get an execution context...execContext=snmpEngine.observer.getExecutionContext(
'rfc3412.receiveMessage:request'
)
sqlDict["log_time"] =int(time.time())
sqlDict["log_from"] =execContext['transportAddress'][0]
ALARM_R='解除'asset=None###foroid, valinvarBinds:
val=str(val).encode('iso-8859-1').decode('utf-8')
log.debug('oid:%r, val:%s', oid, val)
ifoid.prettyPrint() =="1.3.6.1.4.1.32328.6.1.4":
ifnotis_audit_product() andnotis_device_online(str(val)):
log.warn('The device [%s][%s] is not online.', sqlDict["log_from"], str(val))
returnsqlDict["log_from"] =val# cpuPercentUsageelifoid.prettyPrint() =="1.3.6.1.4.1.32328.6.1.10.1.2":
sqlDict["en_msg"] =valsqlDict["cn_msg"] =valsqlDict["log_type"] =ALARM_T_CPUifALARM_Rinval:
sqlDict['log_type'] =ALARM_T_CPU+ALARM_CLEAR# memoryPercentUsageelifoid.prettyPrint() =="1.3.6.1.4.1.32328.6.1.10.3.2":
sqlDict["en_msg"] =valsqlDict["cn_msg"] =valsqlDict["log_type"] =ALARM_T_MEMifALARM_Rinval:
sqlDict['log_type'] =ALARM_T_MEM+ALARM_CLEAR# CF-diskPercentUsage/harddiskPercentUsageelifoid.prettyPrint() in ("1.3.6.1.4.1.32328.6.1.10.2.2", "1.3.6.1.4.1.32328.6.1.10.2.4"):
sqlDict["en_msg"] =valsqlDict["cn_msg"] =valsqlDict["log_type"] =ALARM_T_DISKifALARM_Rinval:
sqlDict['log_type'] =ALARM_T_DISK+ALARM_CLEAR# cpuTempelifoid.prettyPrint() =="1.3.6.1.4.1.32328.6.1.10.1.3.1.3":
sqlDict["en_msg"] =valsqlDict["cn_msg"] =valsqlDict["log_type"] =ALARM_T_CPU_TEMPifALARM_Rinval:
sqlDict['log_type'] =ALARM_T_CPU_TEMP+ALARM_CLEAR# FanSpeedelifoid.prettyPrint() =="1.3.6.1.4.1.32328.6.1.10.5.1.1.3":
sqlDict["en_msg"] =valsqlDict["cn_msg"] =valsqlDict["log_type"] =ALARM_T_FAN_SPEEDifALARM_Rinval:
sqlDict['log_type'] =ALARM_T_FAN_SPEED+ALARM_CLEAR# attack Eventelifoid.prettyPrint() =="1.3.6.1.4.1.32328.6.1.11.2":
sqlDict["en_msg"] =valsqlDict["cn_msg"] =valsqlDict["log_type"] =ALARM_T_ABNORMAL_EV# startEventelifoid.prettyPrint() =="1.3.6.1.4.1.32328.6.1.11.5":
sqlDict["en_msg"] =valsqlDict["cn_msg"] =valsqlDict["log_type"] =ALARM_T_START_EV# natThresholdEventelifoid.prettyPrint() =="1.3.6.1.4.1.32328.6.1.11.6":
sqlDict["en_msg"] =valsqlDict["cn_msg"] =valsqlDict["log_type"] =ALARM_T_NAT_EVifALARM_Rinval:
sqlDict['log_type'] =ALARM_T_NAT_EV+ALARM_CLEAR# interfaceselifoid.prettyPrint() in ("1.3.6.1.4.1.32328.6.2.3.1.1.7", "1.3.6.1.4.1.32328.6.2.3.1.1.8"):
sqlDict["en_msg"] =valsqlDict["cn_msg"] =valsqlDict["log_type"] =ALARM_T_INF_EVifALARM_Rinval:
sqlDict['log_type'] =ALARM_T_INF_EV+ALARM_CLEARelifoid.prettyPrint() =="1.3.6.1.4.1.32328.6.1.11.7":
sqlDict["en_msg"] =valsqlDict["cn_msg"] =valsqlDict["log_type"] =ALARM_T_CONNECTION_STATEelifoid.prettyPrint() =="1.3.6.1.4.1.32328.6.1.11.8":
sqlDict["en_msg"] =valsqlDict["cn_msg"] =valsqlDict["log_type"] =ALARM_T_LOG_ROLLBACKelifoid.prettyPrint() =="1.3.6.1.4.1.32328.6.1.11.9":
data=json.loads(str(val))
ifdataanddata["property_terminal"] andlen(data["property_terminal"]) >0:
sqlDict["log_type"] =ALARM_ASSET_ALARMasset=data["property_terminal"][0]
log.info("asset %s", asset)
ifasset["action"] =='pending':
sqlDict["en_msg"] ="There is a new pending terminal to access network, please audit it at once."sqlDict["cn_msg"] ="您有1个新待审批设备接入网络,请审核是否准入网络。"elifasset["action"] =='abnormal':
sqlDict["en_msg"] ="There is a new abnormal terminal to access network, please handle it at once."sqlDict["cn_msg"] ="您有1个新异常仿冒设备接入网络,请审核是否准入网络。"else:
log.error("unknown type asset: %s", asset)
elifoid.prettyPrint() =="1.3.6.1.4.1.32328.6.1.11.10":
sqlDict["en_msg"] =valsqlDict["cn_msg"] =valsqlDict["log_type"] =ALARM_T_CONCURRENCYelifoid.prettyPrint() =="1.3.6.1.4.1.32328.6.1.11.11":
sqlDict["en_msg"] =valsqlDict["cn_msg"] =valsqlDict["log_type"] =ALARM_T_FALL_LOSTifsqlDict["log_type"] ==ALARM_ASSET_ALARM:
ifsqlDict["log_from"] andasset:
ne_id=get_device_id(str(sqlDict["log_from"]))
add_asset(ne_id, asset)
else:
log.error("trap does not contain device sn: %s", varBinds)
returnifsqlDict["cn_msg"] isNoneorsqlDict["en_msg"] isNone:
returnlog.debug('ALARM:%s', sqlDict)
trap_insert(sqlDict, logger=log)
# AuthProtocolConvertdefAuthProtocolConvert(proto):
auth_proto=config.usmNoAuthProtocolifproto=="MD5":
auth_proto=config.usmHMACMD5AuthProtocolelifproto=="SHA1":
auth_proto=config.usmHMACSHAAuthProtocolreturnauth_proto# PrivProtocolConvertdefPrivProtocolConvert(proto):
priv_proto=config.usmNoPrivProtocolifproto=="DES":
priv_proto=config.usmDESPrivProtocolelifproto=="AES-128":
priv_proto=config.usmAesCfb128Protocolreturnpriv_proto# ################ main function ################defmain_loop(logger=None):
iflogger:
globaldebug_logdebug_log=logger# load trap configload_config(g_trapConf, debug_log)
# local variablesauth_proto=AuthProtocolConvert(g_trapConf.auth_proto)
priv_proto=PrivProtocolConvert(g_trapConf.priv_proto)
# global enableifnotg_trapConf.status:
debug_log.info('trap status disable, exit')
return# debug# setLogger(Debug('msgproc', 'secmod', 'app', 'dsp', 'io'))# Create SNMP engine with auto-generated engineID and pre-bound# to socket transport dispatchersnmpEngine=engine.SnmpEngine()
# Transport setup# UDP over IPv4domain_name=udp6.domainNametransport=udp6.Udp6Transport().openServerMode(('::', g_trapConf.listen_port))
config.addTransport(snmpEngine, domain_name, transport)
# SNMPv2 setupifg_trapConf.support_v2:
config.addV1System(snmpEngine, 'my-area', g_trapConf.community)
# SNMPv3/USM setupifg_trapConf.support_v3:
config.addV3User(
snmpEngine, g_trapConf.user_name,
auth_proto, g_trapConf.auth_key,
priv_proto, g_trapConf.priv_key
)
# Register SNMP Application at the SNMP enginentfrcv.NotificationReceiver(snmpEngine, cbFun, cbCtx=debug_log)
snmpEngine.transportDispatcher.jobStarted(1) # this job would never finish# Run I/O dispatcher which would receive queries and send confirmationstry:
snmpEngine.transportDispatcher.runDispatcher()
exceptExceptionase:
snmpEngine.transportDispatcher.closeDispatcher()
debug_log.error(e)
The text was updated successfully, but these errors were encountered:
How can I receive the alarm information sent by snmptrap v3, not only snmpinform, because the sender uses snmptrap instead of snmpinform。But there is no such problem in python 2。In addition, there is no problem with v1 and v2 in Python 3.9。
If python3 wants to use snmptrap to receive messages, the trap receiver may need to add a securityEngineId. If you do not add a securityEngineId, you cannot obtain snmptrap. Only snmpinform can be obtained
nishaoshan
changed the title
Python3.9 transportDispatcher cannot receive the alarm information sent from snmptrap and does not report an error, but it can receive the alarm information from snmpinform
Python3.9 transportDispatcher cannot receive the alarm information sent from snmptrap V3 and does not report an error, but it can receive the alarm information from snmpinform
Oct 19, 2022
Python3.9 pysnmp4.4.12
transportDispatcher cannot receive the alarm information sent from snmptrap and does not report an error, but it can receive the alarm information from snmpinform
The text was updated successfully, but these errors were encountered: