Пример #1
0
    def test_snmp_get(self):
        self.simulator.add_walkfile(WALK_FILE)

        def cbFun(sendRequestHandle, errorIndication, errorStatus, errorIndex,
                  varBinds, cbCtx):
            if errorIndication or errorStatus:
                raise Exception('SNMP error!')

            oid, val = varBinds[0]
            assert oid.prettyPrint() == '1.3.6.1.2.1.1.1.0'
            assert val.prettyPrint().startswith('Cisco')

        cmdGen = cmdgen.AsynCommandGenerator()
        cmdGen.getCmd(cmdgen.CommunityData('public'),
                      cmdgen.UdpTransportTarget(('127.0.0.1', 1161)),
                      (cmdgen.MibVariable('SNMPv2-MIB', 'sysDescr', 0), ),
                      (cbFun, None))
        simulator_snmpEngine = self.simulator.snmp_agent.snmpEngine
        while cmdGen.snmpEngine.transportDispatcher.jobsArePending(
        ) or cmdGen.snmpEngine.transportDispatcher.transportsAreWorking():
            asyncore.poll(0.001,
                          cmdGen.snmpEngine.transportDispatcher.getSocketMap())
            cmdGen.snmpEngine.transportDispatcher.handleTimerTick(time.time())

            asyncore.poll(
                0.001, simulator_snmpEngine.transportDispatcher.getSocketMap())
            simulator_snmpEngine.transportDispatcher.handleTimerTick(
                time.time())
Пример #2
0
 def __init__(self, host, port, auth):
     self.dispatcher = dispatch.AsynsockDispatcher()
     self.engine = engine.SnmpEngine()
     self.engine.registerTransportDispatcher(self.dispatcher)
     self.generator = cmdgen.AsynCommandGenerator(self.engine)
     self.auth = auth
     self.transport = cmdgen.UdpTransportTarget((host, port))
Пример #3
0
def dowork():
	global snmp_dict
	timenow = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
	start = datetime.datetime.now()
	print("Started...", timenow)
	cmdGen  = cmdgen.AsynCommandGenerator()

	# Read input file of devices, credentials, MIB objects, indicies, etc
	data = ''
	with open('devicemetrics2.conf', 'r') as inputfile:
		for line in inputfile:
			if not line.startswith("#"):
				data += line.strip()
	
	targets = eval(data)
	
	# Submit GET requests
	for authData, transportTarget, varNames in targets:
		#print(authData, transportTarget, varNames)
		cmdGen.getCmd(authData, transportTarget, varNames,
		# User-space callback function and its context
		(cbFun, (authData, transportTarget)),lookupNames=True, lookupValues=True)
	
	cmdGen.snmpEngine.transportDispatcher.runDispatcher()
	calcratios()
	calcmemratios()
	
	end = datetime.datetime.now()
	elapsed = end - start
	#print(elapsed.seconds,":",elapsed.microseconds) 
	
	#timenow = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
	print("   Done... {0}.{1}sec".format(elapsed.seconds, elapsed.microseconds))
Пример #4
0
    def __init__(self, zmq_core, logger):
        self.zmq_core = zmq_core
        self.logger = logger
        self.scheduler = SNMPScheduler(logger=logger)

        self.transport_dispatcher = AsynsockDispatcher()
        self.transport_dispatcher.setSocketMap(zmq_core.socket_map)
        snmp_engine = engine.SnmpEngine()
        snmp_engine.registerTransportDispatcher(self.transport_dispatcher)
        self.cmd_gen = cmdgen.AsynCommandGenerator(snmpEngine=snmp_engine)
Пример #5
0
def snmp_get_next_v1v2c(a_device, oid, timeout, retries):
    OID = (oid, )

    a_host, community_string, snmp_port = a_device
    snmp_target = (a_host, snmp_port)
    cmd_gen = cmdgen.AsynCommandGenerator()

    cmd_gen.nextCmd(cmdgen.CommunityData(community_string),
                    cmdgen.UdpTransportTarget(snmp_target, timeout, retries),
                    OID, (cbFun, None))
    cmd_gen.snmpEngine.transportDispatcher.runDispatcher()
Пример #6
0
 def __init__(self):
     super(SNMP, self).__init__()
     self.__lock = Lock()
     self.__mib_builder = None
     self.__mib_view = None
     self.__loaded_mibs = []
     snmpEngine = engine.SnmpEngine()
     transportDispatcher = dispatch.AsynsockDispatcher()
     transportDispatcher.setSocketMap({})
     snmpEngine.registerTransportDispatcher(transportDispatcher)
     self.__dispatcher = snmpEngine.transportDispatcher.runDispatcher
     self.__generator = cmdgen.AsynCommandGenerator(snmpEngine)
     self.__engine = snmpEngine
     return
Пример #7
0
    def poll(self):
        """Called once for each poll (say every 5 minutes)"""

        poll_start.send(sender=self)
        poll = Poll.objects.create(start=datetime.datetime.now())

        mvc = get_mib_view_controller()
        asyn_command_generator = cmdgen.AsynCommandGenerator()

        for device in Device.objects.all():
            transport = get_transport(device)
            authentication = get_authentication(device)

            # If the transport could not be setup (usually a network error) or
            # the authentication details for the device were provided
            # incorrectly, then skip this device in the polling process
            if transport is None or authentication is None:
                poll_error.send(sender=self,
                                device=device,
                                message=DEVICE_TRANSPORT_ERROR_MESSAGE)
                continue

            pre_poll.send(sender=self, device=device)

            # Setup the asynchronous SNMP BULK requests (non blocking)
            package_instance_memberships = \
                PackageInstanceMembership.objects.filter(
                    package_instance__device=device,
                    package_instance__enabled=True, enabled=True)

            for package_instance_membership in package_instance_memberships:
                rule = package_instance_membership.rule
                asyn_command_generator.asyncBulkCmd(
                    authentication, transport, 0, SNMP_GETBULK_SIZE,
                    (rule.data_object.get_identifier_tuple(), ),
                    (self._callback, {
                        'rule': rule,
                        'mvc': mvc,
                        'poll': poll,
                    }))

        # Blocks until all requests have returned
        asyn_command_generator.snmpEngine.transportDispatcher.runDispatcher()

        poll.stop = datetime.datetime.now()
        poll.save()

        post_poll.send(sender=self, device=device)
        poll_stop.send(sender=self)
Пример #8
0
 def v1_scan_func(self, ip):
     asynCommandGenerator = cmdgen.AsynCommandGenerator()
     for j in self.communities.split(","):
         print j
         try:
             sendRequestHandle = asynCommandGenerator.asyncGetCmd(
                 cmdgen.CommunityData('my-agent', j, 1),
                 cmdgen.UdpTransportTarget((ip, 161),
                                           timeout=self.scan_timeout,
                                           retries=self.scan_retries),
                 ((1, 3, 6, 1, 2, 1, 1, 1, 0), (1, 3, 6, 1, 2, 1, 1, 4, 0),
                  (1, 3, 6, 1, 2, 1, 1, 5, 0), (1, 3, 6, 1, 2, 1, 1, 6, 0)),
                 (self.v1_scan_cb, (ip, j)))
             asynCommandGenerator.snmpEngine.transportDispatcher.runDispatcher(
             )
         except Exception, e:
             print "OOooops: " + e
Пример #9
0
def snmp_get_next_v3(snmp_device, snmp_user, oid, timeout, retries, auth_proto,
                     encrypt_proto):
    OID = (oid, )
    a_user, auth_key, encrypt_key = snmp_user

    auth_proto_map = {
        'sha': cmdgen.usmHMACSHAAuthProtocol,
        'md5': cmdgen.usmHMACMD5AuthProtocol,
        'none': cmdgen.usmNoAuthProtocol
    }

    if auth_proto in auth_proto_map.keys():
        auth_protocol = auth_proto_map[auth_proto]
    else:
        raise ValueError("Invalid authentication protocol specified: %s" %
                         auth_proto)

    encrypt_proto_map = {
        'des': cmdgen.usmDESPrivProtocol,
        '3des': cmdgen.usm3DESEDEPrivProtocol,
        'aes128': cmdgen.usmAesCfb128Protocol,
        'aes192': cmdgen.usmAesCfb192Protocol,
        'aes256': cmdgen.usmAesCfb256Protocol,
        'none': cmdgen.usmNoPrivProtocol,
    }

    if encrypt_proto in encrypt_proto_map.keys():
        encrypt_protocol = encrypt_proto_map[encrypt_proto]
    else:
        raise ValueError("Invalid encryption protocol specified: %s" %
                         encrypt_proto)

    cmd_gen = cmdgen.AsynCommandGenerator()

    cmd_gen.nextCmd(
        cmdgen.UsmUserData(a_user,
                           auth_key,
                           encrypt_key,
                           authProtocol=auth_protocol,
                           privProtocol=encrypt_protocol),
        cmdgen.UdpTransportTarget(snmp_device, timeout, retries), OID,
        (cbFun, None))
    cmd_gen.snmpEngine.transportDispatcher.runDispatcher()
Пример #10
0
    def async (self):
        '''
        http://pysnmp.sourceforge.net/examples/current/v3arch/oneliner/manager/cmdgen/get-async-multiple-transports-and-protocols.html
        '''
        def chunks(l, n):
            for i in range(0, len(l), n):
                yield l[i:i + n]

        cmdGen = cmdgen.AsynCommandGenerator()

        chunk_i = 1
        for chunk in chunks(self.targets, CHUNK_SIZE):
            for authData, transportTarget, varNames in chunk:
                cmdGen.getCmd(
                    authData,
                    transportTarget,
                    varNames,
                    # User-space callback function and its context
                    (self.callback, (authData, transportTarget)),
                    lookupNames=True,
                    lookupValues=True)

            cmdGen.snmpEngine.transportDispatcher.runDispatcher()
            chunk_i += 1
Пример #11
0
            count += 1
    # if result != []:

    for i in data:
        if result["username"] in i["username"]:
            result["hashrate"] = i["hashrate"]
    for j in dataC:
        if result["username"] in j["username"]:
            result["hashrateC"] = j["hashrate"]
    result["time"] = time.time()

    # print result
    db[result["username"]].insert(result)


cmdGen = cmdgen.AsynCommandGenerator()

# Submit GET requests

# result = []
for authData, transportTarget, varNames in targets:
    cmdGen.getCmd(
        authData,
        transportTarget,
        varNames,
        # User-space callback function and its context
        (cbFun, (authData, transportTarget)),
        lookupNames=True,
        lookupValues=True)
# print result
    def real_run(self):
        """ Process SNMP tasks
        SNMP task is a dict:
        - For a bulk request ::

            {"authData": cmdgen.CommunityData('public')
             "transportTarget": cmdgen.UdpTransportTarget((transportTarget, 161))
             "nonRepeaters": 0
             "maxRepetitions": 64
             "varNames": ['1.3.6.1.2.1.2.2.1.2.0', '...']
             "cbInfo:: (cbFun, (arg1, arg2, ...))
             }

        - For a next request ::

            {"authData": cmdgen.CommunityData('public')
             "transportTarget": cmdgen.UdpTransportTarget((transportTarget, 161))
             "varNames": ['1.3.6.1.2.1.2.2.1.2.0', '...']
             "cbInfo:: (cbFun, (arg1, arg2, ...))
            }

        - For a get request ::

            {"authData": cmdgen.CommunityData('public)
             "transportTarget": cmdgen.UdpTransportTarget((transportTarget, 161))
             "varNames": ['1.3.6.1.2.1.2.2.1.2.0', '...']
             "cbInfo:: (cbFun, (arg1, arg2, ...))
            }
        """
        self.must_run = True
        logger.info("[SnmpBooster] [code 0602] is starting")
        slow_host_waiting = []
        while self.must_run:
            # Prevent memory leak
            del self.cmdgen
            self.cmdgen = cmdgen.AsynCommandGenerator()
            # End prevent memory leak
            self.task_prepared = 0
            # slow host
            slow_host_prepared = []
            # Process slow hosts tasks
            for index, snmp_task in enumerate(slow_host_waiting):
                # Check if we have our max prepared tasks
                if self.task_prepared > self.max_prepared_tasks:
                    break
                # Handle slow hosts
                if snmp_task['no_concurrency']:
                    if snmp_task['host'] in slow_host_prepared:
                        continue
                    else:
                        slow_host_prepared.append(snmp_task['host'])
                # Add task dispatcher
                slow_host_waiting.pop(index)
                self.append_task_to_dispatcher(snmp_task)
            # Process normal tasks
            while (not self.mapping_queue.empty()) and self.task_prepared <= self.max_prepared_tasks:
                # Get task
                snmp_task = self.mapping_queue.get()
                # Handle slow hosts
                if snmp_task['no_concurrency']:
                    if snmp_task['host'] in slow_host_prepared:
                        slow_host_waiting.append(snmp_task)
                        continue
                    else:
                        slow_host_prepared.append(snmp_task['host'])
                # Add task dispatcher
                self.append_task_to_dispatcher(snmp_task)

            if self.task_prepared > 0:
                # Launch SNMP requests
                self.cmdgen.snmpEngine.transportDispatcher.runDispatcher()
            else:
                # Sleep
                time.sleep(0.1)

        logger.info("[SnmpBooster] [code 0604] is stopped")