def makeDesc(self, *args, **kwargs): switchObj = kwargs['switchObj'] get_data = switchObj.getData func_value = GET_DATA[switchObj.get_data_function] if not (get_data & func_value == func_value): kwargs['message'] = ' switch makeDesc no start' logger.info(**kwargs) return True oidNameList = [ 'dot1dBasePortIfIndex', 'ifName', 'dot1dTpFdbAddress', 'dot1dTpFdbPort', 'sysDecr' ] desc = '' for oidName in oidNameList: switchObj.oidName = oidName comClass, comFunc = base.get_class_func(model=oid, switchObj=switchObj) generic = comFunc(comClass(), switchObj=switchObj) try: desc = '%s%s generic number is %s\n' % (desc, oidName, len(generic)) except: desc = '%s%s generic None\n' % (desc, oidName) switchObj.desc = desc switchObj.getData = switchObj.getData - func_value switchObj.save()
def get_oid_str(*args, **kwargs): switchObj = kwargs['switchObj'] oidName = switchObj.oidName.lower() ##configparser options default lower function = 'base.get_oid_str' switch_type = cf.sections[switchObj.type] try: oidStr = cf.options[switch_type][oidName] kwargs['message'] = 'oidName:%s switch_type:%s get oid_str:%s' % ( oidName, switch_type, oidStr) logger.info(**kwargs) return oidStr except: try: oidStr = cf.options['Default'][oidName] kwargs[ 'message'] = 'oidName:%s No switch_type:%s Default Class get oid_str:%s' % ( oidName, switch_type, oidStr) logger.info(**kwargs) return oidStr except: kwargs['message'] = 'no oidName:%s in class !!%s and Default!!' % ( oidName, switch_type) logger.error(**kwargs) return None
def _command(self, *args, **kwargs): switchObj = kwargs['switchObj'] get_data = switchObj.getData get_data_function = switchObj.get_data_function func_value = GET_DATA[get_data_function] if define.GET_DATA['enable'] & switchObj.getData == 0: kwargs['message'] = ' switch getData is 0' logger.error(**kwargs) return None kwargs['message'] ='switch name:%s getDataValue:%s, define.GET_DATA[%s] = %s ' % \ (switchObj.name,get_data,get_data_function,func_value) logger.info(**kwargs) if func_value != get_data & func_value: kwargs['message'] = '%s:%s return None' % (switchObj.name, switchObj.oidName) logger.info(**kwargs) return None oidClass, oidFunc = base.get_class_func(model=oid, switchObj=switchObj) ##call Oid function oidObj = oidClass() generic = oidFunc(oidObj, switchObj=switchObj) return generic
def walk(*args, **kwargs): switchObj = kwargs['switchObj'] community = switchObj.community ip = switchObj.ip oidValue = get_oid_str(*args, **kwargs) if not oidValue: kwargs[ 'message'] = 'call get_oid_str return None, so base.walk return None' logger.error(**kwargs) return None errorIndication, errorStatus, errorIndex, \ generic = cmdgen.CommandGenerator().nextCmd(cmdgen.CommunityData('test-agent', community), \ cmdgen.UdpTransportTarget((ip, 161)),oidValue,ignoreNonIncreasingOid=True) if not generic: ## the switch no the Oid kwargs[ 'message'] = 'The Switch:%s not support thd Oid:%s return None' % ( switchObj.name, switchObj.oidName) logger.error(**kwargs) try: number = len(generic) kwargs[ 'message'] = 'Oid:%s base.walk return generic recoder number:[%s]' % ( switchObj.oidName, number) logger.info(**kwargs) except: kwargs[ 'message'] = 'oid:%s len(generic) fault,unknow walk generic number' % switchObj.oidName logger.error(**kwargs) return generic
def wrapper(*args, **kwargs): kwargs['modelName'] = modelName kwargs['funcName'] = funcName kwargs['message'] = 'start: %s--%s' % (modelName, funcName) logger.info(**kwargs) retValue = func(*args, **kwargs) kwargs['message'] = 'end : %s--%s' % (modelName, funcName) logger.info(**kwargs) return retValue
def make_file_name_DEBUG(**kwargs): localtime = time.localtime() taskNumber = 'Year%s-Month%s-Day%s-Hour%s-Min%s-Sec%s'% \ (localtime.tm_year,localtime.tm_mon,localtime.tm_mday,localtime.tm_hour,localtime.tm_min,localtime.tm_sec) path = settings.BASE_DEBUG if not os.path.exists(path): os.mkdir(path) fileName = os.path.join(path, '%s' % taskNumber) kwargs['message'] = 'Return fileName:%s' % fileName logger.info(**kwargs) return fileName
def getMac(self, *args, **kwargs): switchObj = kwargs['switchObj'] if switchObj.getMacControl & define.GET_MAC_CONTROL[ 'standard_getMac'] == define.GET_MAC_CONTROL[ 'standard_getMac']: kwargs['message'] = 'start standard_getMac' logger.info(**kwargs) self._standard_getMac(*args, **kwargs) else: kwargs['message'] = 'start non_standard_getMac' logger.info(**kwargs) self._non_standard_getMac(*args, **kwargs) return True
def target(*args, **kwargs): switchObj = kwargs['switchObj'] switchObj.get_data_function = 'makeDesc' kwargs['message'] = ' --------start %s:%s:%s-------' % ( switchObj.name, switchObj.ip, switchObj.get_data_function) logger.info(**kwargs) comClass, comFunc = base.get_class_func(model=command, switchObj=switchObj) if comFunc: comFunc(comClass(), switchObj=switchObj) kwargs['message'] = ' --------end %s:%s:%s-------' % ( switchObj.name, switchObj.ip, get_data_function) logger.info(**kwargs) print(kwargs['message'])
def initPort(self, *args, **kwargs): switchObj = kwargs['switchObj'] switchObj.func = 'initPort' # get dot1dBasePortIfIndex #oidName = dot1dBasePortIfIndex kwargs['message'] = 'start get dot1dBasePortIfIndex oid value' logger.info(**kwargs) switchObj.oidName = 'dot1dBasePortIfIndex' index_generic = self._command(**kwargs) if not index_generic: kwargs[ 'message'] = ' get dot1dBasePortIfIndex oid value is None' logger.error(**kwargs) return None kwargs[ 'message'] = ' get dot1dBasePortIfIndex oid value number: %s' % len( index_generic) logger.info(**kwargs) kwargs['message'] = 'start get ifName oid value' logger.info(**kwargs) switchObj.oidName = 'ifName' name_generic = self._command(**kwargs) if not name_generic: kwargs['message'] = ' get ifName oid value is None' logger.error(**kwargs) return None kwargs['message'] = ' get ifName oid value number: %s' % len( name_generic) logger.info(**kwargs) # delete port set and mac set portSet = switchObj.ports.all() if len(portSet) > 0: for port in portSet: port.macs.all().delete() portSet.delete() ##create index mac for index2, index in index_generic.items(): try: models.Port.objects.create(switch=switchObj, index=index2, name=name_generic[index]) except: print(index, index2) print(name_generic) switchObj.getData = switchObj.getData - define.GET_DATA[ switchObj.get_data_function] switchObj.save() return True
def make_file_name(**kwargs): ##/../DATA/switch_name/taskNumber switchObj = kwargs['switchObj'] oidName = switchObj.oidName name = switchObj.name localtime = time.localtime() taskNumber = '%s-%s-%s-%sHour-%s-%s' % ( localtime.tm_year, localtime.tm_mon, localtime.tm_mday, localtime.tm_hour, localtime.tm_min, localtime.tm_sec) path = os.path.join(settings.BASE_OID, name) if not os.path.exists(path): os.mkdir(path) fileName = os.path.join(path, '%s-%s' % (oidName, taskNumber)) if os.path.exists(fileName): fileNameMove = "%s.%s" % (fileName, int(time.time())) shutil.move(fileName, fileNameMove) kwargs['message'] = 'Return fileName:%s' % fileName logger.info(**kwargs) return fileName
def get_class_func(*args, **kwargs): model = kwargs['model'] switchObj = kwargs['switchObj'] switch_type = cf.sections[switchObj.type] try: oidName = switchObj.oidName except: oidName = switchObj.get_data_function try: class_name = model.__dict__[switch_type] except: try: class_name = model.__dict__['Default'] except: kwargs[ 'message'] = 'in model:%s No Default class And no :%s,return None' % ( model.__name__, switch_type) logger.error(**kwargs) return None, None try: func = class_name.Func[oidName] except: try: func = model.__dict__['Default'].Func[oidName] except: kwargs['message'] = ' %s.Default No function:%s' % ( model.__name__, oidName) logger.error(**kwargs) return None, None kwargs['message'] = 'reutrn class:func --> %s:%s' % (class_name.__name__, func.__name__) logger.info(**kwargs) return class_name, func
def _non_standard_getMac(self, *args, **kwargs): switchObj = kwargs['switchObj'] ##get index map port portSet = switchObj.ports.all() if len(portSet) == 0: kwargs['message'] = ' portSet number is 0,please initPort' logger.error(**kwargs) logger.error(**kwargs) return None portObj_index = {} for port in portSet: portObj_index[port.index] = port ## get oid:dot1dTpFdbPort --index_generic[mac1]=index-- switchObj.oidName = 'dot1dTpFdbPort' index_generic = self._command(*args, **kwargs) if not index_generic: kwargs['message'] = ' get oid:dot1dTpFdbPort return None' logger.error(**kwargs) return None index_number = len(index_generic) kwargs[ 'message'] = ' get oid:dot1dTpFdbPort generic number: %s' % index_number logger.info(**kwargs) ##get mac_port[mac] = port mac_port = {} for mac, index in index_generic.items(): try: mac_port[mac] = portObj_index[index] except: pass ## make port_macList[port]= [mac list] port_macList = {} for mac, port in mac_port.items(): try: port_macList[port].append(mac) except: port_macList[port] = [] port_macList[port].append(mac) ##make mac[macAddress] = macObj macSet = models.Mac.objects.all() macDict = {} macDictKeys = [] for macObj in macSet: macDictKeys.append(macObj.mac) macDict[macObj.mac] = macObj ##getMacControl ##standard_getMac = 1 #port_access_check = 2 if switchObj.getMacControl & define.GET_MAC_CONTROL[ 'port_access_check'] == define.GET_MAC_CONTROL[ 'port_access_check']: for portObj, macList in port_macList.items(): if portObj.access != 0: ## port access non switch for mac in macList: if mac in macDictKeys: if macDict[mac].port != portObj: macDict[mac].port = portObj macDict[mac].save() else: models.Mac.objects.create(port=portObj, mac=mac) else: ## non start define.port_access_check for portObj, macList in port_macList.items(): if len(macList) < define.MaxMacPortNumber: for mac in macList: if mac in macDictKeys: if macDict[mac].port != portObj: macDict[mac].port = portObj macDict[mac].save() else: models.Mac.objects.create(port=portObj, mac=mac) return True