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 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 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 sysDecr(self, **kwargs): switchObj = kwargs['switchObj'] generic = kwargs['generic'] versionDict = {} fileName = base.make_file_name(switchObj=switchObj) with open(fileName, 'a') as f: key = 0 value = '' for g in generic: try: value = value + str(g[0][1]).replace('\r', ' ').replace( '\n', ' ') except: kwargs[ 'message'] = 'value = value + str(g[0][1]). IS ERROR' logger.error(**args) line = '%s\n' % value f.write(line) versionDict[key] = value return versionDict
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 _default(self,*args,**kwargs): switchObj=kwargs['switchObj'] oidName = switchObj.oidName generic = base.walk(oidName = oidName,switchObj= switchObj) if not generic: return None ############# generic change to dict ############################## #get class ,func dealClass,dealFunc = base.get_class_func(model=deal,switchObj=switchObj) if not dealFunc: switch_type = self.sections[switchObj.type] kwargs['message'] = 'Switch.oid.%s.%s no funC' % (switch_type,oidName) logger.error(**kwargs) return None ## deal with generic dealObj = dealClass() retDict = dealFunc(dealObj,model=deal,switchObj=switchObj,generic=generic) return retDict
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