I have a code that receives snmp traps. When the new traps come, i will save the output in database, as row data. Traps are coming one after another. In database they are mixed. it's confusing which trap values it belongs to i cannot understand. What can i do for it, how can i seperate one trap from another in database? Here is my code
def cbFun(snmpEngine, stateReference, contextEngineId, contextName, varBinds,
cbCtx):
print('\n{0}New trap message received on {1} {0}'.format(
'-' * 20,
datetime.now().strftime('%d-%b-%Y at %H:%M:%S')))
execContext = snmpEngine.observer.getExecutionContext(
'rfc3412.receiveMessage:request')
print('Trap is coming from %s:%s' % execContext['transportAddress'])
for name, val in varBinds:
print('{0} = {1}'.format(name.prettyPrint(), val.prettyPrint()))
sql = "INSERT INTO snmp_log (name_oid, value_oid, date) VALUES (%s, %s, %s)"
now = datetime.now()
dt_string = now.strftime("%Y-%m-%d-%H:%M:%S")
val = (name.prettyPrint(), val.prettyPrint(), dt_string)
cursor.execute(sql, val)
connection.commit()
print('{0}Trap message ends{0}\n'.format('-' * 20))