etingof / pysnmp

Python SNMP library
http://snmplabs.com/pysnmp/
BSD 2-Clause "Simplified" License
583 stars 201 forks source link

writecommit failed #77

Open HarelAshwal opened 7 years ago

HarelAshwal commented 7 years ago

Hi All,

I'm having a SET Sequence with SNMP using the writeCommit method:

def writeCommit(self, name, val, *args):
print " Setting var..." obj = setValue(fullPath,val)
if (obj == 'err') : raise 'some error'

I'm trying to fail the write sequence in case the set operation didn't passed OK. but, when i'm raising an error the entire agent is stopping. how do i do that? how do i inform the SNMP client the set operation failed without stopping the agent?

etingof commented 7 years ago

Depending on which SNMP error you are trying to convey to the client, you should only raise exceptions based on the pysnmp.smi.error.SmiError class. For example NotWritableError or NoAccessError.

Also note that it is generally cheaper to fail in the writeTest() method which is called for every variable in the SET request prior to writeCommit. That's because if you fail in writeCommit(), the rest of the variables in the SET query that were "set" prior to the failure would have to be "unset".

Here is a baseline implementation for your reference.

HarelAshwal commented 7 years ago

LK"I

Thanks for the quick replay. here is my code:

main function to start the agent: def serve_forever(self): print "Starting agent" self._snmpEngine.transportDispatcher.jobStarted(1) try: self._snmpEngine.transportDispatcher.runDispatcher() except: self._snmpEngine.transportDispatcher.closeDispatcher() raise

and the writeTest: (raising the ValueConstraintError from SmiError) def writeTest(self, name, idx, acInfo, *args): print " Testing var..."
raise error.ValueConstraintError()

but it still just fails and stops the snmp engine: Testing var... Traceback (most recent call last): File "C:\projects_node\CompleteDemo\SNMP\SNMP.py", line 265, in agent.serve_forever() File "C:\projects_node\CompleteDemo\SNMP\SNMP.py", line 193, in serve_forever self._snmpEngine.transportDispatcher.runDispatcher() File "C:\Python27\lib\site-packages\pysnmp\carrier\asyncore\dispatch.py", line 49, in runDispatcher raise PySnmpError('poll error: %s' % ';'.join(format_exception(*exc_info())) ) PySnmpError: poll error: Traceback (most recent call last): ; File "C:\Python27\lib\site-packages\pysnmp\carrier\asyncore\dispatch.py", lin e 45, in runDispatcher use_poll=True, map=self.sockMap, count=1) ; File "C:\Python27\lib\asyncore.py", line 220, in loop poll_fun(timeout, map) ; File "C:\Python27\lib\asyncore.py", line 156, in poll read(obj) ; File "C:\Python27\lib\asyncore.py", line 87, in read obj.handle_error() ; File "C:\Python27\lib\asyncore.py", line 83, in read obj.handle_read_event() ; File "C:\Python27\lib\asyncore.py", line 449, in handle_read_event self.handle_read() ; File "C:\Python27\lib\site-packages\pysnmp\carrier\asyncore\dgram\base.py", l ine 157, in handle_read self._cbFun(self, transportAddress, incomingMessage) ; File "C:\Python27\lib\site-packages\pysnmp\carrier\base.py", line 68, in _cbF un self, transportDomain, transportAddress, incomingMessage ; File "C:\Python27\lib\site-packages\pysnmp\entity\engine.py", line 145, in receiveMessageCbFun self, transportDomain, transportAddress, wholeMsg ; File "C:\Python27\lib\site-packages\pysnmp\proto\rfc3412.py", line 407, in re ceiveMessage PDU, maxSizeResponseScopedPDU, stateReference) ; File "C:\Python27\lib\site-packages\pysnmp\entity\rfc3413\cmdrsp.py", line 13 3, in processPdu (self.__verifyAccess, snmpEngine)) ; File "C:\Python27\lib\site-packages\pysnmp\entity\rfc3413\cmdrsp.py", line 31 7, in handleMgmtOperation (acFun, acCtx))) ; File "C:\Python27\lib\site-packages\pysnmp\smi\instrum.py", line 250, in writ eVars return self.flipFlopFsm(self.fsmWriteVar, vars, acInfo) ; File "C:\Python27\lib\site-packages\pysnmp\smi\instrum.py", line 218, in flip FlopFsm rval = f(tuple(name), val, idx, acInfo) ; File "c:\users\ashwalh\appdata\local\temp\pip-build-iibrk4\pysnmp\pysnmp\smi\ mibs\SNMPv2-SMI.py", line 470, in writeTest ; File "c:\users\ashwalh\appdata\local\temp\pip-build-iibrk4\pysnmp\pysnmp\smi\ mibs\SNMPv2-SMI.py", line 570, in writeTest ; File "c:\users\ashwalh\appdata\local\temp\pip-build-iibrk4\pysnmp\pysnmp\smi\ mibs\SNMPv2-SMI.py", line 470, in writeTest ; File "C:\projects_node\CompleteDemo\SNMP\SNMP.py", line 87, in writeTest raise error.ValueConstraintError() ;ValueConstraintError

Press any key to continue . . .