def test_multiTableGetBad(self): oids = [ '.1.3.6.1.2.1.1', '.1.3.6.1.2.1.2', '.1.3.6.2', '.1.3.6.3', ] d = self.client.getTable(oids) self.doUntilFinish(d) for oidObject in oids[:-1]: assert self.response.has_key(oid.OID(oidObject)) assert not self.response.has_key(oid.OID(oids[-1])), self.response
def test_tableGet(self): """Can retrieve a tabular value?""" d = self.client.getTable(['.1.3.6.1.2.1.1']) self.doUntilFinish(d) assert self.success, self.response assert isinstance(self.response, dict), self.response assert self.response.has_key( oid.OID('.1.3.6.1.2.1.1')), (self.response, self) tableData = self.response[oid.OID('.1.3.6.1.2.1.1')] assert isinstance(tableData, dict) assert tableData.has_key(oid.OID('.1.3.6.1.2.1.1.1.0')), tableData
def test_simpleGet(self): """Can retrieve a single simple value?""" d = self.client.get([ '.1.3.6.1.2.1.1.1.0', ]) self.doUntilFinish(d) assert self.success, self.response assert isinstance(self.response, dict), self.response assert self.response.has_key( oid.OID('.1.3.6.1.2.1.1.1.0')), self.response assert self.response[oid.OID( '.1.3.6.1.2.1.1.1.0')] == 'Hello world!', self.response
def __init__( self, proxy, roots, includeStart=0, retryCount=4, timeout= 2.0, maxRepetitions=128, ): """Initialise the retriever proxy -- the AgentProxy instance we want to use for retrieval of the data roots -- root OIDs to retrieve includeStart -- whether to include the starting OID in the set of results, by default, return the OID *after* the root oids retryCount -- number of retries timeout -- initial timeout, is multipled by 1.5 on each timeout iteration. maxRepetitions -- max records to request with a single bulk request """ self.proxy = proxy self.roots = [ oid.OID(r) for r in roots] self.includeStart = includeStart self.retryCount = retryCount self.timeout = timeout self.values = {} # {rootOID: {OID: value}} mapping self.maxRepetitions = maxRepetitions
def test_tableGetAll(self): """Does tabular retrieval work specifying a distant parent (e.g. .1.3.6)?""" d = self.client.getTable(['.1.3.6']) self.doUntilFinish(d) assert self.success, self.response assert self.response == { oid.OID('.1.3.6'): dict(self.oidsForTesting) }, self.response
def test_setReplaceAnOID(self): """After a replace-set, is the set value retrieved?""" d = self.client.set([('.1.3.6.1.2.1.1.4.0', 3)], ) self.doUntilFinish(d) d = self.client.get([ '.1.3.6.1.2.1.1.4.0', ], ) self.doUntilFinish(d) assert self.success, self.response assert self.response == {oid.OID('.1.3.6.1.2.1.1.4.0'): 3}
def test_tableGetWithStart(self): """Can retrieve a tabular value?""" d = self.client.getTable( ['.1.3.6.1.2.1.1'], startOIDs=['.1.3.6.1.2.1.1.3.0'], ) self.doUntilFinish(d) assert self.success, self.response assert isinstance(self.response, dict), self.response assert self.response.has_key( oid.OID('.1.3.6.1.2.1.1')), (self.response, self) tableData = self.response[oid.OID('.1.3.6.1.2.1.1')] assert isinstance(tableData, dict) # won't have this key because started later... assert not tableData.has_key(oid.OID('.1.3.6.1.2.1.1.1.0')), tableData # should (only) have this key because started at 3... assert tableData.has_key(oid.OID('.1.3.6.1.2.1.1.4.0')), tableData assert len(tableData) == 1
def test_multiTableGet(self): oids = [ '.1.3.6.1.2.1.1', '.1.3.6.1.2.1.2', '.1.3.6.2', ] d = self.client.getTable(oids) self.doUntilFinish(d) if not self.success: raise self.response.value else: for oidObject in oids: assert self.response.has_key(oid.OID(oidObject))
def test_setEndOfOIDs(self): """After a set, is the set value retrieved?""" d = self.client.set([('.1.3.6.1.2.1.1.5.0', 3)], ) self.doUntilFinish(d) d = self.client.get([ '.1.3.6.1.2.1.1.5.0', ], ) self.doUntilFinish(d) assert self.success, self.response ## import pdb ## pdb.set_trace() assert self.response == { oid.OID('.1.3.6.1.2.1.1.5.0'): 3 }, self.response
def testMassRetriever(self): """Can we retrieve mass value single-oid values?""" proxies = massretriever.proxies( self.client.protocol, [('127.0.0.1', self.agent.port, 'public', self.version)] * 250) retriever = massretriever.MassRetriever(proxies) retriever.verbose = 1 d = retriever(oids=[ '.1.3.6.1.1.3', ]) self.doUntilFinish(d) assert self.success, self.response assert self.response == { ('127.0.0.1', self.agent.port): { oid.OID('.1.3.6.1.1.3'): 'Blah!' } }, self.response retriever.printStats()
def handleSingleError(self, err, oids, proxy): """Handle single-agent error by making result[key][oid] None err -- handle complete failure of a single proxy to retrieve values. oids -- the oids being queried proxy -- the proxy being queried Values are added to the (ip,port) dictionary for the proxy with value None iff the root OID does not already exist in the dictionary. returns None """ self.errorCount = self.errorCount + 1 log.info('Error: %r %s', proxy, err) self.printStats() if isinstance(err, failure.Failure): actualError = err.value trace = err.getTraceback() else: actualError = err trace = log.getException(err) if not isinstance(actualError, error.TimeoutError): log.error( """Retrieval for proxy %r encountered unexpected error: %s""", proxy, trace, ) if isinstance(err, (KeyboardInterrupt, SystemExit)): if not self.finalDefer.called: self.finalDefer.errback(err) return None key = proxy.ip, proxy.port set = self.result.get(key) if set is None: self.result[key] = set = {} for oidObject in oids: oidObject = oid.OID(oidObject) if not set.has_key(oidObject): set[oidObject] = None return None
def send(self, agent, genericType=6, specificType=0, pdus=None): """Given agent, send our message to the management stations""" # XXX pysnmp doesn't seem to support v2 traps... from pysnmp.proto.api import alpha ver = alpha.v1 req = ver.Message() req.apiAlphaSetCommunity(self.community) trap = ver.TrapPdu() if ver is alpha.v1: trap.apiAlphaSetEnterprise(agent.getSysObjectId()) trap.apiAlphaSetGenericTrap(genericType) trap.apiAlphaSetSpecificTrap(specificType) if pdus: if hasattr(pdus, 'items'): pdus = pdus.items() pdus = [(oid.OID(key), datatypes.typeCoerce(value, v1)) for key, value in pdus] trap.apiAlphaSetVarBindList(*pdus) else: raise NotImplementedError("""No v2c trap-sending support yet""") req.apiAlphaSetPdu(trap) return agent.protocol.send(req.berEncode(), self.managerIP)
def createStorage( oidsForTesting = [ (oid.OID(key),value) for key,value in [ ('.1.3.6.1.2.1.1.1.0', 'Hello world!'), ('.1.3.6.1.2.1.1.2.0', 32), ('.1.3.6.1.2.1.1.3.0', v1.IpAddress('127.0.0.1')), ('.1.3.6.1.2.1.1.4.0', v1.OctetString('From Octet String')), ('.1.3.6.1.2.1.2.1.0', 'Hello world!'), ('.1.3.6.1.2.1.2.2.0', 32), ('.1.3.6.1.2.1.2.3.0', v1.IpAddress('127.0.0.1')), ('.1.3.6.1.2.1.2.4.0', v1.OctetString('From Octet String')), ] + [ ('.1.3.6.1.2.1.3.%s.0'%i, 32) for i in xrange( 512 ) ] + [ ('.1.3.6.2.1.0', 'Hello world!'), ('.1.3.6.2.2.0', 32), ('.1.3.6.2.3.0', v1.IpAddress('127.0.0.1')), ('.1.3.6.2.4.0', v1.OctetString('From Octet String')), ] ]): return bisectoidstore.BisectOIDStore( OIDs = oidsForTesting, )
def testMassRetrieverTables(self): """Can we retrieve mass value tabular sets?""" import random GOOD_COUNT = 500 BAD_COUNT = 500 proxies = massretriever.proxies( self.client.protocol, [('127.0.0.1', self.agent.port, 'public', self.version)] * GOOD_COUNT + [('127.0.0.1', self.agent.port + 10000, 'public', self.version)] * BAD_COUNT) random.shuffle(proxies) random.shuffle(proxies) random.shuffle(proxies) retriever = massretriever.MassRetriever(proxies) retriever.verbose = 1 d = retriever(tables=[ '.1.3.6.1.2.1', ]) self.doUntilFinish(d) assert self.success, self.response expected = { ('127.0.0.1', self.agent.port): { oid.OID('.1.3.6.1.2.1'): { oid.OID('.1.3.6.1.2.1.1.1.0'): 'Hello world!', oid.OID('.1.3.6.1.2.1.1.2.0'): 32, oid.OID('.1.3.6.1.2.1.1.3.0'): '127.0.0.1', oid.OID('.1.3.6.1.2.1.1.4.0'): 'From Octet String', } }, ('127.0.0.1', self.agent.port + 10000): { oid.OID('.1.3.6.1.2.1'): None, }, } assert self.response == expected, (expected, self.response) retriever.printStats() assert retriever.successCount == GOOD_COUNT, """Expected %s valid responses, got %s""" % ( GOOD_COUNT, retriever.successCount) assert retriever.errorCount == BAD_COUNT, """Expected %s valid responses, got %s""" % ( GOOD_COUNT, retriever.successCount)
class GetRetrieverV1(basetestcase.BaseTestCase): version = 'v1' oidsForTesting = [(oid.OID(key), value) for key, value in [ ('.1.3.6.1.2.1.1.1.0', 'Hello world!'), ('.1.3.6.1.2.1.1.2.0', 32), ('.1.3.6.1.2.1.1.3.0', v1.IpAddress('127.0.0.1')), ('.1.3.6.1.2.1.1.4.0', v1.OctetString('From Octet String')), ('.1.3.6.1.2.1.2.1.0', 'Hello world!'), ('.1.3.6.1.2.1.2.2.0', 32), ('.1.3.6.1.2.1.2.3.0', v1.IpAddress('127.0.0.1')), ('.1.3.6.1.2.1.2.4.0', v1.OctetString('From Octet String')), ] + [('.1.3.6.1.2.1.3.%s.0' % i, 32) for i in xrange(512)] + [ ('.1.3.6.2.1.0', 'Hello world!'), ('.1.3.6.2.2.0', 32), ('.1.3.6.2.3.0', v1.IpAddress('127.0.0.1')), ('.1.3.6.2.4.0', v1.OctetString('From Octet String')), ]] #good def test_simpleGet(self): """Can retrieve a single simple value?""" d = self.client.get([ '.1.3.6.1.2.1.1.1.0', ]) self.doUntilFinish(d) assert self.success, self.response assert isinstance(self.response, dict), self.response assert self.response.has_key( oid.OID('.1.3.6.1.2.1.1.1.0')), self.response assert self.response[oid.OID( '.1.3.6.1.2.1.1.1.0')] == 'Hello world!', self.response #good def test_tableGet(self): """Can retrieve a tabular value?""" d = self.client.getTable(['.1.3.6.1.2.1.1']) self.doUntilFinish(d) assert self.success, self.response assert isinstance(self.response, dict), self.response assert self.response.has_key( oid.OID('.1.3.6.1.2.1.1')), (self.response, self) tableData = self.response[oid.OID('.1.3.6.1.2.1.1')] assert isinstance(tableData, dict) assert tableData.has_key(oid.OID('.1.3.6.1.2.1.1.1.0')), tableData def test_tableGetWithStart(self): """Can retrieve a tabular value?""" d = self.client.getTable( ['.1.3.6.1.2.1.1'], startOIDs=['.1.3.6.1.2.1.1.3.0'], ) self.doUntilFinish(d) assert self.success, self.response assert isinstance(self.response, dict), self.response assert self.response.has_key( oid.OID('.1.3.6.1.2.1.1')), (self.response, self) tableData = self.response[oid.OID('.1.3.6.1.2.1.1')] assert isinstance(tableData, dict) # won't have this key because started later... assert not tableData.has_key(oid.OID('.1.3.6.1.2.1.1.1.0')), tableData # should (only) have this key because started at 3... assert tableData.has_key(oid.OID('.1.3.6.1.2.1.1.4.0')), tableData assert len(tableData) == 1 #good def test_tableGetMissing(self): """Does tabular retrieval ignore non-existent oid-sets?""" d = self.client.getTable(['.1.3.6.1.2.1.5']) self.doUntilFinish(d) assert self.success, self.response assert self.response == {}, self.response #good def test_tableGetAll(self): """Does tabular retrieval work specifying a distant parent (e.g. .1.3.6)?""" d = self.client.getTable(['.1.3.6']) self.doUntilFinish(d) assert self.success, self.response assert self.response == { oid.OID('.1.3.6'): dict(self.oidsForTesting) }, self.response #bad def test_multiTableGet(self): oids = [ '.1.3.6.1.2.1.1', '.1.3.6.1.2.1.2', '.1.3.6.2', ] d = self.client.getTable(oids) self.doUntilFinish(d) if not self.success: raise self.response.value else: for oidObject in oids: assert self.response.has_key(oid.OID(oidObject)) #good def test_multiTableGetBad(self): oids = [ '.1.3.6.1.2.1.1', '.1.3.6.1.2.1.2', '.1.3.6.2', '.1.3.6.3', ] d = self.client.getTable(oids) self.doUntilFinish(d) for oidObject in oids[:-1]: assert self.response.has_key(oid.OID(oidObject)) assert not self.response.has_key(oid.OID(oids[-1])), self.response #good def test_tableGetErrorReported(self): oids = [ '.1.3.6.1.2.1.1', '.1.3.6.1.2.1.2', '.1.3.6.2', '.1.3.6.3', ] originalAreWeDone = tableretriever.TableRetriever.areWeDone def raiseError(*args, **named): raise TypeError("""Blah""") tableretriever.TableRetriever.areWeDone = raiseError try: d = self.client.getTable(oids) self.doUntilFinish(d) finally: tableretriever.TableRetriever.areWeDone = originalAreWeDone assert not self.success assert isinstance(self.response.value, TypeError) assert self.response.value.args == ('Blah', ) #good def test_socketFailure(self): """Test whether socket failure on send is caught properly Previous versions have not caught failure-on-send conditions, which can wind up having unexpected consequences, particularly with things such as mass-retriever, which build on top of the basic AgentProxy. """ import socket def mockSend(message): raise socket.error(65, 'No route to host') self.client.send = mockSend d = self.client.get([ '.1.3.6.1.2.1.1.1.0', ]) self.doUntilFinish(d) assert not self.success assert isinstance(self.response.value, socket.error) assert self.response.value.args == (65, 'No route to host') #good def test_socketFailureTable(self): """Test whether socket failure on send is caught properly for tables Previous versions have not caught failure-on-send conditions, which can wind up having unexpected consequences, particularly with things such as mass-retriever, which build on top of the basic AgentProxy. """ self.client.send = socketErrorSend d = self.client.getTable([ '.1.3.6.1.2.1.1.1.0', ], retryCount=2) self.doUntilFinish(d) assert not self.success assert isinstance(self.response.value, socket.error) assert self.response.value.args == (65, 'No route to host')