def discoverLinkAggregations(self, aixNetworking=None): ''' Discover networking related to link aggregations topology only list(networking.Interfaces) or None -> networking.UnixNetworking''' logger.debug("Discover Link Aggregation interfaces") aixNetworking = aixNetworking or UnixNetworking() nics = self.discoverAvailableInterfaces() nameToNic = {} map(lambda nic, nameToNic=nameToNic: nameToNic.update({nic.name: nic}), nics) # filter only link aggregations linkAggregations = filter( lambda nic: str(nic.description).lower().count("etherchannel"), nics) for interface in linkAggregations: logger.debug("Found LA: %s" % interface) nic = aixNetworking.getInterfaceByName(interface.name) if not nic: nic = interface aixNetworking.addInterface(nic) aggregationRole = AggregationRole() nic._addRole(aggregationRole) try: names = self._getMemberNamesOfLinkAggr(nic) logger.debug( 'Gather aggregation information for names %s of %s' % (names, nic)) for name in names: aggregatedInterface = aixNetworking.getInterfaceByName( name) if not aggregatedInterface: aggregatedInterface = nameToNic.get(name) if not aggregatedInterface: index = self._getDevNameAndIndex(name)[1] aggregatedInterface = Interface(name=name, index=index, mac=index) aixNetworking.addInterface(aggregatedInterface) aggregatedRole = _AggregatedRole() aggregatedRole.setAggregatingInterface(nic) aggregatedInterface._addRole(aggregatedRole) if not netutils.isValidMac( aggregatedInterface.mac) and netutils.isValidMac( nic.mac): aggregatedInterface.mac = nic.mac logger.debug('aggregated %s' % aggregatedInterface) aggregationRole.addInterface(aggregatedInterface) except Exception, e: logger.warn(str(e)) self._gatherIpInformation(nic, aixNetworking)
def getFormattedMac(mac): finalResult = None if mac and netutils.isValidMac(mac): normalized = netutils.parseMac(mac) if not isInvalidMac(normalized): finalResult = normalized return finalResult
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 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 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 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 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 discoverInterfaceData(client, sysTable): snmpAgent = snmputils.SnmpAgent(None, client) queryBuilder = snmputils.SnmpQueryBuilder(BASE_INTERFACE_OID) queryBuilder.addQueryElement(1, 'ifIndex') queryBuilder.addQueryElement(2, 'ifDescr') queryBuilder.addQueryElement(3, 'ifType') queryBuilder.addQueryElement(5, 'ifSpeed') queryBuilder.addQueryElement(6, 'ifMac', 'hexa') queryBuilder.addQueryElement(7, 'ifAdminStatus') queryBuilder.addQueryElement(8, 'ifOperStatus') ifList = snmpAgent.getSnmpData(queryBuilder) queryBuilderHigh = snmputils.SnmpQueryBuilder(BASE_HIGH_INTERFACE_OID) queryBuilderHigh.addQueryElement(1, 'ifName') queryBuilderHigh.addQueryElement(15, 'ifHighSpeed') ifHighList = snmpAgent.getSnmpData(queryBuilderHigh) refifHigh = {} for iface in ifHighList: if iface.ifName: refifHigh[iface.ifName] = iface indx2if = {} refIfList = [] for interface in ifList: if interface.ifType == '': interface.ifType = -1 if interface.ifMac == None: interface.ifMac = '' ifValid = SnmpQueries.checkValidMacByInterfaceTypeAndDescription(int(interface.ifType), interface.ifMac, str(interface.ifDescr).lower()) if (ifValid == 1) and (not netutils.isValidMac(interface.ifMac)): logger.debug('Mac is invalid:' + interface.ifMac+', using mac index instead - '+interface.ifIndex) interface.ifMac = interface.ifIndex setattr(interface, 'ifName', None) setattr(interface, 'ifAlias', None) indx2if[interface.ifIndex] = interface refIfList.append(interface) try: getInterfaceNameAndAlias(client, indx2if) except: logger.debugException('Failed to get Name and Alias') #removing interfaces with invalid mac address and no name or description for refIf in refIfList: if not modeling.isValidInterface(refIf.ifMac, refIf.ifDescr, refIf.ifName): logger.warn('Skipped invalid interface [', str(refIf.ifMac), '], name[', str(refIf.ifName), '], description[', str(refIf.ifDescr), ']') ifList.remove(refIf) for iface in ifList: if iface.ifSpeed and long(iface.ifSpeed) == 4294967295L:#magic number in case speed is higher than 10Gb hiIface = refifHigh.get(iface.ifName) if hiIface and hiIface.ifHighSpeed: iface.ifSpeed = long(hiIface.ifHighSpeed) * 1000000 return ifList
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 _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 filterInterfaces(self, interfacesList): """list(NetworkInterface) -> list(NetworkInterface)""" uniqueMacs = {} for interface in interfacesList: if not interface.macAddress or not netutils.isValidMac(interface.macAddress): continue if not uniqueMacs.get(interface.macAddress): uniqueMacs[interface.macAddress] = interface else: uniqueMacs[interface.macAddress].ips.extend(interface.ips) uniqueMacs[interface.macAddress].masks.extend(interface.masks) return uniqueMacs.values()
def discoverLinkAggregations(self, aixNetworking = None): ''' Discover networking related to link aggregations topology only list(networking.Interfaces) or None -> networking.UnixNetworking''' logger.debug("Discover Link Aggregation interfaces") aixNetworking = aixNetworking or UnixNetworking() nics = self.discoverAvailableInterfaces() nameToNic = {} map(lambda nic, nameToNic = nameToNic: nameToNic.update({nic.name : nic}), nics) # filter only link aggregations linkAggregations = filter(lambda nic: str(nic.description).lower().count("etherchannel"), nics) for interface in linkAggregations: logger.debug("Found LA: %s" % interface) nic = aixNetworking.getInterfaceByName(interface.name) if not nic: nic = interface aixNetworking.addInterface(nic) aggregationRole = AggregationRole() nic._addRole(aggregationRole) try: names = self._getMemberNamesOfLinkAggr(nic) logger.debug('Gather aggregation information for names %s of %s' % (names, nic)) for name in names: aggregatedInterface = aixNetworking.getInterfaceByName(name) if not aggregatedInterface: aggregatedInterface = nameToNic.get(name) if not aggregatedInterface: index = self._getDevNameAndIndex(name)[1] aggregatedInterface = Interface(name = name, index = index, mac = index) aixNetworking.addInterface(aggregatedInterface) aggregatedRole = _AggregatedRole() aggregatedRole.setAggregatingInterface(nic) aggregatedInterface._addRole(aggregatedRole) if not netutils.isValidMac(aggregatedInterface.mac) and netutils.isValidMac(nic.mac): aggregatedInterface.mac = nic.mac logger.debug('aggregated %s' % aggregatedInterface) aggregationRole.addInterface(aggregatedInterface) except Exception, e: logger.warn(str(e)) self._gatherIpInformation(nic, aixNetworking)
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 filterInterfaces(self, interfacesList): '''list(NetworkInterface) -> list(NetworkInterface)''' uniqueMacs = {} for interface in interfacesList: if not interface.macAddress or not netutils.isValidMac( interface.macAddress): continue if not uniqueMacs.get(interface.macAddress): uniqueMacs[interface.macAddress] = interface else: uniqueMacs[interface.macAddress].ips.extend(interface.ips) uniqueMacs[interface.macAddress].masks.extend(interface.masks) return uniqueMacs.values()
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 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 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 _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 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 __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 get_remote_devices(self): ''' Creates remote devices dictionary on base of CDP info @type : ->{Str:NetworkDevice} ''' devices = {} connections = self.get_cdp_info() for connection in connections: ip_address = None mac_address = None name = None try: if int(connection.cdpCacheAddressType) == 1: ip_address = int(connection.cdpCacheAddress, 16) ip_address = str(ip_addr.IPAddress(ip_address)) except: logger.debug('Failed to convert %s to int' % connection.cdpCacheAddressType) try: if netutils.isValidMac(connection.cdpCacheDeviceId): mac_address = connection.cdpCacheDeviceId else: name = str(connection.cdpCacheDeviceId).decode('hex') except: logger.debugException('') device = NetworkDevice() device.ip_address = ip_address device.mac_address = mac_address device.name = name device.model = connection.cdpCachePlatform device.description = connection.cdpCacheVersion device.port = connection.cdpCacheDevicePort device.address_id = mac_address or ip_address interface_index = connection.index.split('.')[0] if interface_index: devices[interface_index] = device return devices
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 __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 __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 _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 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 processNmapResult(fileName, OSHVResult, discoverOsName, doServiceFingerprints, createApp, Framework): try: document = SAXBuilder(0).build(fileName) except: raise ValueError, "Can't parse XML document with nmap results. Skipped." hosts = XmlWrapper(document.getRootElement().getChildren('host')) for host in hosts: hostOsh = None ip = None macs = [] addresses = XmlWrapper(host.getChildren('address')) for address in addresses: type = address.getAttributeValue('addrtype') addr = address.getAttributeValue('addr') if type == 'ipv4': ip = addr elif type == 'mac': macs.append(addr) hostnames = host.getChild('hostnames') if (hostnames is not None) and netutils.isValidIp(ip): hostnames = map(lambda elem: elem.getAttributeValue('name'), XmlWrapper(hostnames.getChildren('hostname'))) hostname = hostnames and hostnames[ 0] or None #using only first dnsname os = host.getChild('os') if os and discoverOsName: osClass = os.getChild('osclass') if not osClass: osMatch = os.getChild('osmatch') osClass = osMatch.getChild('osclass') if osClass: osType = osClass.getAttributeValue("type") osFamily = osClass.getAttributeValue("osfamily") osVendor = osClass.getAttributeValue("vendor") hostClass = getHostClass(osType, osFamily) if not hostClass: Framework.reportWarning( "Unknown OS detected. Vendor '%s', family '%s'" % (osVendor, osFamily)) hostClass = "host" hostOsh = modeling.createHostOSH(ip, hostClass) hostOsh.setAttribute("host_vendor", osVendor) osMatch = os.getChild('osmatch') if osMatch: separateCaption(hostOsh, osMatch.getAttributeValue("name")) hostOsh.setAttribute( "host_osaccuracy", osMatch.getAttributeValue("accuracy") + '%') if not hostOsh: hostOsh = modeling.createHostOSH(ip) ipOsh = modeling.createIpOSH(ip, dnsname=hostname) OSHVResult.add(ipOsh) OSHVResult.add(finalizeHostOsh(hostOsh)) OSHVResult.add(modeling.createLinkOSH('contained', hostOsh, ipOsh)) for mac in macs: if netutils.isValidMac(mac): interfaceOsh = modeling.createInterfaceOSH(mac, hostOsh) OSHVResult.add(interfaceOsh) OSHVResult.add( modeling.createLinkOSH('containment', interfaceOsh, ipOsh)) applicationList = [] if not host.getChild('ports'): return ports = XmlWrapper(host.getChild('ports').getChildren('port')) for port in ports: portNumber = port.getAttributeValue('portid') protocol = port.getAttributeValue('protocol') serviceName = None if doServiceFingerprints: if port.getChild("state").getAttributeValue("state").find( 'open') == -1: continue serviceNode = port.getChild("service") if serviceNode: serviceName = serviceNode.getAttributeValue("name") serviceProduct = serviceNode.getAttributeValue( "product") serviceVersion = serviceNode.getAttributeValue( "version") if createApp and serviceProduct and serviceProduct not in applicationList: addApplicationCI(ip, hostOsh, serviceProduct, serviceVersion, OSHVResult) applicationList.append(serviceProduct) addServiceAddressOsh(hostOsh, OSHVResult, ip, portNumber, protocol, serviceName)
def _parseVmInterfaces(self, output): if output: rawMacs = re.findall(',0x([\da-fA-F]+):', output) return [mac for mac in rawMacs if netutils.isValidMac(mac)]
def discoverInterfaceData(client, sysTable): snmpAgent = snmputils.SnmpAgent(None, client) queryBuilder = snmputils.SnmpQueryBuilder(BASE_INTERFACE_OID) queryBuilder.addQueryElement(1, 'ifIndex') queryBuilder.addQueryElement(2, 'ifDescr') queryBuilder.addQueryElement(3, 'ifType') queryBuilder.addQueryElement(5, 'ifSpeed') queryBuilder.addQueryElement(6, 'ifMac', 'hexa') queryBuilder.addQueryElement(7, 'ifAdminStatus') queryBuilder.addQueryElement(8, 'ifOperStatus') ifList = snmpAgent.getSnmpData(queryBuilder) queryBuilderHigh = snmputils.SnmpQueryBuilder(BASE_HIGH_INTERFACE_OID) queryBuilderHigh.addQueryElement(1, 'ifName') queryBuilderHigh.addQueryElement(15, 'ifHighSpeed') ifHighList = snmpAgent.getSnmpData(queryBuilderHigh) refifHigh = {} for iface in ifHighList: if iface.ifName: refifHigh[iface.ifName] = iface indx2if = {} refIfList = [] for interface in ifList: if interface.ifType == '': interface.ifType = -1 if interface.ifMac == None: interface.ifMac = '' if interface.ifMac and len(interface.ifMac) >= 34: #most likely we're dealing with the string to hex encoded value #try to decode it try: mac = interface.ifMac.decode('hex') #the fetched MAC might have missing 0 character at the end if mac: if len(mac) in [16, 11]: mac = mac + '0' interface.ifMac = mac except: logger.debugException('') ifValid = SnmpQueries.checkValidMacByInterfaceTypeAndDescription(int(interface.ifType), interface.ifMac, interface.ifDescr and interface.ifDescr.lower()) if (ifValid == 1) and (not netutils.isValidMac(interface.ifMac)): logger.debug('Mac is invalid:' + interface.ifMac+', using mac index instead - '+interface.ifIndex) interface.ifMac = interface.ifIndex setattr(interface, 'ifName', None) setattr(interface, 'ifAlias', None) indx2if[interface.ifIndex] = interface refIfList.append(interface) try: getInterfaceNameAndAlias(client, indx2if) except: logger.debugException('Failed to get Name and Alias') #removing interfaces with invalid mac address and no name or description for refIf in refIfList: if not modeling.isValidInterface(refIf.ifMac, refIf.ifDescr, refIf.ifName): logger.warn('Skipped invalid interface [', str(refIf.ifMac), '], name[', str(refIf.ifName), '], description[', str(refIf.ifDescr), ']') ifList.remove(refIf) for iface in ifList: if iface.ifSpeed and long(iface.ifSpeed) == 4294967295L:#magic number in case speed is higher than 10Gb hiIface = refifHigh.get(iface.ifName) if hiIface and hiIface.ifHighSpeed: iface.ifSpeed = long(hiIface.ifHighSpeed) * 1000000 return ifList
def doWMI(client, wmiOSH, ip_address, ip_domain, hostForLinkOSH, host_cmdbid=None, host_key=None, host_macs=None, ucmdb_version=None): '''@types: WmiClient, ObjectStateHolder, IPAddress, str, ObjectStateHolder, str, str, list[str], int -> ObjectStateHolderVector @param ip_address: Destination IP address ''' wmiProvider = wmiutils.getWmiProvider(client) hostDiscoverer = host_win_wmi.WmiHostDiscoverer(wmiProvider) hostInfo = hostDiscoverer.discoverHostInfo() machineName = hostInfo.hostName interfacesDiscover = networking_win_wmi.WmiInterfaceDiscoverer(wmiProvider, ip_address) vector = ObjectStateHolderVector() interfaceList = interfacesDiscover.getInterfaces() parentLinkList = ObjectStateHolderVector() isVirtual = 1 resultEmpty = 1 interfacesToUpdateList = [] for interface in interfaceList: ips = interface.ips MACAddress = interface.macAddress masks = interface.masks Description = interface.description dhcpEnabled = interface.dhcpEnabled resultEmpty = 0 for ipIndex, ipAddress in enumerate(__iterate_valid_ips(ips)): IPSubnet = (masks and masks[ipIndex]) or None if str(ip_address) == str(ipAddress): isVirtual = 0 ipOSH = modeling.createIpOSH(ipAddress) # Test if the same ip and interface are in the list allready logger.debug('Found ip address: ', ipAddress, ', MACAddress: ', MACAddress, ', IPSubnet: ', IPSubnet, ', Description: ', Description) # create OSH for IP and add it to the list # create OSH for Network and add it to the list __vector = getIPNetworkMemebrList(ipAddress, IPSubnet, '', dhcpEnabled, Description) if __vector.size() > 0: vector.addAll(__vector) if netutils.isValidMac(MACAddress): # create link interface to its ip only it has an ip defined interfaceOSH = modeling.createInterfaceOSH(MACAddress, hostForLinkOSH, Description) parentLinkList.add(modeling.createLinkOSH('containment', interfaceOSH, ipOSH)) interfacesToUpdateList.append(interfaceOSH) # Check if the Input IP is virtual, we do not want to return the WMI if isVirtual == 1: logger.warn('Destination is not among discovered IPs assuming virtual. WMI object will not be reported.') vIPOSH = modeling.createIpOSH(ip_address) vIPOSH.setBoolAttribute('isvirtual', 1) vector.add(vIPOSH) if resultEmpty == 1: logger.warn('WMI was able to connect, but WMI Query returns no results') vector.clear() return vector # create the host and all the objects if len(interfaceList) > 0: hostOSH = None try: hostOSH = modeling.createCompleteHostOSHByInterfaceList('nt', interfaceList, 'Windows', machineName, None, host_cmdbid, host_key, host_macs, ucmdb_version) except: hostOSH = modeling.createHostOSH(str(ip_address), 'nt') logger.debugException('Could not find a valid MAC address for key on ip : %s. Creating incomplete host\n' % ip_address) logger.warn('Could not find a valid MAC address for key on ip : %s. Creating incomplete host\n' % ip_address) # select from Win32_OperatingSystem _wmiQuery = 'select Caption,Version,ServicePackMajorVersion,ServicePackMinorVersion,BuildNumber,Organization,RegisteredUser,TotalVisibleMemorySize,LastBootUpTime,OtherTypeDescription,description from Win32_OperatingSystem' resultSet = client.executeQuery(_wmiQuery) # @@CMD_PERMISION wmi protocol execution osinstalltype = None if resultSet.next(): Caption = resultSet.getString(1) Version = resultSet.getString(2) ServicePackMajorVersion = resultSet.getString(3) ServicePackMinorVersion = resultSet.getString(4) BuildNumber = resultSet.getString(5) Organization = resultSet.getString(6) RegisteredUser = resultSet.getString(7) TotalVisibleMemorySize = resultSet.getString(8) LastBootUpTime = resultSet.getString(9) OtherTypeDescription = resultSet.getString(10) description = resultSet.getString(11) (vendor, osName, osinstalltype) = separateCaption(Caption, OtherTypeDescription) hostOSH.setAttribute('host_vendor', vendor) modeling.setHostOsName(hostOSH, osName) hostOSH.setAttribute('host_osinstalltype', osinstalltype) setLastBootUpTime(hostOSH, LastBootUpTime) biosUUID = getBIOSUUID(client) defaultGateway = getDefaultGateway(client) hostOSH.setAttribute('host_osversion', Version) sp = ServicePackMajorVersion + '.' + ServicePackMinorVersion if checkSpVersion(sp): hostOSH.setAttribute('nt_servicepack', sp) hostOSH.setAttribute('host_osrelease', str(BuildNumber)) hostOSH.setAttribute('nt_registrationorg', Organization) hostOSH.setAttribute('nt_registeredowner', RegisteredUser) hostOSH.setAttribute('nt_physicalmemory', TotalVisibleMemorySize) hostOSH.setAttribute('host_hostname', machineName) modeling.setHostBiosUuid(hostOSH, biosUUID) modeling.setHostDefaultGateway(hostOSH, defaultGateway) modeling.setHostOsFamily(hostOSH, 'windows') hostOSH = HostBuilder(hostOSH).setDescription(description).build() _wmiQuery2 = 'select Manufacturer,NumberOfProcessors,Model,Domain from Win32_ComputerSystem' resultSet = client.executeQuery(_wmiQuery2) # @@CMD_PERMISION wmi protocol execution if resultSet.next(): Manufacturer = resultSet.getString(1) if ((Manufacturer != None) and (Manufacturer.find('system manufacturer') == -1)): modeling.setHostManufacturerAttribute(hostOSH, Manufacturer.strip()) NumberOfProcessors = resultSet.getString(2) hostOSH.setAttribute('nt_processorsnumber', int(NumberOfProcessors)) Model = resultSet.getString(3) modeling.setHostModelAttribute(hostOSH, Model) osDomain = resultSet.getString(4) hostOSH.setAttribute('host_osdomain', osDomain.strip()) biosAssetTag = hostDiscoverer.getBiosAssetTag() if biosAssetTag: hostOSH.setAttribute('bios_asset_tag', biosAssetTag) _wmiQuery4 = 'SELECT SerialNumber FROM Win32_BIOS' resultSet = client.executeQuery(_wmiQuery4) # @@CMD_PERMISSION wmi protocol execution if resultSet.next(): serialNumber = processSerialNumber(resultSet.getString(1)) if not serialNumber: wmiBaseBoardSerialNumber = 'SELECT SerialNumber FROM Win32_BaseBoard' resultSet = client.executeQuery(wmiBaseBoardSerialNumber) # @@CMD_PERMISION wmi protocol execution serialNumber = processSerialNumber(str(resultSet)) modeling.setHostSerialNumberAttribute(hostOSH, serialNumber) try: paeEnabled = getPAEState(client) if paeEnabled and paeEnabled.lower() in ['1', 'true']: hostOSH.setBoolAttribute('pae_enabled', 1) elif paeEnabled and paeEnabled.lower() in ['0', 'false']: hostOSH.setBoolAttribute('pae_enabled', 0) except Exception, ex: logger.warn('Failed getting PAE state. %s' % ex) try: osArchitecture = getOsArchitecture(client) if osinstalltype and osinstalltype.find('64') != -1: osArchitecture = '64-bit' if osArchitecture: hostOSH.setStringAttribute('os_architecture', osArchitecture) except Exception, ex: logger.warn('Failed getting OS Architecture value. %s' % ex)
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 addToOSHV(self, aSwitch, ObjSHV): aPtOSH = ObjectStateHolder("port") aPtOSH.setContainer(aSwitch.hostOSH) modeling.setPhysicalPortNumber(aPtOSH, self.portNumber) aPtOSH.setAttribute('port_remotestpbgid', self.remoteStpBgId) self.__setRemotePortNumber(aPtOSH, self.remoteStpPortId) aPtOSH.setAttribute('port_intfcindex', int(self.interfaceIndex)) aPtOSH.setAttribute('port_nextmac', self.remoteTermMac) aPtOSH.setAttribute('port_hostbgid', self.hostBgId) aPtOSH.setAttribute('port_discoverylastrun', self.discoveryInst) aPtOSH.setAttribute('data_name', self.data_name) ObjSHV.add(aPtOSH) if self.ucmdbversion and self.ucmdbversion >= 9: interface = aSwitch.getInterfaceByIfIndex(self.interfaceIndex) if interface: intfOsh = modeling.createInterfaceOSH( interface.ifMac, aSwitch.hostOSH, interface.ifDescr, interface.ifIndex, interface.ifType, interface.ifAdminStatus, interface.ifOperStatus, interface.ifSpeed, interface.ifName, interface.ifAlias) if intfOsh: ObjSHV.add( modeling.createLinkOSH('realization', aPtOSH, intfOsh)) #in case we get a MAC for remote iface and this is a real MAC report a Layer2Connection between these interfaces if self.remoteTermMac and not self.remoteTermMac.endswith( ":" + self.portNumber) and netutils.isValidMac( self.remoteTermMac): logger.debug('Created L2C from Ports') #write the reported layer2 info to file in order to post process in case of remote MAC "Multiple Match" md5_obj = md5.new() updateValue = '' vlan_id = self.framework.getDestinationAttribute( 'snmpCommunityPostfix') for value in [ interface.ifMac.upper(), interface.ifDescr, interface.ifIndex, interface.ifType, interface.ifAdminStatus, interface.ifOperStatus, interface.ifSpeed, interface.ifName, interface.ifAlias, vlan_id ]: if not updateValue: updateValue = str(value) else: updateValue = updateValue + ':::' + str(value) md5_obj.update(updateValue) filename = md5_obj.hexdigest() filepath = os.path.join(CollectorsParameters.HOME_DIR, 'runtime/l2reported/') if not os.path.exists(filepath): try: os.mkdir(filepath) except: logger.debug('folder creation failed') f = open(filepath + filename, 'w') f.write(updateValue + '\n') macsSeparated = '' for mac in [self.remoteTermMacRaw.get(self.remoteTermMac)]: if mac.count('.'): mac = self.convertToHexString(mac) if not macsSeparated: macsSeparated = mac else: macsSeparated = macsSeparated + ':::' + mac f.write(macsSeparated) f.close() ObjSHV.addAll( modeling.createLayer2ConnectionWithLinks( [self.remoteTermMacRaw.get(self.remoteTermMac)], aSwitch, interface))
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 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 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 _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 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 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