def parseDladmAggregationsOutputS11(dladmOutput): aggregationsByKey = {} lines = dladmOutput.split('\n') for line in lines: line = line and line.strip() if line: tokens = line.split(':', 2) if len(tokens) == 3: try: mac = tokens[2].replace('\\:', ':') netutils.parseMac(mac) except ValueError, ex: logger.warn(str(ex)) continue port = tokens[1] if port == '': if aggregationsByKey.has_key(tokens[0]): continue aggregation = _DladmAggregationRecord() aggregation.key = tokens[0] aggregation.mac = mac aggregationsByKey[aggregation.key] = aggregation else: aggregation = aggregationsByKey.get(tokens[0]) if aggregation: aggregatedInterface = _DladmAggregatedInterfaceRecord() aggregatedInterface.name = port aggregatedInterface.mac = mac aggregation.aggregatedInterfacesByName[port] = aggregatedInterface
def parseDladmAggregationsOutput(dladmOutput): aggregationsByKey = {} lines = dladmOutput.split('\n') for line in lines: line = line and line.strip() if line: tokens = re.split(r"\s+", line, 1) if not tokens or len(tokens) < 2: logger.warn("Unknown format of line in dladm command output") continue typeStr = tokens[0] and tokens[0].lower() attributesStr = tokens[1] attributes = __parseDladmAttributes(attributesStr) key = attributes.get('key') if not key: continue if typeStr == 'aggr': if aggregationsByKey.has_key(key): continue aggregation = _DladmAggregationRecord() aggregation.key = key try: mac = attributes.get('address') aggregation.mac = mac and netutils.parseMac(mac) except ValueError, ex: logger.warn(str(ex)) continue aggregation.policy = attributes.get('policy') aggregationsByKey[key] = aggregation elif typeStr == 'dev': aggregation = aggregationsByKey.get(key) if aggregation is None: continue aggregatedInterface = _DladmAggregatedInterfaceRecord() try: mac = attributes.get('address') aggregatedInterface.mac = mac and netutils.parseMac(mac) except ValueError, ex: logger.warn(str(ex)) continue name = attributes.get('device') if name: aggregatedInterface.name = name else: continue aggregation.aggregatedInterfacesByName[name] = aggregatedInterface
def _parseEth(self, buffer): """ Created NIC DO from the output of the lshwres -r virtualio --rsubtype eth --level lpar -m '<Managed System Name>' @param buffer: command output @type buffer: String @return: instance of NetworkInterfaceDo """ if buffer: propertiesDict = self.buildPropertiesDict(buffer) vEth = ibm_hmc_lib.LparNetworkInterface() vEth.macAddress = propertiesDict.get('mac_addr') if not vEth.macAddress and not netutils.isValidMac( vEth.macAddress): raise ValueError, "Failed parsing MAC addres of Virtual Ethernet Card from buffer: %s" % buffer vEth.macAddress = netutils.parseMac(vEth.macAddress) vEth.portVlanId = ibm_hmc_lib.toInteger( propertiesDict.get('port_vlan_id')) vEth.lparName = propertiesDict.get('lpar_name') vEth.slotNum = propertiesDict.get('slot_num') if not vEth.lparName: raise ValueError, "Failed parsing LPar Name for Virtual Ethernet Card from buffer: %s" % buffer vEth.lparId = ibm_hmc_lib.toInteger(propertiesDict.get('lpar_id')) vEth.isVirtual = 1 return vEth
def getFormattedMac(mac): finalResult = None if mac and netutils.isValidMac(mac): normalized = netutils.parseMac(mac) if not isInvalidMac(normalized): finalResult = normalized return finalResult
def getVmwareEthernetPortsByUnitaryComputerSystem(client, unitaryComputerSystem): ''' CimClient, UnitaryComputerSystem -> list[VmwareEthernetPort] ''' # instead of querying by associations query directly and compare key attributes # associations currently return all possible classes, cannot filter by class name portInstances = client.getInstances("VMware_EthernetPort") ports = [] for portInstance in portInstances: port = VmwareEthernetPort() port.setObjectPath(portInstance.getObjectPath()) port.systemName = cim_discover.cleanString(_getCimInstanceProperty(portInstance, 'SystemName')) port.name = cim_discover.cleanString(_getCimInstanceProperty(portInstance, 'Name')) port.elementName = cim_discover.cleanString(_getCimInstanceProperty(portInstance, 'ElementName')) permanentAddress = _getCimInstanceProperty(portInstance, 'PermanentAddress') if netutils.isValidMac(permanentAddress): port.permanentAddress = netutils.parseMac(permanentAddress) if port.systemName == unitaryComputerSystem.name and port.permanentAddress: ports.append(port) return ports
def handlePhysicalNics(self, pnicsArray, hostObject): ''' override in order to parse MACs ''' pnics = pnicsArray and pnicsArray.getPhysicalNic() or None if pnics: physicalNicsByKey = {} physicalNicDeviceToKey = {} for pnic in pnics: key = pnic.getKey() if not key: continue mac = pnic.getMac() if mac: parsedMac = None try: parsedMac = netutils.parseMac(mac) except ValueError, ex: logger.debug(str(ex)) pnic.setMac(parsedMac) device = pnic.getDevice() if device: physicalNicDeviceToKey[device] = key physicalNicsByKey[key] = pnic hostObject.pnicsByKey = physicalNicsByKey hostObject._physicalNicDeviceToKey = physicalNicDeviceToKey
def getMAC(shell, int_name): cmdResult = None rawCmdResult = None mac = None entstat = None try: entstat_command = concatenate('entstat ', int_name) logger.debug(concatenate(' Executing command: ', entstat_command)) entstat = shell.execCmd(entstat_command) if entstat != None: m = re.search('Device Type: (.+)', entstat) description = None if(m): description = m.group(1).strip() m = re.search('Hardware Address: ([0-9a-f:]{17})', entstat) rawMac = None if(m): rawMac = m.group(1) mac = netutils.parseMac(rawMac) except: msg = " Failed getting MAC address for interface '%s'" % int_name errobj = errorobject.createError(errorcodes.FAILED_GETTING_INFORMATION, None, msg) logger.reportWarningObject(errobj) logger.debug(msg) return None return mac
def getVmwareEthernetPortsByUnitaryComputerSystem(client, unitaryComputerSystem): ''' CimClient, UnitaryComputerSystem -> list[VmwareEthernetPort] ''' # instead of querying by associations query directly and compare key attributes # associations currently return all possible classes, cannot filter by class name portInstances = client.getInstances("VMware_EthernetPort") ports = [] for portInstance in portInstances: port = VmwareEthernetPort() port.setObjectPath(portInstance.getObjectPath()) port.systemName = cim_discover.cleanString( _getCimInstanceProperty(portInstance, 'SystemName')) port.name = cim_discover.cleanString( _getCimInstanceProperty(portInstance, 'Name')) port.elementName = cim_discover.cleanString( _getCimInstanceProperty(portInstance, 'ElementName')) permanentAddress = _getCimInstanceProperty(portInstance, 'PermanentAddress') if netutils.isValidMac(permanentAddress): port.permanentAddress = netutils.parseMac(permanentAddress) if port.systemName == unitaryComputerSystem.name and port.permanentAddress: ports.append(port) return ports
def getMAC(shell, int_name): cmdResult = None rawCmdResult = None mac = None entstat = None try: entstat_command = concatenate('entstat ', int_name) logger.debug(concatenate(' Executing command: ', entstat_command)) entstat = shell.execCmd(entstat_command) if entstat != None: m = re.search('Device Type: (.+)', entstat) description = None if (m): description = m.group(1).strip() m = re.search('Hardware Address: ([0-9a-f:]{17})', entstat) rawMac = None if (m): rawMac = m.group(1) mac = netutils.parseMac(rawMac) except: msg = " Failed getting MAC address for interface '%s'" % int_name errobj = errorobject.createError(errorcodes.FAILED_GETTING_INFORMATION, None, msg) logger.reportWarningObject(errobj) logger.debug(msg) return None return mac
def getPhysicalEthAdapters(ethAdaptersList): adapters = [] if ethAdaptersList: for eth in ethAdaptersList: if not eth.isVirtual and eth.interfaceType == ibm_hmc_lib.ETH_ADAPTER and eth.physicalPath and eth.macAddress and netutils.isValidMac(eth.macAddress): eth.macAddress = netutils.parseMac(eth.macAddress) adapters.append(eth) return adapters
def DiscoveryMain(Framework): OSHVResult = ObjectStateHolderVector() snmpMethod = getFrameworkParameter(Framework, 'snmpMethod', SnmpQueries.defaultSnmpMethod) backupSnmpMethod = getFrameworkParameter(Framework, 'backupSnmpMethod', SnmpQueries.defaultBackupSnmpMethod) moonWalkBulkSize = int(getFrameworkParameter(Framework, 'moonWalkBulkSize', SnmpQueries.defaultMoonWalkBulkSize)) moonWalkSleep = long(getFrameworkParameter(Framework, 'moonWalkSleep', SnmpQueries.defaultMoonWalkSleep)) snmpBulkSize = int(getFrameworkParameter(Framework, 'snmpBulkSize', SnmpQueries.defaultSnmpBulkSize)) discoverUnknownIPs = Boolean.parseBoolean(Framework.getParameter('discoverUnknownIPs')) #getting DestinationData from Framework; the method is protected. destination = Framework.getCurrentDestination() discoveredHostIpList = SnmpQueries.getSnmpIpDataOneDestination(snmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, destination) logger.debug('Discover ARP by %s returned %s objects' % (snmpMethod, str(discoveredHostIpList.size()))) if (discoveredHostIpList.size() == 0) and (snmpMethod != backupSnmpMethod): discoveredHostIpList = SnmpQueries.getSnmpIpDataOneDestination(backupSnmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, destination) logger.debug('Discover ARP by %s returned %s objects' % (backupSnmpMethod, str(discoveredHostIpList.size()))) if (discoveredHostIpList.size()==0): Framework.reportWarning('Failed to discover SNMP IP data') return OSHVResult discoveredHostArpList = SnmpQueries.getSnmpArpDataOneDestination(snmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, destination) discoveredHostArpList.addAll(SnmpQueries.getSnmpArpDataOneDestination(snmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, Boolean.TRUE, destination)) if (discoveredHostArpList.size()==0) and (snmpMethod != backupSnmpMethod): discoveredHostArpList = SnmpQueries.getSnmpArpDataOneDestination(backupSnmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, destination) discoveredHostArpList.addAll(SnmpQueries.getSnmpArpDataOneDestination(backupSnmpMethod, snmpBulkSize, moonWalkBulkSize, moonWalkSleep, Boolean.FALSE, Boolean.TRUE, destination)) if (discoveredHostArpList.size()==0): Framework.reportWarning('Failed to discover SNMP ARP data') return OSHVResult networkOSH = None for i in range(discoveredHostArpList.size()): currArp = discoveredHostArpList.get(i) for currIp in discoveredHostIpList: if networkOSH is None: networkOSH = modeling.createNetworkOSH(currIp.netaddr, currIp.netmask) OSHVResult.add(networkOSH) if (currIp.domain == 'unknown') and not discoverUnknownIPs: continue if not netutils.isValidMac(currArp.designatedMacAddress): continue #report start designatedIpNetAddress = IPv4(currArp.designatedIpAddress, currIp.netmask).getFirstIp().toString(); if designatedIpNetAddress == currIp.netaddr: hostOSH = modeling.createHostOSH(currArp.designatedIpAddress) OSHVResult.add(hostOSH) OSHVResult.add(modeling.createLinkOSH('member', networkOSH, hostOSH)) ipOsh = modeling.createIpOSH(currArp.designatedIpAddress, currIp.netmask) OSHVResult.add(ipOsh) OSHVResult.add(modeling.createLinkOSH('member', networkOSH, ipOsh)) ifOsh = modeling.createInterfaceOSH(netutils.parseMac(currArp.designatedMacAddress), hostOSH) OSHVResult.add(ifOsh) OSHVResult.add(modeling.createLinkOSH('containment', ifOsh, ipOsh)) return OSHVResult
def getPhysicalEthAdapters(ethAdaptersList): adapters = [] if ethAdaptersList: for eth in ethAdaptersList: if not eth.isVirtual and eth.interfaceType == ibm_hmc_lib.ETH_ADAPTER and eth.physicalPath and eth.macAddress and netutils.isValidMac( eth.macAddress): eth.macAddress = netutils.parseMac(eth.macAddress) adapters.append(eth) return adapters
def parseDomainConfiguration(output): profile = getProfileInstance(output) if output: for line in re.split('[\r\n]+', output): domid = re.search('<domain type=\'.*?id=\'(\d+)\'>', line) if domid: profile.domainId = toInteger(domid.group(1)) uuid = re.search('<uuid>\s*([\w\-]+)\s*</uuid>', line) if uuid: profile.domainUuid = uuid.group(1).strip() vcpus = re.search('<vcpu>\s*(\d+)\s*</vcpu>', line) if vcpus: profile.vCpus = toInteger(vcpus.group(1)) memory = re.search('<currentMemory>\s*(\d+)\s*</currentMemory>', line) if memory: profile.memory = toLong(memory.group(1)) maxmem = re.search('<memory>\s*(\d+)\s*</memory>', line) if maxmem: profile.maxMemory = toLong(maxmem.group(1)) name = re.search('<name>\s*([\w\-\.]+)\s*</name>', line) if name: profile.domainName = name.group(1) onPowerOff = re.search('<on_poweroff>\s*(\w+)\s*</on_poweroff>', line) if onPowerOff: profile.onPoweroff = onPowerOff.group(1) onReboot = re.search('<on_reboot>\s*(\w+)</on_reboot>', line) if onReboot: profile.onRestart = onReboot.group(1) onCrash = re.search('<on_crash>\s*(\w+)\s*</on_crash>', line) if onCrash: profile.onCrash = onCrash.group(1) # state = re.search('\(state\s+([rbpsc\-]+)', line) # if state: # charState = re.subn('\-', '', state.group(1)) # profile.state = XEN_DOMAIN_STATUS_MAP.get(charState) # disk = re.search('\(uname tap:aio:([\w\-\./\ ]+)', line) # if disk: # profile.disk = disk.group(1).strip() bridge = re.search('\(bridge\s+([\w\-\.]+)', line) if bridge: profile.bridgeNameList.append(bridge.group(1)) mac = re.search('<mac\s+address=\'(..:..:..:..:..:..)', line) if mac and netutils.isValidMac(mac.group(1)): logger.debug('parsed mac is %s', mac.group(1)) parsedMac = netutils.parseMac(mac.group(1)) interface = modeling.NetworkInterface('Xen interface', parsedMac) profile.interfaceList.append(interface) if not profile.name: raise ValueError, 'Failed to parse VM configuration' return profile
def _parseCdpLayer2Output(self, output): results = {} if output: blocks = re.split('----------------------------------------', output) for block in blocks: if not re.search('Device ID', block): continue m = re.search('System Name:\s+([\w\-\.\/]+)', block) system_name = m and m.group(1) if not system_name: continue m = re.search('Interface:\s+([\w\-\.\/]+),', block) local_interface_name = m and m.group(1) if not local_interface_name: continue remote_ips = [] elems = re.findall('IPv[46] Address:(.+)\r?\n?Platform', block, re.DOTALL) if m: for elem in elems: remote_ips = [ip_addr.IPAddress(raw_ip.strip()) for raw_ip in elem.split(',') if ip_addr.isValidIpAddress(raw_ip.strip())] m = re.search('Platform:\s+([\w\-\.]+),', block) platform = m and m.group(1) # ''' # Capability Codes: R - Router, T - Trans-Bridge, B - Source-Route-Bridge # S - Switch, H - Host, I - IGMP, r - Repeater, # V - VoIP-Phone, D - Remotely-Managed-Device, # s - Supports-STP-Dispute # ''' m = re.search('Capabilities:\s+([\w\-\.]+)', block) type = m and m.group(1) mac = None iface_name = None m = re.search('Port\s+ID\s+\(outgoing\s+port\):\s+([\w\-\.\:\/]+)', block)#can be interface name, interface mac. if not m: continue if netutils.isValidMac(m.group(1)): mac = netutils.parseMac(m.group(1)) else: iface_name = m.group(1) m = re.search('Version:(.+)Advertisement', block, re.DOTALL) description = m and m.group(1).strip() try: remote_list = results.get(local_interface_name, []) remote_list.append(layer2.RemotePeer(system_name, iface_name, mac, remote_ips, platform, type, description)) results[local_interface_name] = remote_list except: logger.debugException('') return results
def parseIfconfig(output): ifaceNameToIfaceMacMap = {} if output: for line in re.split('[\r\n]+', output): match = re.match('([\w\.\-]+)\s+.*HWaddr\s+(..:..:..:..:..:..)', line) if match: ifaceName = match.group(1) ifaceMac = match.group(2) if ifaceMac and netutils.isValidMac(ifaceMac): ifaceNameToIfaceMacMap[ifaceName] = netutils.parseMac(ifaceMac) return ifaceNameToIfaceMacMap
def parseIfconfig(output): ifaceNameToIfaceMacMap = {} if output: for line in re.split('[\r\n]+', output): match = re.match('([\w\.\-]+)\s+.*HWaddr\s+(..:..:..:..:..:..)', line) if match: ifaceName = match.group(1) ifaceMac = match.group(2) if ifaceMac and netutils.isValidMac(ifaceMac): ifaceNameToIfaceMacMap[ifaceName] = netutils.parseMac( ifaceMac) return ifaceNameToIfaceMacMap
def parseEthAdapterInformation(output, adaptersDict): if output: entries = re.split('ETHERNET STAT', output) for entrie in entries: ifaceName = re.search('ISTICS \(\s*(\w+)\s*\)', entrie) if not ifaceName: continue ifaceName = ifaceName.group(1).strip() ifaceType = re.search('Device Type:\s+([\w \-\\/]+)', entrie) if not ifaceType: continue ifaceType = ifaceType.group(1).strip() ifaceMac = re.search('Hardware Address:\s+(..:..:..:..:..:..)', entrie) if ifaceMac: ifaceMac = ifaceMac.group(1).strip() adapter = adaptersDict.get(ifaceName) if adapter: ifaceSpeed = re.search('Media Speed Running:\s+(\d+)', entrie) if ifaceSpeed: adapter.speed = int(ifaceSpeed.group(1)) * 1000 if ifaceType.lower() == "shared ethernet adapter": adapter.interfaceType = ibm_hmc_lib.SEA_ADAPTER adapter.isVirtual = 1 elif ifaceType.lower() == "etherchannel": adapter.interfaceType = ibm_hmc_lib.LINK_AGGREGATION_ADAPTER if ifaceMac and netutils.isValidMac(ifaceMac): adapter.macAddress = netutils.parseMac(ifaceMac) else: if re.search('virtual', ifaceType, re.I): adapter.isVirtual = 1 if ifaceMac and netutils.isValidMac(ifaceMac): adapter.macAddress = netutils.parseMac(ifaceMac) adaptersDict[ifaceName] = adapter
def createinterfaces(shell, HostOSH, Framework): mac = None myVec = ObjectStateHolderVector() lines = () # Get a listing of the device names for the network interfaces # prtconf = client.execCmd('lscfg | grep ent') prtconf = shell.execAlternateCmds( '/usr/sbin/lsdev -Cc adapter | egrep ^ent', 'lsdev -Cc adapter | egrep ^ent', 'lsdev -Cc adapter | grep -e ^ent', '/usr/sbin/lsdev -Cc adapter | grep -e ^ent') #prtconf ='ent0 Available 03-08 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)' if prtconf == None: prtconf = '' lines = prtconf.split('\n') interfaces = {} for line in lines: m = re.search('^(\S+)', line) if (m): interfaces[m.group(1)] = 'pending' if (len(interfaces) < 1): logger.debug('did not find any interfaces in prtconf (timeout?)') #fail step return # Get further info on each of the network interfaces for devname in interfaces.keys(): entstat = shell.execCmd( 'entstat ' + devname) #@@CMD_PERMISION shell protocol execution # entstat = simulatecmd('c:/entstat.txt') if entstat == None: entstat = '' m = re.search('Device Type: (.+)', entstat) intdescription = None if (m): intdescription = m.group(1).strip() m = re.search('Hardware Address: ([0-9a-f:]{17})', entstat) rawMac = None if (m): rawMac = m.group(1) mac = netutils.parseMac(rawMac) hostifOSH = modeling.createInterfaceOSH( mac, description=intdescription) hostifOSH.setContainer(HostOSH) myVec.add(hostifOSH) return myVec
def parseEthAdapterInformation(output, adaptersDict): if output: entries = re.split('ETHERNET STAT', output) for entrie in entries: ifaceName = re.search('ISTICS \(\s*(\w+)\s*\)', entrie) if not ifaceName: continue ifaceName = ifaceName.group(1).strip() ifaceType = re.search('Device Type:\s+([\w \-\\/]+)' , entrie) if not ifaceType: continue ifaceType = ifaceType.group(1).strip() ifaceMac = re.search('Hardware Address:\s+(..:..:..:..:..:..)', entrie) if ifaceMac: ifaceMac = ifaceMac.group(1).strip() adapter = adaptersDict.get(ifaceName) if adapter: ifaceSpeed = re.search('Media Speed Running:\s+(\d+)', entrie) if ifaceSpeed: adapter.speed = int(ifaceSpeed.group(1)) * 1000 if ifaceType.lower() == "shared ethernet adapter": adapter.interfaceType = ibm_hmc_lib.SEA_ADAPTER adapter.isVirtual = 1 elif ifaceType.lower() == "etherchannel": adapter.interfaceType = ibm_hmc_lib.LINK_AGGREGATION_ADAPTER if ifaceMac and netutils.isValidMac(ifaceMac): adapter.macAddress = netutils.parseMac(ifaceMac) else: if re.search('virtual', ifaceType, re.I): adapter.isVirtual = 1 if ifaceMac and netutils.isValidMac(ifaceMac): adapter.macAddress = netutils.parseMac(ifaceMac) adaptersDict[ifaceName] = adapter
def createinterfaces(shell,HostOSH,Framework): mac = None myVec = ObjectStateHolderVector() lines = () # Get a listing of the device names for the network interfaces # prtconf = client.execCmd('lscfg | grep ent') prtconf = shell.execAlternateCmds('/usr/sbin/lsdev -Cc adapter | egrep ^ent', 'lsdev -Cc adapter | egrep ^ent', 'lsdev -Cc adapter | grep -e ^ent', '/usr/sbin/lsdev -Cc adapter | grep -e ^ent') #prtconf ='ent0 Available 03-08 2-Port 10/100/1000 Base-TX PCI-X Adapter (14108902)' if prtconf == None: prtconf = '' lines = prtconf.split('\n') interfaces = {} for line in lines: m = re.search('^(\S+)',line) if(m): interfaces[m.group(1)] = 'pending' if(len(interfaces) < 1): logger.debug('did not find any interfaces in prtconf (timeout?)' ) #fail step return # Get further info on each of the network interfaces for devname in interfaces.keys(): entstat = shell.execCmd('entstat ' + devname)#@@CMD_PERMISION shell protocol execution # entstat = simulatecmd('c:/entstat.txt') if entstat == None: entstat = '' m = re.search('Device Type: (.+)', entstat) intdescription = None if(m): intdescription = m.group(1).strip() m = re.search('Hardware Address: ([0-9a-f:]{17})', entstat) rawMac = None if(m): rawMac = m.group(1) mac = netutils.parseMac(rawMac) hostifOSH = modeling.createInterfaceOSH(mac, description=intdescription) hostifOSH.setContainer(HostOSH) myVec.add(hostifOSH) return myVec
def parseMac(data): mac = None matcher = re.search(r"link/ether\s+([0-9a-f:]{17})", data) if matcher is None: matcher = re.search(r"link/infiniband\s+([0-9a-f:]{17})", data) if matcher: mac = matcher.group(1) if netutils.isValidMac(mac): try: mac = netutils.parseMac(mac) except: mac = None else: mac = None return mac
def addOshToVector(self, resultVector): osh = ObjectStateHolder('ms_nlb_cluster') props = self.props osh.setAttribute('vendor', 'microsoft_corp') osh.setAttribute('cluster_ip_address', props['ClusterIPAddress']) osh.setAttribute('cluster_network_mask', props['ClusterNetworkMask']) osh.setAttribute('mcast_ip_address', props['McastIPAddress']) osh.setAttribute('cluster_domain_name', props['ClusterName']) #have to be transformed as MAC address clusterNetworkAddress = props['ClusterNetworkAddress'] if netutils.isValidMac(clusterNetworkAddress): clusterNetworkAddress = netutils.parseMac(clusterNetworkAddress) else: msg = 'Invalid network address %s' % str(clusterNetworkAddress) logger.reportWarning('Invalid network address') logger.warn(msg) osh.setAttribute('cluster_network_address', clusterNetworkAddress) osh.setBoolAttribute('ip_to_mac_enable', resolveBoolean(props['IPToMACEnable'])) osh.setBoolAttribute('multicast_support_enable', resolveBoolean(props['MulticastSupportEnable'])) osh.setBoolAttribute('igmp_support', resolveBoolean(props['IGMPSupport'])) osh.setBoolAttribute('remote_control_enabled', resolveBoolean(props.get('RemoteControlEnabled'))) osh.setAttribute('data_name', 'MS NLB Cluster') resultVector.add(osh) clusteredServer = modeling.createCompleteHostOSH( 'cluster_resource_group', clusterNetworkAddress) clusteredServer.setStringAttribute('name', props['ClusterName']) resultVector.add(clusteredServer) resultVector.add( modeling.createLinkOSH('contained', osh, clusteredServer)) resultVector.add(self.clusterIpOSH) resultVector.add( modeling.createLinkOSH('contained', clusteredServer, self.clusterIpOSH)) resultVector.add(self.hostOSH) resultVector.add( modeling.createLinkOSH('contained', self.hostOSH, self.clusterIpOSH)) self.config.setContainer(osh) resultVector.add(self.config) self.nlbClusterOSH = osh
def __discoverClimInterface(self, climName): """ @types: string -> None @raise ValueError: when command "gtacl -cv "climcmd %s /sbin/ifconfig -a % <clim_name>" gives no output or fails """ cmd = "climcmd %s /sbin/ifconfig -a" % climName cmdOutput = self.__shell.execCmd('gtacl -cv "%s"' % cmd) if not cmdOutput or self.__shell.getLastCmdReturnCode() != 0: raise ValueError('Failed to get CLIM') (header, interfaceData) = cmdOutput.split(cmd) if header and interfaceData: interfacesByName = {} matches = ShellDiscoverer.__INTERFACE_REGEXP.findall(interfaceData) for match in matches: name = match[0] uniqueName = "%s.%s" % (climName, match[0]) mac= match[1] if netutils.isValidMac(mac): interface = Interface(netutils.parseMac(mac), uniqueName) parentInterfaceName = self.__getParentInterfaceName(name) if parentInterfaceName and interfacesByName.has_key(parentInterfaceName): parentInterface = interfacesByName[parentInterfaceName] aliasRole = AliasRole() aliasRole.parentInterface = parentInterface interface._addRole(aliasRole) self.__networking.addInterface(interface) interfacesByName[name] = interface matches = ShellDiscoverer.__INTERFACE_AND_IP_REGEXP.findall(interfaceData) for match in matches: name = match[0] ip = match[2] netmask = match[4] if netutils.isValidIp(ip) and netutils.isValidIp(netmask): if interfacesByName.has_key(name): interface = interfacesByName[name] self.__networking.addIpAndNetwork(ip, netmask, interface.name) else: self.__networking.addIpAndNetwork(ip, netmask) else: logger.warn('Unrecognized output') logger.reportWarning("Failed to discover CLIM network interfaces")
def discoverAvailableInterfaces(self): ''' Discover details of all available network interfaces -> list(networking.Interface)''' logger.debug('Discover details of interfaces') if not self.__availableInterfaces: for interface in self._discoverInterfacesList(): try: info = self.__getInterfaceDetails(interface.name) if netutils.isValidMac(info.mac): interface.mac = netutils.parseMac(info.mac) interface.description = info.description interface.speed = info.speed except Exception, e: logger.warn(str(e)) # do not add SEA adapters in the list if self._isSharedAdapter(interface): self.__seaInterfaces.append(interface) else: self.__availableInterfaces.append(interface)
def __parseInterfaceInEntstatOutput(self, devname, output): 'str, str -> networking.Interface or None' #type m = re.search('Device Type:\s+(.+)' , output) type = m and m.group(1).strip() #mac m = re.search('Hardware Address: ([0-9a-f:]{17})', output) rawMac = m and m.group(1) if type and rawMac: mac = None index = self._getDevNameAndIndex(devname)[1] if netutils.isValidMac(rawMac): mac = netutils.parseMac(rawMac) else: logger.warn("Mac value for interface %s is not valid '%s'" % (devname, rawMac)) mac = index m = re.search('Media Speed Running:\s+(\d+)', output) speed = m and long(m.group(1)) * 1000 return Interface(mac = mac, description = type, name = devname, index = index, speed = speed)
def parseNetstatOutput(netstatOutput): # There are more names than macs, names can appear several times netstatRecordsByName = {} results = re.findall(r"\n(\S+).+SP[A-Za-z]*\s+([0-9a-f:]+)", netstatOutput) resultsIpv6 = re.findall(r"\n(\S+)\s+([\da-f:]+)\s\s(?!other)", netstatOutput) results.extend(resultsIpv6) for row in results: interfaceName = row[0] if not netstatRecordsByName.has_key(interfaceName): macRaw = row[1] try: mac = netutils.parseMac(macRaw) netstatRecord = _NetstatRecord() netstatRecord.mac = mac netstatRecord.interfaceName = interfaceName netstatRecordsByName[interfaceName] = netstatRecord except: logger.warn("Failed parsing MAC address '%s'" % macRaw) return netstatRecordsByName
def _findHostKeyAndIps(self): if self.networkConnectionSection is not None: macs = [] connections = self.networkConnectionSection.getNetworkConnection() for connection in connections: isConnected = connection.isIsConnected() if isConnected: rawMac = connection.getMACAddress() if netutils.isValidMac(rawMac): mac = netutils.parseMac(rawMac) macs.append(mac) ip = connection.getIpAddress() if ip is not None and netutils.isValidIp(ip) and not netutils.isLocalIp(ip): self._ips.append(ip) self._validMacs = macs macs.sort() if macs: self._hostKey = macs[0]
def parseEthAdapterVpdInformation(output, ethAdaptersDict): if output: match = re.match('\s*(e[nt]+\d+).*?Network Address[\s.]+(\w+).*?Displayable Message[\s.]+([\w /\-\(\)]*).*?Hardware Location Code[\s.]+([\w\-\.]*)', output, re.DOTALL) if match: ifaceName = match.group(1) iface = ethAdaptersDict.get(ifaceName) if iface: ifaceMac = match.group(2) if ifaceMac and netutils.isValidMac(ifaceMac): iface.macAddress = netutils.parseMac(ifaceMac) ifaceDescription = match.group(3) if ifaceDescription: iface.description = ifaceDescription if re.search('virtual', ifaceDescription, re.I): iface.isVirtual = 1 physPath = match.group(4) if physPath: iface.physicalPath = physPath
def _findHostKeyAndIps(self): if self.networkConnectionSection is not None: macs = [] connections = self.networkConnectionSection.getNetworkConnection() for connection in connections: isConnected = connection.isIsConnected() if isConnected: rawMac = connection.getMACAddress() if netutils.isValidMac(rawMac): mac = netutils.parseMac(rawMac) macs.append(mac) ip = connection.getIpAddress() if ip is not None and netutils.isValidIp( ip) and not netutils.isLocalIp(ip): self._ips.append(ip) self._validMacs = macs macs.sort() if macs: self._hostKey = macs[0]
def parseEthAdapterVpdInformation(output, ethAdaptersDict): if output: match = re.match( '\s*(e[nt]+\d+).*?Network Address[\s.]+(\w+).*?Displayable Message[\s.]+([\w /\-\(\)]*).*?Hardware Location Code[\s.]+([\w\-\.]*)', output, re.DOTALL) if match: ifaceName = match.group(1) iface = ethAdaptersDict.get(ifaceName) if iface: ifaceMac = match.group(2) if ifaceMac and netutils.isValidMac(ifaceMac): iface.macAddress = netutils.parseMac(ifaceMac) ifaceDescription = match.group(3) if ifaceDescription: iface.description = ifaceDescription if re.search('virtual', ifaceDescription, re.I): iface.isVirtual = 1 physPath = match.group(4) if physPath: iface.physicalPath = physPath
def __discoverRegularInterfaces(self): """ @raise ValueError: when command "gtacl -p scf info lif '$zzlan.*'" gives no output of fails """ interfacesData = self.__shell.execCmd("gtacl -p scf info lif '$zzlan.*'") if not interfacesData or self.__shell.getLastCmdReturnCode() != 0: raise ValueError("Failed to discover regular interfaces") lines = [line.strip() for line in interfacesData.split('\n') if line and re.match(r"\$ZZLAN", line.strip(), re.I)] for line in lines: interfaceData = line.split() # Four means the number of groups in valid output string describing interface if len(interfaceData) != 4: logger.warn("Output format is not supported: %s" % line) continue if interfaceData[3].lower() != 'ethernet': logger.info("Interface type %s was skipped." % interfaceData[3]) continue mac = interfaceData[2] if netutils.isValidMac(mac): mac = netutils.parseMac(mac) else: logger.warn("Interface is skipped -- MAC address is invalid: %s" % mac) continue m = re.match(r"\$ZZLAN\.(.*)", interfaceData[0], re.I) if m: name = m.group(1) else: logger.warn("Interface is skipped -- name was not found in line: %s" % line) continue description = interfaceData[1] interface = Interface(mac, name, description) self.__getNetworking().addInterface(interface)
def addOshToVector(self, resultVector): osh = ObjectStateHolder('ms_nlb_cluster') props = self.props osh.setAttribute('vendor', 'microsoft_corp') osh.setAttribute('cluster_ip_address', props['ClusterIPAddress']) osh.setAttribute('cluster_network_mask', props['ClusterNetworkMask']) osh.setAttribute('mcast_ip_address', props['McastIPAddress']) osh.setAttribute('cluster_domain_name', props['ClusterName']) #have to be transformed as MAC address clusterNetworkAddress = props['ClusterNetworkAddress'] if netutils.isValidMac(clusterNetworkAddress): clusterNetworkAddress = netutils.parseMac(clusterNetworkAddress) else: msg = 'Invalid network address %s' % str(clusterNetworkAddress) logger.reportWarning('Invalid network address') logger.warn(msg) osh.setAttribute('cluster_network_address', clusterNetworkAddress) osh.setBoolAttribute('ip_to_mac_enable', resolveBoolean(props['IPToMACEnable'])) osh.setBoolAttribute('multicast_support_enable', resolveBoolean(props['MulticastSupportEnable'])) osh.setBoolAttribute('igmp_support', resolveBoolean(props['IGMPSupport'])) osh.setBoolAttribute('remote_control_enabled', resolveBoolean(props.get('RemoteControlEnabled'))) osh.setAttribute('data_name', 'MS NLB Cluster') resultVector.add(osh) clusteredServer = modeling.createCompleteHostOSH('cluster_resource_group', clusterNetworkAddress) clusteredServer.setStringAttribute('name', props['ClusterName']) resultVector.add(clusteredServer) resultVector.add(modeling.createLinkOSH('contained', osh, clusteredServer)) resultVector.add(self.clusterIpOSH) resultVector.add(modeling.createLinkOSH('contained', clusteredServer, self.clusterIpOSH)) resultVector.add(self.hostOSH) resultVector.add(modeling.createLinkOSH('contained', self.hostOSH, self.clusterIpOSH)) self.config.setContainer(osh) resultVector.add(self.config) self.nlbClusterOSH = osh
def _parseEth(self, buffer): """ Created NIC DO from the output of the lshwres -r virtualio --rsubtype eth --level lpar -m '<Managed System Name>' @param buffer: command output @type buffer: String @return: instance of NetworkInterfaceDo """ if buffer: propertiesDict = self.buildPropertiesDict(buffer) vEth = ibm_hmc_lib.LparNetworkInterface() vEth.macAddress = propertiesDict.get('mac_addr') if not vEth.macAddress and not netutils.isValidMac(vEth.macAddress): raise ValueError, "Failed parsing MAC addres of Virtual Ethernet Card from buffer: %s" % buffer vEth.macAddress = netutils.parseMac(vEth.macAddress) vEth.portVlanId = ibm_hmc_lib.toInteger(propertiesDict.get('port_vlan_id')) vEth.lparName = propertiesDict.get('lpar_name') vEth.slotNum = propertiesDict.get('slot_num') if not vEth.lparName: raise ValueError, "Failed parsing LPar Name for Virtual Ethernet Card from buffer: %s" % buffer vEth.lparId = ibm_hmc_lib.toInteger(propertiesDict.get('lpar_id')) vEth.isVirtual = 1 return vEth
def _parseIfConfigCmd(ifconfig, regLinuxMac, regLinuxIpNetMask): '@types: ... -> iterable[tuple[Interface, list[networking.Ip]]]' ipsByIfaceName = collections.defaultdict(list) interfaces = [] if_macs = re.compile(regLinuxMac).findall(ifconfig) for name, mac in if_macs: if netutils.isValidMac(mac): try: mac = netutils.parseMac(mac) except: mac = None else: mac = None if mac: interfaces.append(networking.Interface(mac, name)) ipNetmasks = re.compile(regLinuxIpNetMask).findall(ifconfig) if ipNetmasks: for name, _, ip, _, netmask in ipNetmasks: ipsByIfaceName[name].append(networking.Ip(ip, netmask)) return tuple((interface, ipsByIfaceName.get(interface.name, ())) for interface in interfaces)
def __parseInterfaceInEntstatOutput(self, devname, output): 'str, str -> networking.Interface or None' #type m = re.search('Device Type:\s+(.+)', output) type = m and m.group(1).strip() #mac m = re.search('Hardware Address: ([0-9a-f:]{17})', output) rawMac = m and m.group(1) if type and rawMac: mac = None index = self._getDevNameAndIndex(devname)[1] if netutils.isValidMac(rawMac): mac = netutils.parseMac(rawMac) else: logger.warn("Mac value for interface %s is not valid '%s'" % (devname, rawMac)) mac = index m = re.search('Media Speed Running:\s+(\d+)', output) speed = m and long(m.group(1)) * 1000 return Interface(mac=mac, description=type, name=devname, index=index, speed=speed)
def __discoverInterfacesInfo(self): ips = {} masks = {} interfacesDict = {} interfacesList = [] # Separate out the entries from the network information class entries for entry in self.__ipInfo: interfaceDescr = entry.interfaceDescr interfacesDict[interfaceDescr] = entry.interfaceName ip = entry.ip mask = entry.mask if netutils.isValidIp(ip) and not netutils.isLocalIp(ip): inIPs = ips.get(interfaceDescr, []) if not inIPs: ips[interfaceDescr] = inIPs inIPs.append(ip) inMasks = masks.get(interfaceDescr, []) if not inMasks: masks[interfaceDescr] = inMasks inMasks.append(mask) maclist = self.__getMACsData() if maclist == None: logger.warn('Invalid MAC info, skipping.') else: for interfaceDesc in maclist.keys(): mac = maclist.get(interfaceDesc) if netutils.isValidMac(mac): logger.debug("Interface: %s, MAC: %s" % (interfaceDesc, mac)) mac = netutils.parseMac(mac) interface = NetworkInterface(interfaceDesc, mac, ips.get(interfaceDesc), masks.get(interfaceDesc), -1, 0) interface.name = interfacesDict.get(interfaceDescr) or None; interfacesList.append(interface) return interfacesList
def getNodes(localDbClient, netDeviceOSHV, portOSHV, allowDnsLookup, queryChunkSize, localFramework): try: resultVector = ObjectStateHolderVector() ipAddrList = macAddrList = [] nodeOshDict = {} # {'NodeName':NodeOSH} nodeResultSetData = {} #{'NodeName':[NodeData]} # ## Get total number of nodes in the database numNodes = 0 nodeCountQuery = 'SELECT COUNT(1) FROM lmsdatagrp.End_Hosts' nodeCountResultSet = ciscoworks_utils.doQuery(localDbClient, nodeCountQuery) ## Return if query returns no results if nodeCountResultSet == None: logger.warn('[' + SCRIPT_NAME + ':getNodes] No Nodes found') return None ## We have query results! while nodeCountResultSet.next(): numNodes = int(ciscoworks_utils.getStringFromResultSet(nodeCountResultSet, 1)) ## Determine chunk count ciscoworks_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getNodes] Got <%s> nodes...' % numNodes) numChunks = int(numNodes / queryChunkSize) + 1 ciscoworks_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getNodes] Got <%s> chunks...' % numChunks) for chunkIndex in range(0, numChunks): queryStartRow = chunkIndex*queryChunkSize if queryStartRow == 0: queryStartRow = 1 nodeQuery = '''SELECT TOP %s START AT %s HostName, DeviceName, Device, MACAddress, IPAddress, SubnetMask, Port, PortName, VLAN, VlanId, associatedRouters FROM lmsdatagrp.End_Hosts WHERE MACAddress IS NOT NULL AND NOT MACAddress='' ''' % (queryChunkSize, queryStartRow) nodeResultSet = ciscoworks_utils.doQuery(localDbClient, nodeQuery) ## Return if query returns no results if nodeResultSet == None: logger.warn('No Nodes found') return None ## We have query results! while nodeResultSet.next(): nodeOSH = ipOSH = None ## Get values from result set nodeName = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 1) netDeviceName = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 2) netDeviceIP = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 3) nodeMacAddress = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 4) ipAddress = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 5) nodeSubnetMask = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 6) portName = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 7) portDesc = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 8) vlanName = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 9) vlanID = ciscoworks_utils.getStringFromResultSet(nodeResultSet, 10) ## Build Node OSH if ipAddress and netutils.isValidIp(ipAddress): end_device_ip_address = ipAddress elif nodeName and netutils.isValidIp(nodeName): end_device_ip_address = nodeName else: end_device_ip_address = None (nodeOSH, interfaceOSH, ipOSH) = processNodeInfo(end_device_ip_address, nodeMacAddress, nodeName, nodeSubnetMask, ipAddrList, macAddrList, nodeOshDict, allowDnsLookup) if nodeOSH: resultVector.add(nodeOSH) else: ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getNodes] Unable to build CI for Node <%s> with MAC address <%s>' % (nodeName, nodeMacAddress)) continue if interfaceOSH: resultVector.add(interfaceOSH) if ipOSH: resultVector.add(ipOSH) resultVector.add(modeling.createLinkOSH('containment', nodeOSH, ipOSH)) ## Build Net Device OSH if netDeviceIP and netutils.isValidIp(netDeviceIP): net_device_ip_address = netDeviceIP elif netDeviceName and netutils.isValidIp(netDeviceName): net_device_ip_address = netDeviceName else: net_device_ip_address = None (netDeviceOSH, netDeviceIpOSH) = processNetDeviceInfo(net_device_ip_address, netDeviceName, ipAddrList, netDeviceOSHV, allowDnsLookup) netDeviceCmdbID = None ## Add Net Device to OSHV only if it is a new one if netDeviceOSH: ## Check if this NetDevice is from the CMDB try: netDeviceCmdbID = netDeviceOSH.getCmdbId().toString() except: ## An exception will be thrown for all Net Device OSHs that were not already in the UCMDB, ignore it pass if not netDeviceCmdbID: resultVector.add(netDeviceOSH) else: ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getNodes] Unable to build CI for Net Device <%s> with IP <%s>' % (netDeviceName, netDeviceIP)) continue if netDeviceIpOSH and netDeviceOSH: resultVector.add(netDeviceIpOSH) resultVector.add(modeling.createLinkOSH('containment', netDeviceOSH, netDeviceIpOSH)) #report Layer 2 topology end_node_mac_address = None if nodeMacAddress and netutils.isValidMac(nodeMacAddress): end_node_mac_address = netutils.parseMac(nodeMacAddress) resultVector.addAll(build_layer2_connection(netDeviceOSH, portName, net_device_ip_address, nodeOSH, end_node_mac_address, interfaceOSH)) ## Build PORT and VLAN CIs if netDeviceCmdbID and netDeviceOSH and netutils.isValidMac(nodeMacAddress): portOSH = processPortInfo(portName, portDesc, vlanName, vlanID, portOSHV, netutils.parseMac(nodeMacAddress), netDeviceCmdbID, netDeviceName, netDeviceOSH) if portOSH: resultVector.add(portOSH) nodeResultSet.close() ## Send results to server localFramework.sendObjects(resultVector) localFramework.flushObjects() resultVector.clear() return resultVector except: excInfo = logger.prepareJythonStackTrace('') logger.warn('[' + SCRIPT_NAME + ':getNodes] Exception: <%s>' % excInfo) pass
def _parseCdpLayer2Output(self, output): results = {} if output: blocks = re.split('----------------------------------------', output) for block in blocks: if not re.search('Device ID', block): continue m = re.search('System Name:\s+([\w\-\.\/]+)', block) system_name = m and m.group(1) if not system_name: continue m = re.search('Interface:\s+([\w\-\.\/]+),', block) local_interface_name = m and m.group(1) if not local_interface_name: continue remote_ips = [] elems = re.findall('IPv[46] Address:(.+)\r?\n?Platform', block, re.DOTALL) if m: for elem in elems: remote_ips = [ ip_addr.IPAddress(raw_ip.strip()) for raw_ip in elem.split(',') if ip_addr.isValidIpAddress(raw_ip.strip()) ] m = re.search('Platform:\s+([\w\-\.]+),', block) platform = m and m.group(1) # ''' # Capability Codes: R - Router, T - Trans-Bridge, B - Source-Route-Bridge # S - Switch, H - Host, I - IGMP, r - Repeater, # V - VoIP-Phone, D - Remotely-Managed-Device, # s - Supports-STP-Dispute # ''' m = re.search('Capabilities:\s+([\w\-\.]+)', block) type = m and m.group(1) mac = None iface_name = None m = re.search( 'Port\s+ID\s+\(outgoing\s+port\):\s+([\w\-\.\:\/]+)', block) #can be interface name, interface mac. if not m: continue if netutils.isValidMac(m.group(1)): mac = netutils.parseMac(m.group(1)) else: iface_name = m.group(1) m = re.search('Version:(.+)Advertisement', block, re.DOTALL) description = m and m.group(1).strip() try: remote_list = results.get(local_interface_name, []) remote_list.append( layer2.RemotePeer(system_name, iface_name, mac, remote_ips, platform, type, description)) results[local_interface_name] = remote_list except: logger.debugException('') return results
def __parseLanscanMacString(self, macStr): 'str -> str or None' mac = macStr[2:] if netutils.isValidMac(mac): return netutils.parseMac(mac)
def getNetDevicePortsAndVlans(localDbClient, portVlanIdMap, netDeviceID, netDeviceElementID, netDeviceName, netDeviceOSH): try: returnOSHV = ObjectStateHolderVector() netDevicePortVlanQuery = '''SELECT phyPort.PhysicalPortID, phyPort.SNMPPhysicalIndex, phyPort.ParentRelPos, port.PORT_NAME, port.PORT_DESC, port.PORT_DUPLEX_MODE, port.PORT_TYPE, port.PORT_SPEED, port.VLAN_NAME, port.VLANID, port.Port_Admin_Status, port.Port_Oper_Status, interface.EndpointID, interface.Description, interface.Alias, interface.MediaAccessAddress FROM lmsdatagrp.PORT_INVENTORY port JOIN dba.PhysicalPort phyPort ON port.PORT_NAME=phyPort.PortName JOIN dba.IFEntryEndpoint interface ON port.PORT_NAME=interface.EndpointName WHERE phyPort.NetworkElementID=%s AND interface.NetworkElementID=%s AND port.DEVICE_ID=%s AND phyPort.PortName=port.PORT_NAME''' % ( netDeviceElementID, netDeviceElementID, netDeviceID) netDevicePortVlanResultSet = ciscoworks_utils.doQuery( localDbClient, netDevicePortVlanQuery) ## Return if query returns no results if netDevicePortVlanResultSet == None: logger.info( '[' + SCRIPT_NAME + ':getNetDevicePortsAndVlans] No Ports or VLANs found for Net Device <%s>' % netDeviceName) return None ## We have query results! while netDevicePortVlanResultSet.next(): portID = ciscoworks_utils.getStringFromResultSet( netDevicePortVlanResultSet, 1) portIndex = ciscoworks_utils.getStringFromResultSet( netDevicePortVlanResultSet, 2) portSlot = ciscoworks_utils.getStringFromResultSet( netDevicePortVlanResultSet, 3) portName = ciscoworks_utils.getStringFromResultSet( netDevicePortVlanResultSet, 4) portDesc = ciscoworks_utils.getStringFromResultSet( netDevicePortVlanResultSet, 5) portDuplexMode = ciscoworks_utils.getStringFromResultSet( netDevicePortVlanResultSet, 6) interfaceType = ciscoworks_utils.getStringFromResultSet( netDevicePortVlanResultSet, 7) interfaceSpeed = ciscoworks_utils.getStringFromResultSet( netDevicePortVlanResultSet, 8) vlanName = ciscoworks_utils.getStringFromResultSet( netDevicePortVlanResultSet, 9) vlanID = ciscoworks_utils.getStringFromResultSet( netDevicePortVlanResultSet, 10) adminStatus = ciscoworks_utils.getStringFromResultSet( netDevicePortVlanResultSet, 11) operStatus = ciscoworks_utils.getStringFromResultSet( netDevicePortVlanResultSet, 12) interfaceIndex = ciscoworks_utils.getStringFromResultSet( netDevicePortVlanResultSet, 13) interfaceDesc = ciscoworks_utils.getStringFromResultSet( netDevicePortVlanResultSet, 14) interfaceAlias = ciscoworks_utils.getStringFromResultSet( netDevicePortVlanResultSet, 15) interfaceMAC = ciscoworks_utils.getStringFromResultSet( netDevicePortVlanResultSet, 16) if not portID or type(eval(portID)) != type(1): logger.debug( '[' + SCRIPT_NAME + ':getNetDevicePortsAndVlans] Invalid portID found for Port <%s> on Net Device <%s>! Skipping...' % (portName, netDeviceName)) continue ciscoworks_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getNetDevicePortsAndVlans] Got port <%s> for Net Device <%s> with ID <%s>' % (portName, netDeviceName, netDeviceElementID)) ## Build PHYSICAL PORT OSH portOSH = ObjectStateHolder('physical_port') ciscoworks_utils.populateOSH( portOSH, { 'port_index': int(portIndex), 'name': portName, 'port_displayName': portName, 'description': portDesc, 'port_vlan': vlanID, 'port_slot': portSlot }) ## Map duplex settings value to UCMDB enum portDuplexModeMap = { 'auto-duplex': 'auto-negotiated', 'full-duplex': 'full', 'half-duplex': 'half' } if portDuplexMode and portDuplexMode in portDuplexModeMap.keys(): ciscoworks_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getNetDevicePortsAndVlans] Setting duplex mode <%s> for port <%s>' % (portDuplexModeMap[portDuplexMode], portName)) portOSH.setStringAttribute('duplex_setting', portDuplexModeMap[portDuplexMode]) portOSH.setContainer(netDeviceOSH) returnOSHV.add(portOSH) ## Build INTERFACE OSH if interfaceMAC: macAddress = netutils.parseMac(interfaceMAC) if netutils.isValidMac(macAddress): ciscoworks_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getNetDevicePortsAndVlans] Got interface <%s> for Net Device <%s> with ID <%s>' % (macAddress, netDeviceName, netDeviceElementID)) interfaceOSH = modeling.createInterfaceOSH( macAddress, netDeviceOSH, interfaceDesc, interfaceIndex, interfaceType, adminStatus, operStatus, eval(interfaceSpeed), None, interfaceAlias) returnOSHV.add(interfaceOSH) returnOSHV.add( modeling.createLinkOSH('realization', portOSH, interfaceOSH)) ## Add this port to the port-VLAN map if vlanName and vlanName != 'N/A' and vlanID and vlanID != '-1' and type( eval(vlanID)) == type(1): portVlanIdMapKey = '%s:;:%s' % (vlanName, vlanID) if portVlanIdMapKey in portVlanIdMap.keys(): ciscoworks_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getNetDevicePortsAndVlans] Adding port <%s> to existing VLAN <%s:%s>' % (portName, vlanName, vlanID)) portVlanIdMap[portVlanIdMapKey].append(portOSH) else: portVlanIdMap[portVlanIdMapKey] = [portOSH] ciscoworks_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getNetDevicePortsAndVlans] Adding port <%s> to new VLAN <%s:%s>' % (portName, vlanName, vlanID)) netDevicePortVlanResultSet.close() return returnOSHV except: excInfo = logger.prepareJythonStackTrace('') logger.warn('[' + SCRIPT_NAME + ':getNetDevicePortsAndVlans] Exception: <%s>' % excInfo) pass
def setMac(self, mac): if mac and netutils.isValidMac(mac): self.__mac = netutils.parseMac(mac) else: raise ValueError("invalid mac")
def _parseServerData(self, output): ''' Parses command smcli "lssys -l -t Server" output @param output: string @return: list of ibm_fsm.Host instances ''' result = [] for chunk in re.split('[\r\n]{4}', output): if not (chunk and chunk.strip()): continue elems = parse_result_as_dict(chunk) if (elems.get('MachineType') and str(elems.get('MachineType')) in ('7863', '7955', '7917') ) \ or (elems.get('MgmtHwType') and \ elems.get('MgmtHwType') in ['BaseboardManagementController']): logger.debug('Skipping management node') continue hostDo = ibm_fsm.Host() host_name = elems.get('HostName', '') #logger.debug('Got hostname %s' % host_name) if host_name: if isinstance(host_name, list): host_name = host_name[0] if not netutils.isValidIp(host_name): if host_name.find('.') == -1: hostDo.hostname = host_name else: hostDo.hostname = host_name[:host_name.find('.')] hostDo.domain = host_name[host_name.find('.') + 1:] #logger.debug('Is virtual %s' % elems.get('Virtual')) hostDo.is_virtual = elems.get('Virtual') and elems.get( 'Virtual').strip() == 'true' or False hostDo.serial = elems.get('SerialNumber') hostDo.displayName = elems.get('DisplayName') #logger.debug('Serial %s' % elems.get('SerialNumber')) vendor = elems.get('Manufacturer') if vendor: if vendor.find('(') != -1: vendor = vendor[:vendor.find('(')] hostDo.vendor = vendor hostDo.model = elems.get('Model') hostDo.referenced_chassis = elems.get('ChassisName') hostDo.architecture = elems.get('Architecture') hostDo.server_type = elems.get('ServerType') hostDo.sys_uuid = elems.get('SystemBoardUUID') hostDo.name = elems.get('DisplayName') hostDo.vm_id = elems.get('VMID') #process IP addresses ips_raw = elems.get('IPv4Address') if ips_raw: candidates = [] if not isinstance(ips_raw, list): candidates.append(ips_raw) else: candidates = ips_raw for ip_str in candidates: if ip_str and ip_addr.isValidIpAddressNotZero(ip_str): hostDo.ipList.append(ip_addr.IPAddress(ip_str)) #process MACs macs_raw = elems.get('MACAddress') if macs_raw: candidates = [] if not isinstance(macs_raw, list): candidates.append(macs_raw) else: candidates = macs_raw for mac in candidates: if netutils.isValidMac(mac): hostDo.macs.append(netutils.parseMac(mac)) result.append(hostDo) return result
def parseArpOutput(arpOutput, ip): matcher = re.search(r"\(%s\) at ([0-9a-f:]{11,17})" % re.escape(ip), arpOutput) if matcher: return netutils.parseMac(matcher.group(1))
def processNodeInfo(ipAddress, macAddress, nodeName, subnetMask, ipAddressList, macAddressList, nodeOshDict, allowDnsLookup): try: nodeOSH = interfaceOSH = ipOSH = None ## Try and get a DNS name for this node nodeDnsName = None if allowDnsLookup and ipAddress and netutils.isValidIp(ipAddress): nodeDnsName = netutils.getHostName(ipAddress) ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processNodeInfo] Got DNS name <%s> for Node <%s> using IP <%s>' % (nodeDnsName, nodeName, ipAddress)) if nodeDnsName: nodeName = nodeDnsName ## Discard IP if this is a duplicate if ipAddress and netutils.isValidIp(ipAddress) and ipAddress in ipAddressList: logger.debug('Ignoring duplicate IP <%s> on Node <%s>...' % (ipAddress, nodeName)) ipAddress = None else: ipAddressList.append(ipAddress) ## Set the real name of the netDevice nodeRealName = nodeName if nodeName and netutils.isValidIp(nodeName): nodeRealName = '' ## Build a host key and create OSHs ## Check for and clean up MAC macAddy = None if macAddress: macAddy = netutils.parseMac(macAddress) ## Check for duplicate MAC addresses if macAddy in macAddressList: logger.debug('Ignoring duplicate MAC Address <%s> on Node <%s>...' % (macAddy, nodeName)) macAddy = None else: macAddressList.append(macAddy) if netutils.isValidMac(macAddy): ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processNodeInfo] Got MAC Address <%s> for Node <%s>' % (macAddy, nodeName)) nodeOSH = modeling.createCompleteHostOSH('node', macAddy, None, nodeName) interfaceOSH = modeling.createInterfaceOSH(macAddy, nodeOSH) else: ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processNodeInfo] Got invalid MAC Address <%s> for Node <%s>' % (macAddress, nodeName)) macAddy = None ## Check for a valid IP if ipAddress and netutils.isValidIp(ipAddress): subnetMask = None if subnetMask: subnetMask = netutils.parseNetMask(subnetMask) ipOSH = modeling.createIpOSH(ipAddress, subnetMask, ipAddress, None) ## Use IP as a host key if a MAC is not available if not macAddy: nodeOSH = modeling.createHostOSH(ipAddress, 'node', None, nodeRealName) else: logger.debug('IP address not available for Node <%s> with MAC address <%s>' % (nodeName, macAddress)) if not nodeOSH: logger.debug('Ignoring Node <%s> because a valid IP/MAC address was not found for it...' % nodeName) return (nodeOSH, interfaceOSH, ipOSH) except: excInfo = logger.prepareJythonStackTrace('') logger.warn('[' + SCRIPT_NAME + ':processNodeInfo] Exception: <%s>' % excInfo) pass
def parseInterfaces(self, output): """ This function performs parsing of interface related part from lsconfig -n command @param output: lsconfig -n command output @type output: string @return: nwtworking information @rtype: UnixNetworking class instance """ elems = re.split('(\n\s*eth)', output) #strip irrelevant part elems = elems and elems[1:] chunks = map(lambda x: ''.join(x), izip(elems[::2], elems[1::2])) fsm_networking = networking.UnixNetworking() for chunk in chunks: iface = networking.Interface() m = re.search('(eth\d+)', chunk) iface.name = m and m.group(1) m = re.search('mac_address_\w+:\s+([\w:]+)', chunk) mac = m and m.group(1) if netutils.isValidMac(mac): iface.mac = netutils.parseMac(mac) if not (iface.name and iface.mac): logger.debug( 'Neither name nor MAC is found for interface chunk "%s". Skipping' % chunk) continue # m = re.search('duplex_\w+:\s+(\w+)', chunk) # duplex = m and m.group(1) m = re.search('ipv4dhcp_\w+:\s+(\w+)', chunk) is_dhcpv4 = m and m.group(1) m = re.search('ipv6dhcp_\w+:\s+(\w+)', chunk) is_dhcpv6 = m and m.group(1) m = re.search('media_speed_\w+:\s+(\d+)\s+(\w+)', chunk) iface.speed = m and m.group(1) if m.group(2) and m.group(2).lower() != 'mbps': iface.speed = iface.speed * 1024 fsm_networking.addInterface(iface) #IP related part m = re.search('ipv4addr_\w+:\s+([\d\.]+)', chunk) ipv4 = m and m.group(1) m = re.search('ipv4netmask_\w+:\s+([\d\.]+)', chunk) maskv4 = m and m.group(1) if ip_addr.isValidIpAddressNotZero(ipv4): fsm_networking.addIpAndNetwork(ipv4, maskv4, iface.name, is_dhcpv4) else: logger.debug('IP %s is invalid skipping.' % ipv4) m = re.search('ipv6addr_\w+:\s+(.+)', chunk) if m: for ip in m.group(1).split(','): ipv6 = ip mask = None if ip.find('/') != -1: elems = ip.split('/') ipv6 = elems[0] mask = elems[1] if ip_addr.isValidIpAddressNotZero(ipv6): fsm_networking.addIpAndNetwork(ipv6, mask, iface.name, is_dhcpv6) else: logger.debug('IP %s is invalid skipping.' % ipv6) return fsm_networking
def getNetDevicePortsAndVlans(localDbClient, portVlanIdMap, netDeviceID, netDeviceElementID, netDeviceName, netDeviceOSH): try: returnOSHV = ObjectStateHolderVector() netDevicePortVlanQuery = '''SELECT phyPort.PhysicalPortID, phyPort.SNMPPhysicalIndex, phyPort.ParentRelPos, port.PORT_NAME, port.PORT_DESC, port.PORT_DUPLEX_MODE, port.PORT_TYPE, port.PORT_SPEED, port.VLAN_NAME, port.VLANID, port.Port_Admin_Status, port.Port_Oper_Status, interface.EndpointID, interface.Description, interface.Alias, interface.MediaAccessAddress FROM lmsdatagrp.PORT_INVENTORY port JOIN dba.PhysicalPort phyPort ON port.PORT_NAME=phyPort.PortName JOIN dba.IFEntryEndpoint interface ON port.PORT_NAME=interface.EndpointName WHERE phyPort.NetworkElementID=%s AND interface.NetworkElementID=%s AND port.DEVICE_ID=%s AND phyPort.PortName=port.PORT_NAME''' % (netDeviceElementID, netDeviceElementID, netDeviceID) netDevicePortVlanResultSet = ciscoworks_utils.doQuery(localDbClient, netDevicePortVlanQuery) ## Return if query returns no results if netDevicePortVlanResultSet == None: logger.info('[' + SCRIPT_NAME + ':getNetDevicePortsAndVlans] No Ports or VLANs found for Net Device <%s>' % netDeviceName) return None ## We have query results! while netDevicePortVlanResultSet.next(): portID = ciscoworks_utils.getStringFromResultSet(netDevicePortVlanResultSet, 1) portIndex = ciscoworks_utils.getStringFromResultSet(netDevicePortVlanResultSet, 2) portSlot = ciscoworks_utils.getStringFromResultSet(netDevicePortVlanResultSet, 3) portName = ciscoworks_utils.getStringFromResultSet(netDevicePortVlanResultSet, 4) portDesc = ciscoworks_utils.getStringFromResultSet(netDevicePortVlanResultSet, 5) portDuplexMode = ciscoworks_utils.getStringFromResultSet(netDevicePortVlanResultSet, 6) interfaceType = ciscoworks_utils.getStringFromResultSet(netDevicePortVlanResultSet, 7) interfaceSpeed = ciscoworks_utils.getStringFromResultSet(netDevicePortVlanResultSet, 8) vlanName = ciscoworks_utils.getStringFromResultSet(netDevicePortVlanResultSet, 9) vlanID = ciscoworks_utils.getStringFromResultSet(netDevicePortVlanResultSet, 10) adminStatus = ciscoworks_utils.getStringFromResultSet(netDevicePortVlanResultSet, 11) operStatus = ciscoworks_utils.getStringFromResultSet(netDevicePortVlanResultSet, 12) interfaceIndex = ciscoworks_utils.getStringFromResultSet(netDevicePortVlanResultSet, 13) interfaceDesc = ciscoworks_utils.getStringFromResultSet(netDevicePortVlanResultSet, 14) interfaceAlias = ciscoworks_utils.getStringFromResultSet(netDevicePortVlanResultSet, 15) interfaceMAC = ciscoworks_utils.getStringFromResultSet(netDevicePortVlanResultSet, 16) if not portID or type(eval(portID)) != type(1): logger.debug('[' + SCRIPT_NAME + ':getNetDevicePortsAndVlans] Invalid portID found for Port <%s> on Net Device <%s>! Skipping...' % (portName, netDeviceName)) continue ciscoworks_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getNetDevicePortsAndVlans] Got port <%s> for Net Device <%s> with ID <%s>' % (portName, netDeviceName, netDeviceElementID)) ## Build PHYSICAL PORT OSH portOSH = ObjectStateHolder('physical_port') ciscoworks_utils.populateOSH(portOSH, {'port_index':int(portIndex), 'name':portName, 'port_displayName':portName, 'description':portDesc, 'port_vlan':vlanID, 'port_slot':portSlot}) ## Map duplex settings value to UCMDB enum portDuplexModeMap = {'auto-duplex':'auto-negotiated', 'full-duplex':'full', 'half-duplex':'half'} if portDuplexMode and portDuplexMode in portDuplexModeMap.keys(): ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getNetDevicePortsAndVlans] Setting duplex mode <%s> for port <%s>' % (portDuplexModeMap[portDuplexMode], portName)) portOSH.setStringAttribute('duplex_setting', portDuplexModeMap[portDuplexMode]) portOSH.setContainer(netDeviceOSH) returnOSHV.add(portOSH) ## Build INTERFACE OSH if interfaceMAC: macAddress = netutils.parseMac(interfaceMAC) if netutils.isValidMac(macAddress): ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getNetDevicePortsAndVlans] Got interface <%s> for Net Device <%s> with ID <%s>' % (macAddress, netDeviceName, netDeviceElementID)) interfaceOSH = modeling.createInterfaceOSH(macAddress, netDeviceOSH, interfaceDesc, interfaceIndex, interfaceType, adminStatus, operStatus, eval(interfaceSpeed), None, interfaceAlias) returnOSHV.add(interfaceOSH) returnOSHV.add(modeling.createLinkOSH('realization', portOSH, interfaceOSH)) ## Add this port to the port-VLAN map if vlanName and vlanName != 'N/A' and vlanID and vlanID != '-1' and type(eval(vlanID)) == type(1): portVlanIdMapKey = '%s:;:%s' % (vlanName, vlanID) if portVlanIdMapKey in portVlanIdMap.keys(): ciscoworks_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getNetDevicePortsAndVlans] Adding port <%s> to existing VLAN <%s:%s>' % (portName, vlanName, vlanID)) portVlanIdMap[portVlanIdMapKey].append(portOSH) else: portVlanIdMap[portVlanIdMapKey] = [portOSH] ciscoworks_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getNetDevicePortsAndVlans] Adding port <%s> to new VLAN <%s:%s>' % (portName, vlanName, vlanID)) netDevicePortVlanResultSet.close() return returnOSHV except: excInfo = logger.prepareJythonStackTrace('') logger.warn('[' + SCRIPT_NAME + ':getNetDevicePortsAndVlans] Exception: <%s>' % excInfo) pass