def parseNetworkingInformation(self, buffer): """ This function performs parsing of the 'lshmc -n' command output @param buffer: string buffer of the command output @return: host and ip information @rtype: instance of HostDo object """ host = ibm_hmc_lib.Host() if buffer: propertiesDict = self.buildPropertiesDict(buffer) host.hostname = propertiesDict.get('hostname') host.domainName = propertiesDict.get('domain') for (key, value) in propertiesDict.items(): if key.startswith('ipv4addr_eth'): if value and netutils.isValidIp(value) and not netutils.isLocalIp(value): ip = ibm_hmc_lib.Ip(value, propertiesDict.get(key.replace('addr', 'netmask'))) host.ipList.append(ip) if not host.ipList: #another version of HMC outputs, for the minor HMC version change ipStr = propertiesDict.get('ipaddr') netMaskStr = propertiesDict.get('networkmask') if ipStr and netMaskStr: ipList = ipStr.split(',') netMaskList = netMaskStr.split(',') for i in range(len(ipList)): ipAddr = ipList[i].strip() netMask = netMaskList[i].strip() if ipAddr and netutils.isValidIp(ipAddr) and not netutils.isLocalIp(ipAddr): ip = ibm_hmc_lib.Ip(ipAddr, netMask) host.ipList.append(ip) if not host.ipList: raise ValueError("Failed parsing HMC host IP addresses from %s " % buffer) return host
def createScpOSHV(container, type, host, port, context, shell, localIP=None, dnsServers=None): OSHVResult = ObjectStateHolderVector() if not host: return OSHVResult ipAddresses = [] if (host in LOCALHOST) and localIP: logger.debug("found local ip: %s , use %s instead" % (host, localIP)) host = localIP if netutils.isValidIp(host): ipAddresses.append(host) else: # try to resolve ip address from hostname logger.debug('Trying to resolve ip address from hostname:', host) ipAddresses = resolveIPByNsLookup(dnsServers, shell, host) if len(ipAddresses) == 0: ipAddresses = resolveIPByINet(host, port) if len(ipAddresses) == 0: ipAddresses = resolveIPBySocket(host) for ipAddress in ipAddresses: if not netutils.isValidIp(ipAddress): logger.debug("ignore invalid ip address: ", ipAddress) continue scpOsh = createScpOsh(container, type, ipAddress, port, context, host) OSHVResult.add(scpOsh) # Add additional ip CIs for all next hops to make sure new jobs could be triggered. ip = ip_addr.IPAddress(ipAddress) OSHVResult.add(modeling.createIpOSH(ip)) return OSHVResult
def __init__(self, id, ip = None, name = None, systemObjId = None, descr = None, localHba = None, \ localPorts = None, model = None, serial = None, version = None, vendor = None, status = None): ''' @param id: string @param ip: string @param name: string @param systemObjId: id of the object in Storage System @param description: string @param localHba: list of host base adapters @param localPorts: list of fcPorts @raise ValueError: in case ip is invalid and no name, or no name and ip ''' self.ip = None try: if netutils.isValidIp(ip): self.ip = ip except: logger.warn('IP "%s" for node name "%s" is invalid' % (ip, name)) if not id and not (name or self.ip): raise ValueError("Name or ip attribute must be set along with id attribute") self.name = name self.id = id self.localHba = localHba or [] self.localPorts = localPorts or [] self.systemObjId = systemObjId self.description = descr self.model = model self.serial = serial self.version = version self.vendor = vendor self.status = status
def createCimOsh(ipAddress, containerOsh, credentialId, category=None): ''' Builds a CIM OSH representing successful connection. @param ipAddress: string @param containerOsh: corresponding container OSH @param credentialId: protocol entry @raise ValueError: no credential or no IP. @return: OSH ''' if not credentialId: raise ValueError('CredentialsId must be set') if not netutils.isValidIp(ipAddress): raise ValueError('IP Address must be set') cimOsh = ObjectStateHolder('cim') cimOsh.setAttribute('data_name', Protocol.FULL) cimOsh.setAttribute('credentials_id', credentialId) cimOsh.setAttribute('application_ip', ipAddress) cimOsh.setContainer(containerOsh) if category: list_ = StringVector((category, )) categoryAttribute = AttributeStateHolder('cim_category', list_) cimOsh.addAttributeToList(categoryAttribute) return cimOsh
def reportProcessToPort(hostId, processes, endpoints): if not (processes and endpoints): return vector = ObjectStateHolderVector() hostOsh = modeling.createOshByCmdbId('node', hostId) vector.add(hostOsh) proc_builder = ProcessBuilder() key_to_endpoints_map = {} [ key_to_endpoints_map.update({x.getKey(): x.getEndpoints()}) for x in endpoints ] for process in processes: processOsh = proc_builder.buildProcessOsh(process) processOsh.setContainer(hostOsh) vector.add(processOsh) remotes = key_to_endpoints_map.get(process.getPid()) if remotes: for remote in remotes: if not netutils.isValidIp(remote.getAddress()): logger.debug(remote, ' is not a valid ip') continue builder = netutils.ServiceEndpointBuilder() reporter = netutils.EndpointReporter(builder) nodeOsh = reporter.reportHostFromEndpoint(remote) endpointOsh = reporter.reportEndpoint(remote, nodeOsh) linkOsh = modeling.createLinkOSH('client_server', processOsh, endpointOsh) linkOsh.setStringAttribute('clientserver_protocol', 'tcp') vector.add(nodeOsh) vector.add(endpointOsh) vector.add(linkOsh) return vector
def createCimOsh(ipAddress, containerOsh, credentialId, category=None): ''' Builds a CIM OSH representing successful connection. @param ipAddress: string @param containerOsh: corresponding container OSH @param credentialId: protocol entry @raise ValueError: no credential or no IP. @return: OSH ''' if not credentialId: raise ValueError('CredentialsId must be set') if not netutils.isValidIp(ipAddress): raise ValueError('IP Address must be set') cimOsh = ObjectStateHolder('cim') cimOsh.setAttribute('data_name', Protocol.FULL) cimOsh.setAttribute('credentials_id', credentialId) cimOsh.setAttribute('application_ip', ipAddress) cimOsh.setContainer(containerOsh) if category: list_ = StringVector((category,)) categoryAttribute = AttributeStateHolder('cim_category', list_) cimOsh.addAttributeToList(categoryAttribute) return cimOsh
def set(self, ip): '''@types: str @raise ValueError: IP is not valid ''' if not netutils.isValidIp(ip): raise ValueError("IP is not valid %s" % ip) self.__ip = ip
def resolveNamesToIPs(self, hostNode, parsedData): if parsedData: for internalName in parsedData.keys(): nodeData = parsedData[internalName] values = nodeData.get(hostNode) if values: listenedIPs = {} for ipaddr in values: stripedIpAddr = ipaddr.strip() if netutils.isValidIp(stripedIpAddr): listenedIPs[stripedIpAddr] = '' else: resolver = DNSResolver(self.shell, stripedIpAddr) resolvedIp = resolver.getIPAddress() if resolvedIp: listenedIPs[resolvedIp.strip()] = '' else: aliasBasedIps = resolver.resolveNSLookupAliasBased( ) if aliasBasedIps: for ip in aliasBasedIps: listenedIPs[ip.strip()] = '' nodeData[hostNode] = listenedIPs.keys() parsedData[internalName] = nodeData return parsedData
def DiscoveryMain(Framework): OSHVResult = ObjectStateHolderVector() urlString = Framework.getParameter(PARAM_URL) reportPoweredOffVms = 0 reportPoweredOffVmsValue = Framework.getParameter(PARAM_REPORT_POWERED_OFF_VMS) if reportPoweredOffVmsValue and reportPoweredOffVmsValue.lower() =='true': reportPoweredOffVms = 1 ipAddress = None try: urlObject = URL(urlString) hostname = urlObject.getHost() if not hostname: logger.debug("Hostname is not defined in URL '%s'" % urlString) raise MalformedURLException() ipAddress = vcloud_discover.getIpFromUrlObject(urlObject) if not ipAddress or not netutils.isValidIp(ipAddress) or netutils.isLocalIp(ipAddress): msg = "Failed to resolve the IP address of server from specified URL" errormessages.resolveAndReport(msg, vcloud_discover.VcloudProtocol.DISPLAY, Framework) return OSHVResult except MalformedURLException: msg = "Specified URL '%s' is malformed" % urlString errormessages.resolveAndReport(msg, vcloud_discover.VcloudProtocol.DISPLAY, Framework) except: msg = logger.prepareJythonStackTrace("") errormessages.resolveAndReport(msg, vcloud_discover.VcloudProtocol.DISPLAY, Framework) else: #configure how connections should be discovered/established connectionDiscoverer = vcloud_discover.ConnectionDiscoverer(Framework) urlGenerator = vcloud_discover.ConstantUrlGenerator(urlString) connectionDiscoverer.setUrlGenerator(urlGenerator) connectionDiscoverer.addIp(ipAddress) #configure how established/failed connection should be used connectionHandler = vcloud_discover.BaseDiscoveryConnectionHandler(Framework) topologyDiscoverer = vcloud_discover.createVcloudDiscoverer(Framework) topologyReporter = vcloud_report.createVcloudReporter(Framework, None, reportPoweredOffVms) connectionHandler.setDiscoverer(topologyDiscoverer) connectionHandler.setReporter(topologyReporter) connectionDiscoverer.setConnectionHandler(connectionHandler) connectionDiscoverer.initConnectionConfigurations() connectionDiscoverer.discover(firstSuccessful=0) if not connectionHandler.connected: for errorMsg in connectionHandler.connectionErrors: Framework.reportError(errorMsg) for warningMsg in connectionHandler.connectionWarnings: Framework.reportWarning(warningMsg) return OSHVResult
def __init__(self, props, nlbClusterOsh, hostOSH, framework): if not nlbClusterOsh: raise Exception( 'NLB cluster discovery misconfigured; NLB node have to be linked to existing cluster' ) self.nlbClusterOsh = nlbClusterOsh self.hostOSH = hostOSH self.priority = props['HostPriority'] self.modeOnStart = props['ClusterModeOnStart'] if props['DedicatedIPAddresses/']: self.ip = props['DedicatedIPAddresses/'].split('/')[0] netMask = props['DedicatedIPAddresses/'].split('/')[1] else: self.ip = props['DedicatedIPAddress'] netMask = props['DedicatedNetworkMask'] self.dedicatedIpOSH = None #check the IP #very unreal situation but we have to handle it if not netutils.isValidIp(self.ip) or netutils.isLocalIp(self.ip): msg = 'Dedicated IP of cluster node is local or invalid: ' + str( self.ip) logger.warn(msg) framework.reportWarning( 'Dedicated IP of cluster node is local or invalid') else: self.dedicatedIpOSH = modeling.createIpOSH(self.ip, netMask)
def doReadFile(shellUtils, fileName, OSHVResult, tnsFile): tns_entries = dbutils.parseTNSNames(tnsFile, '', shellUtils) # parseTnsEntries(fileName, shellUtils, tns_entries, OSHVResult) if (len(tns_entries)==0): logger.info('no entries returns from ', fileName, '. Please verify if the file exists and it is valid TNS file.') return logger.debug('Found ', len(tns_entries), ' entries in tnsnames.ora file.') oracleList = [] for tns_entry in tns_entries: try: db_type = 'oracle' connectString = tns_entry[0] host_dns= tns_entry[1] db_port = tns_entry[2] db_sid = tns_entry[3].upper() host_ip = tns_entry[5] if (netutils.isValidIp(host_ip)): hashName = host_ip + db_sid if ((hashName in oracleList) == 0) : oracleList.append(hashName) hostOSH = modeling.createHostOSH(host_ip) oracleOSH = modeling.createDatabaseOSH(db_type, db_sid, db_port, host_ip, hostOSH) oracleOSH.setAttribute('database_dbconnectstring', connectString) oracleOSH.setContainer(hostOSH) OSHVResult.add(hostOSH) OSHVResult.add(oracleOSH) else: logger.warn("Can not resolve the IP from the TNS entry's host name (", host_dns, ") - TNS entry skipped.") except: logger.debugException('Unexpected TNS Parsing Exception:')
def discoverOAMEndpoint(shell, configFile): """ Discover OAM endpoint in ObAccessClient.xml @types: str -> Endpoint """ logger.debug('find OAM server') root = _buildDocumentForXpath(configFile, 0) xpath = _getXpath() servers = xpath.evaluate( '//CompoundList/ValNameList[@ListName="primaryServer1"]', root, XPathConstants.NODESET) for i in range(0, servers.getLength()): server = servers.item(i) host = xpath.evaluate('//NameValPair[@ParamName="host"]/@Value', server, XPathConstants.STRING) port = xpath.evaluate('//NameValPair[@ParamName="port"]/@Value', server, XPathConstants.STRING) if host and port: logger.debug('got OAM server: %s:%s' % (host, port)) if netutils.isValidIp(host): ip = host else: ip = _resolveHostName(shell, host) if ip: return netutils.createTcpEndpoint(ip, port) else: logger.error('Cannot resolve ip from host name "%s"' % host) else: logger.error('failed to get OAM server') return None
def __init__(self, system, ipAddress=None, ipDomain=None, username=None, credentialsId=None, connectionClient=None, router=None, isSolutionManager=None, tmsDomain=None): r'''@type system: System @type tmsDomain: TmsDomain''' if not system: raise ValueError("System is not specified") self.system = system if ipAddress and not netutils.isValidIp(ipAddress): raise ValueError("Invalid IP address") self.ipAddress = ipAddress self.ipDomain = ipDomain self.username = username self.connectionClient = connectionClient self.connectionCredentialsId = credentialsId self.router = router self.isSolutionManager = isSolutionManager self.tmsDomain = tmsDomain
def reportDatasourceWithDeployer(self, domainOsh, deploymentScopeOsh, datasource): r'''@types: ObjectStateHolder, ObjectStateHolder, jms.Datasource -> ObjectStateHolderVector @param deploymentScope: Used as a container for the resources ''' # for instance resources for cell has the same deployment scope as domain\ # no need to create link containerOsh = domainOsh vector = self._createVector() # first of all report datasource datasourceOsh = self.reportDatasource(datasource, containerOsh) vector.add(datasourceOsh) # report all destinations where datasource is a container for destination in datasource.getDestinations(): destinationOsh = self.reportDestination(destination, datasourceOsh) vector.add(destinationOsh) # report mq server if present if destination.server and netutils.isValidIp( destination.server.address): vector.addAll( self.reportMqServerWithEndpoint(destination.server)) # also there must be a link between datasource and server #vector.add(self.linkDatasourceAndMqServer( # datasourceOsh, # destination.server.getOsh()) #) if deploymentScopeOsh: vector.add( self.linkDeploymentScopeAndDatasource(deploymentScopeOsh, datasourceOsh)) return vector
def set(self, ip): '''@types: str @raise ValueError: IP is not valid ''' if not netutils.isValidIp(ip): raise ValueError( "IP is not valid %s" % ip ) self.__ip = ip
def _resolve(hostname): '@types: str -> str?' if netutils.isValidIp(hostname): return hostname ip = netutils.getHostAddress(hostname) if ip and not netutils.isLocalIp(ip): return ip
def discoverOAMEndpoint(shell, configFile): """ Discover OAM endpoint in ObAccessClient.xml @types: str -> Endpoint """ logger.debug('find OAM server') root = _buildDocumentForXpath(configFile, 0) xpath = _getXpath() servers = xpath.evaluate('//CompoundList/ValNameList[@ListName="primaryServer1"]', root, XPathConstants.NODESET) for i in range(0, servers.getLength()): server = servers.item(i) host = xpath.evaluate('//NameValPair[@ParamName="host"]/@Value', server, XPathConstants.STRING) port = xpath.evaluate('//NameValPair[@ParamName="port"]/@Value', server, XPathConstants.STRING) if host and port: logger.debug('got OAM server: %s:%s' % (host, port)) if netutils.isValidIp(host): ip = host else: ip = _resolveHostName(shell, host) if ip: return netutils.createTcpEndpoint(ip, port) else: logger.error('Cannot resolve ip from host name "%s"' % host) else: logger.error('failed to get OAM server') return None
def _createHttpContext(self, uri, transport, serverName, webServerOsh): if uri and transport.hostIp and transport.port: compositeKey = "_".join( [uri, transport.hostIp, str(transport.port)]) httpContextOsh = ObjectStateHolder('httpcontext') httpContextOsh.setAttribute('data_name', compositeKey) if transport.hostName: httpContextOsh.setAttribute('httpcontext_webapplicationhost', transport.hostName) httpContextOsh.setAttribute('httpcontext_webapplicationcontext', uri) if serverName: httpContextOsh.setAttribute('httpcontext_webapplicationserver', serverName) if netutils.isValidIp(transport.hostIp): httpContextOsh.setAttribute('httpcontext_webapplicationip', transport.hostIp) if transport.protocol: httpContextOsh.setAttribute('applicationresource_type', transport.protocol) httpContextOsh.setContainer(webServerOsh) return httpContextOsh
def reportMqServerWithEndpoint(self, server): r''' Make reporting of MQ server based on its IP where contains is incomplete host built using IP address and if port specified linked with corresponding service endpoint @types: jms.MqServer -> ObjectStateHolderVector @raise ValueError: JMS Server is not specified @raise ValueError: MQ Server IP address is empty or not resolved ''' if not server: raise ValueError("JMS Server is not specified") ip = server.address if not (ip and netutils.isValidIp(ip)): raise ValueError("MQ Server IP address is empty or not resolved") vector = ObjectStateHolderVector() hostOsh = modeling.createIpOSH(ip) vector.add(hostOsh) serverOsh = self.reportMqServer(server, hostOsh) vector.add(serverOsh) if server.getPort() is not None: vector.add( modeling.createServiceAddressOsh( hostOsh, ip, server.getPort(), modeling.SERVICEADDRESS_TYPE_TCP)) return vector
def discoverReplication(self, mysqlOsh): """ Tries to find config variables related to mysql replication @param ObjectStateHolder mysqlOsh mysql osh @return list list of OSHs """ masterHostIp = self.getProperty('master-host') if not masterHostIp: return if not netutils.isValidIp(masterHostIp): try: resolver = netutils.DnsResolverByShell(self.shell) masterHostIp = resolver.resolveIpsByHostname(masterHostIp)[0] except netutils.ResolveException: logger.warn('Failed to resolve Master Host into IP') return masterPort = self.getProperty('master-port') mysqlReplicationOsh = ObjectStateHolder('mysql_replication') mysqlReplicationOsh.setAttribute('data_name', 'MySQL Replication') mysqlReplicationOsh.setContainer(mysqlOsh) self.setAttribute(mysqlReplicationOsh, 'master_user', self.REPL_ARGS_MAPPING) self.setAttribute(mysqlReplicationOsh, 'master_connect_retry', self.REPL_ARGS_MAPPING) masterHostOsh = modeling.createHostOSH(masterHostIp) serviceAddressOsh = modeling.createServiceAddressOsh(masterHostOsh, masterHostIp, masterPort, modeling.SERVICEADDRESS_TYPE_TCP) clientServerLink = modeling.createLinkOSH('client_server', mysqlReplicationOsh, serviceAddressOsh) clientServerLink.setStringAttribute('clientserver_protocol', 'TCP') clientServerLink.setLongAttribute('clientserver_destport', int(masterPort)) # masterMysqlOsh = modeling.createDatabaseOSH('mysql', 'MySQL. Port ' + masterPort, masterPort, masterHostIp, masterHostOsh) # useLink = modeling.createLinkOSH('use', masterHostOsh, serviceAddressOsh) return [masterHostOsh, serviceAddressOsh, clientServerLink, mysqlReplicationOsh]
def reportDatasourceWithDeployer(self, domainOsh, deploymentScopeOsh, datasource): r'''@types: ObjectStateHolder, ObjectStateHolder, jms.Datasource -> ObjectStateHolderVector @param deploymentScope: Used as a container for the resources ''' # for instance resources for cell has the same deployment scope as domain\ # no need to create link containerOsh = domainOsh vector = self._createVector() # first of all report datasource datasourceOsh = self.reportDatasource(datasource, containerOsh) vector.add(datasourceOsh) # report all destinations where datasource is a container for destination in datasource.getDestinations(): destinationOsh = self.reportDestination(destination, datasourceOsh) vector.add(destinationOsh) # report mq server if present if destination.server and netutils.isValidIp(destination.server.address): vector.addAll(self.reportMqServerWithEndpoint(destination.server)) # also there must be a link between datasource and server #vector.add(self.linkDatasourceAndMqServer( # datasourceOsh, # destination.server.getOsh()) #) if deploymentScopeOsh: vector.add(self.linkDeploymentScopeAndDatasource(deploymentScopeOsh, datasourceOsh)) return vector
def reportMqServerWithEndpoint(self, server): r''' Make reporting of MQ server based on its IP where contains is incomplete host built using IP address and if port specified linked with corresponding service endpoint @types: jms.MqServer -> ObjectStateHolderVector @raise ValueError: JMS Server is not specified @raise ValueError: MQ Server IP address is empty or not resolved ''' if not server: raise ValueError("JMS Server is not specified") ip = server.address if not (ip and netutils.isValidIp(ip)): raise ValueError("MQ Server IP address is empty or not resolved") vector = ObjectStateHolderVector() hostOsh = modeling.createIpOSH(ip) vector.add(hostOsh) serverOsh = self.reportMqServer(server, hostOsh) vector.add(serverOsh) if server.getPort() is not None: vector.add(modeling.createServiceAddressOsh(hostOsh, ip, server.getPort(), modeling.SERVICEADDRESS_TYPE_TCP ) ) return vector
def getUrl(WSDLUrl, containerOSH): res = ObjectStateHolderVector() urlIP = None try: url = URL(WSDLUrl) hostName = url.getHost() urlIP = netutils.getHostAddress(hostName, None) if (not netutils.isValidIp(urlIP)) or netutils.isLocalIp(urlIP): urlIP = None except: urlIP = None urlOSH = modeling.createUrlOsh(containerOSH, WSDLUrl, 'wsdl') urlIpOSH = None if urlIP != None: try: urlIpOSH = modeling.createIpOSH(urlIP) except: urlIpOSH = None res.add(urlOSH) if urlIpOSH: res.add(urlIpOSH) urlToIpOSH = modeling.createLinkOSH('depend', urlOSH, urlIpOSH) res.add(urlToIpOSH) return res
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 getVirtualMachinesByEsx(client, esxInstance): ''' CimClient -> list(VmComputerSystem) ''' vmInstances = cim_discover.getAssociatorsWithTypeEnforcement( client, esxInstance.getObjectPath(), "VMWARE_HostedDependency", "VMWARE_VMComputerSystem") vmList = [] for vmInstance in vmInstances: vm = VmComputerSystem() vm.setObjectPath(vmInstance.getObjectPath()) vm.name = cim_discover.cleanString( _getCimInstanceProperty(vmInstance, "Name")) elementName = _getCimInstanceProperty(vmInstance, "ElementName") elementName = cim_discover.cleanString(elementName) # html unescape : & -> & elementName = cim_discover.htmlUnescape(elementName) # url unescape: %25 -> % # vmware escapes 3 characters, both slashes and % elementName = urllib.unquote(elementName) vm.elementName = elementName description = _getCimInstanceProperty(vmInstance, "Description") description = cim_discover.cleanString(description) vm.description = cim_discover.htmlUnescape(description) identifyingDescriptions = _getCimInstanceProperty( vmInstance, 'IdentifyingDescriptions') identifyingDescriptions = map(cim_discover.cleanString, identifyingDescriptions) otherIdentifyingInfo = _getCimInstanceProperty(vmInstance, 'OtherIdentifyingInfo') otherIdentifyingInfo = map(cim_discover.cleanString, otherIdentifyingInfo) customProperties = _dictionaryFromCrossCollections( identifyingDescriptions, otherIdentifyingInfo) vm.biosUuid = customProperties.get("BIOS UUID") vm.hostName = customProperties.get("Hostname") primaryIpAddress = customProperties.get("Primary IP Address") if netutils.isValidIp( primaryIpAddress) and not netutils.isLocalIp(primaryIpAddress): vm.primaryIpAddress = primaryIpAddress operationalStatusArray = _getCimInstanceProperty( vmInstance, 'OperationalStatus') if operationalStatusArray is not None: for statusValue in operationalStatusArray: if statusValue is not None: statusValueInt = cim_discover.getIntFromCimInt(statusValue) vm.operationalStatus.add(statusValueInt) vmList.append(vm) return vmList
def __resolveEndpointAddress(self, resolveAddressFn, endpoint): r'@types: (str -> list[str]), netutils.Endpoint -> list[netutils.Endpoint]' endpoints = [] if netutils.isValidIp(endpoint.getAddress()): return endpoint for ip in resolveAddressFn(endpoint.getAddress()): endpoints.append(netutils.updateEndpointAddress(endpoint, ip)) return endpoints
def _resolveEndpoint(resolver, endpoint): r'@types: dns_resolver.DnsResolver, Endpoint -> list[Endpoint]' address = endpoint.getAddress() if netutils.isValidIp(address): return [endpoint] resolveAddressFn = Sfn(resolver.resolveIpsByHostname, fallbackFn = constantly([])) return filter(None, (netutils.updateEndpointAddress(endpoint, ip) for ip in resolveAddressFn(address)))
def __init__(self, publicIp, instanceId=None): r'''@types: str, str @raise ValueError: Invalid IP ''' if not (publicIp and netutils.isValidIp(publicIp)): raise ValueError("Invalid IP") self.__publicIp = publicIp self.__instanceId = instanceId
def _resolveHostIps(shell, hostName): '@types: Shell, str -> list[str]' ips = () if not netutils.isValidIp(hostName): resolver = dns_resolver.SocketDnsResolver() try: ips = resolver.resolve_ips(hostName) except dns_resolver.ResolveException, re: logger.warn("Failed to resolve: %s. %s" % (hostName, re))
def parseNetworkingInformation(self, output): host = ibm_hmc_lib.Host() if output: potential_ips = re.findall('e[nt]+\s+([\da-fA-F:]+)\s+([\da-fA-F:]+)', output) for (ipAddr, mask) in potential_ips: if ipAddr and netutils.isValidIp(ipAddr) and not netutils.isLocalIp(ipAddr): ip = ibm_hmc_lib.Ip(ipAddr, mask) host.ipList.append(ip) return host
def __parseClusterResourceNameAndIps(self, output): result = [] if output: ips = re.findall('\(([\w\.\-]+)\)\s+Address\s+(\d+\.\d+\.\d+\.\d+)', output) logger.debug('Got reources output "%s"' % output) for ip in ips: if ip and netutils.isValidIp(ip[1]): result.append(ip) return result
def getOracleServerIP(self): try: host_address_result = self._client.executeQuery("select UTL_INADDR.get_host_address from dual") while host_address_result.next(): ip = host_address_result.getString(1) if netutils.isValidIp(ip) and not netutils.isLoopbackIp(ip): return ip except: logger.debugException('')
def _parceLsofOutput(self, output): results = [] if output: matcher = re.compile('TCP\s+([\d\.]+)\:(\d+)\s+\(LISTEN') for block in re.split('\n', output): match = matcher.search(block) if match and netutils.isValidIp(match.group(1)) and not netutils.isLocalIp(match.group(1)): results.append(netutils.createTcpEndpoint(match.group(1), match.group(2))) return results
def getEndpointsFromListener(self): endpoints = [] resolver = dns_resolver.create(self.__shell) if self.__listenerStatus: raw_endpoints = re.findall('HOST=([\w\.\-]+)\)\s*\(PORT=(\d+)\)', self.__listenerStatus) if raw_endpoints: for endpoint in raw_endpoints: if netutils.isValidIp(endpoint[0]): endpoints.append(endpoint) else: try: ips = resolver.resolve_ips(endpoint[0]) for ip in ips: if netutils.isValidIp(str(ip)): endpoints.append((ip, endpoint[1])) except: logger.debugException('') return endpoints
def _resolveEndpoint(resolver, endpoint): r'@types: dns_resolver.DnsResolver, Endpoint -> list[Endpoint]' address = endpoint.getAddress() if netutils.isValidIp(address): return [endpoint] resolveAddressFn = Sfn(resolver.resolveIpsByHostname, fallbackFn=constantly([])) return filter(None, (netutils.updateEndpointAddress(endpoint, ip) for ip in resolveAddressFn(address)))
def createURLOSHV(urlString, framework = None): OSHVResult = ObjectStateHolderVector() #urlOSH2 = modeling.createOshByCmdbIdString('uri_endpoint', urlId) logger.debug("Starting URL discovery on '%s'" % urlString) #urlString = urlString[1:len(urlString)-1] if not urlString: return OSHVResult try: urlString = str(urlString).replace("\\", "//") urlObject = URL(urlString) hostname = urlObject.getHost() if not hostname: raise MalformedURLException("Hostname is not defined in URL '%s'" % urlString) urlObjectResolver = URLObjectResolver(urlObject) protocol = urlObjectResolver.getProtocolFromUrlObject() if not protocol: raise Exception("Failed to resolve the http/https protocol from specified URL") port = urlObjectResolver.getPortFromUrlObject() if not port: raise Exception("Failed to resolve the port number from specified URL") # get topology # create business element CI and attach the url as configuration document CI to it ips = urlObjectResolver.getIpFromUrlObject() for ipAddress in ips: logger.debug('%s: Reporting ip address: %s' % (urlString, ipAddress)) if not ipAddress or not netutils.isValidIp(ipAddress) or netutils.isLocalIp(ipAddress): raise Exception("Failed to resolve the IP address of server from specified URL") hostOSH, ipOSH, OSHVResult2 = createIPEndpointOSHV(framework, ipAddress, port, protocol, hostname) OSHVResult.addAll(OSHVResult2) # create UriEndpoint and relations between business element and UriEndpoint urlOSH = modeling.createUrlOsh(hostOSH, urlString, None) #urlOSH.setCmdbObjectId(urlOSH2.getCmdbObjectId()) OSHVResult.add(urlOSH) OSHVResult.add(modeling.createLinkOSH('dependency', urlOSH, ipOSH)) #create Web Server except: logger.warnException("Error creating URL OSH for %s" % urlString) return OSHVResult
def __init__(self, commandLineDescriptor, ip = None): ''' @types: jee.JvmCommandLineDescriptor, str @raise ValueError: Empty command line descriptor @raise ValueError: IP is not valid ''' self.__cmdDescriptor = commandLineDescriptor if ip is not None and not netutils.isValidIp(ip): raise ValueError("IP is not valid: %s" % ip) self.__ip = ip
def parseNslookupOutput(nslookupOutput, hostname): if re.search(r"Non-existent host", nslookupOutput): return None pattern = "Name:[^\n]+\s*\nAddress:\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})" matcher = re.search(pattern, nslookupOutput, re.I) if matcher: resolvedIp = matcher.group(1) if resolvedIp is not None: resolvedIp = resolvedIp.strip() if netutils.isValidIp(resolvedIp) and not netutils.isLocalIp(resolvedIp): return resolvedIp
def getOracleServerIP(self): try: host_address_result = self._client.executeQuery( "select UTL_INADDR.get_host_address from dual") while host_address_result.next(): ip = host_address_result.getString(1) if netutils.isValidIp(ip) and not netutils.isLoopbackIp(ip): return ip except: logger.debugException('')
def __parseClusterResourceNameAndIps(self, output): result = [] if output: ips = re.findall( '\(([\w\.\-]+)\)\s+Address\s+(\d+\.\d+\.\d+\.\d+)', output) logger.debug('Got reources output "%s"' % output) for ip in ips: if ip and netutils.isValidIp(ip[1]): result.append(ip) return result
def resolveAddress(resolveFn, destinationIps, address): r'''@types: (str -> list[str]), list[str], str -> list[str] Decorate resolverFn with additional cases related to the web dispatcher - asterisk as address used to enable binding for any available interface ''' if netutils.isValidIp(address): return address if address == '*': return filter(netutils.isValidIp, destinationIps) return resolveFn(address)
def DiscoveryMain(Framework): OSHVResult = ObjectStateHolderVector() businessElementId = Framework.getDestinationAttribute('id') urlString = Framework.getDestinationAttribute('url') jobId = Framework.getDiscoveryJobId() dnsServers = Framework.getParameter('dnsServers') or None shell = None if dnsServers: dnsServers = [ dnsServer for dnsServer in dnsServers.split(',') if dnsServer and dnsServer.strip() ] or None if dnsServers: logger.debug('Using dns servers: ', dnsServers) if not urlString: msg = "There is no specified URL in the input BusinessElement CI" errormessages.resolveAndReport(msg, jobId, Framework) return OSHVResult try: bizOSH = modeling.createOshByCmdbIdString('business_element', businessElementId) OSHVResult.add(bizOSH) urlString = urlString[1:len(urlString) - 1] if netutils.isValidIp(urlString): productName = Framework.getDestinationAttribute('product') OSHVResult.add( scp.createScpOsh(bizOSH, 'tcp', urlString, 0, productName)) else: protocol, hostname, port, context = parseUrl(urlString) if not hostname: raise ValueError("Hostname is not defined in URL '%s'" % urlString) if not protocol: raise ValueError( "Failed to resolve the protocol from specified URL") shell = shellutils.ShellUtils( Framework.createClient( ClientsConsts.LOCAL_SHELL_PROTOCOL_NAME)) OSHVResult.addAll( scp.createScpOSHV(bizOSH, protocol, hostname, port, context, shell, dnsServers=dnsServers)) except ValueError, e: errormessages.resolveAndReport(e.message, jobId, Framework)
def processNetDeviceInfo(ipAddress, netDeviceName, ipAddressList, netDeviceOSHV, allowDnsLookup): try: netDeviceOSH = ipOSH = None ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processNetDeviceInfo] Got Net Device <%s>' % netDeviceName) ## Check if this NetDevice is already in the OSHV netDeviceOSH = ciscoworks_utils.getCiByAttributesFromOSHV(netDeviceOSHV, 'netdevice', {'data_externalid':netDeviceName}) if netDeviceOSH: ciscoworks_utils.debugPrint(2, '[' + SCRIPT_NAME + ':processNetDeviceInfo] CI for Net Device <%s> exists in UCMDB' % netDeviceName) else: ## Try and get a DNS name for this node netDeviceDnsName = None if allowDnsLookup and ipAddress and netutils.isValidIp(ipAddress): netDeviceDnsName = netutils.getHostName(ipAddress) ciscoworks_utils.debugPrint(4, '[' + SCRIPT_NAME + ':processNetDeviceInfo] Got DNS name <%s> for Net Device <%s> using IP <%s>' % (netDeviceDnsName, netDeviceName, ipAddress)) ## Discard IP if this is a duplicate if ipAddress and netutils.isValidIp(ipAddress) and ipAddress in ipAddressList: logger.debug('Ignoring duplicate IP <%s> on Net Device <%s>...' % (ipAddress, netDeviceName)) else: ipAddressList.append(ipAddress) ## Check for a valid IP if ipAddress and netutils.isValidIp(ipAddress): ipOSH = modeling.createIpOSH(ipAddress, None, netDeviceDnsName, None) else: logger.debug('Ignoring duplicate IP <%s>...' % netDeviceName) ## If an IP is available, build a Net Device CI if ipOSH: ciscoworks_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processNetDeviceInfo] Creating CI for Net Device <%s> with <%s> as key' % (netDeviceName, ipAddress)) netDeviceOSH = modeling.createHostOSH(ipAddress, 'netdevice') netDeviceOSH.setAttribute('data_externalid', netDeviceName) if netDeviceName and not netutils.isValidIp(netDeviceName): netDeviceOSH.setAttribute('name', netDeviceName) if netDeviceDnsName: netDeviceOSH.setAttribute('primary_dns_name', netDeviceDnsName) netDeviceOSHV.add(netDeviceOSH) return (netDeviceOSH, ipOSH) except: excInfo = logger.prepareJythonStackTrace('') logger.warn('[' + SCRIPT_NAME + ':processNetDeviceInfo] Exception: <%s>' % excInfo) pass
def _resolveHostNameWithCaching(self, hostName): # TODO: use external resolver ip = None if self.__resolveCache.has_key(hostName): ip = self.__resolveCache.get(hostName) else: ip = netutils.getHostAddress(hostName) self.__resolveCache[hostName] = ip if ip and netutils.isValidIp(ip) and not netutils.isLocalIp(ip): return ip
def createHostOsh(self, rs, prefix): hid = rs.getString(prefix + 'hid') ipaddr = rs.getString(prefix + 'Addr') if ipaddr and netutils.isValidIp(ipaddr): hostOsh = modeling.createHostOSH(ipaddr, filter_client_ip=True) elif hid: hostOsh = modeling.createOshByCmdbIdString('host', hid) else: logger.debug('Not enough info to create host from network connection data %s' % prefix) hostOsh = None return hostOsh, hid
def _resolveNames(self, ips_dict): result_dict = {} if ips_dict: for (lpar_id, ip) in ips_dict.items(): result_dict[lpar_id] = ip if ip and not netutils.isValidIp(ip): #ip might be a dns name, will try to resolve it ipAddress = netutils.resolveIP(self._shell, ip) if ipAddress: result_dict[lpar_id] = ipAddress return result_dict
def __init__(self, hostname, ipAddress): r'''@types: str, str @raise ValueError: hostname is empty @raise ValueError: Invalid IP address ''' if not hostname: raise ValueError("hostname is empty") if not (ipAddress and netutils.isValidIp(ipAddress)): raise ValueError("Invalid IP address") self.__hostname = hostname self.__ipAddress = ipAddress
def parseTransportAdaptersForNode(node, scconfOutput): adaptersList = [] matcher = re.search(r"\(%s\) Node transport adapters:([^\n]+)" % re.escape(node.name), scconfOutput) if matcher: adaptersListStr = matcher.group(1) and matcher.group(1).strip() if adaptersListStr: adaptersList = re.split(r"\s+", adaptersListStr) adaptersList = [adapter for adapter in adaptersList if adapter] if adaptersList: for adapterName in adaptersList: adapterStatus = None tuple = (re.escape(node.name), re.escape(adapterName)) matcher = re.search(r"\(%s:%s\) Adapter enabled:([^\n]+)" % tuple, scconfOutput) if matcher: adapterStatus = matcher.group(1) and matcher.group(1).strip() if adapterStatus != 'yes': logger.debug("Skipping disabled transport adapter '%s'" % adapterName) continue logger.debug("Found transport adapter '%s'" % adapterName) transportAdapter = TransportAdapter(adapterName) properties = {} results = re.findall(r"\(%s:%s\) Adapter property:([^\n]+)" % tuple, scconfOutput) if results: for row in results: elements = re.split(r"=", row, 1) if len(elements) == 2: propertyName = elements[0] and elements[0].strip() propertyValue = elements[1] and elements[1].strip() if propertyName and propertyValue: properties[propertyName] = propertyValue ip = properties.get('ip_address') if ip and netutils.isValidIp(ip): transportAdapter.ip = ip logger.debug("Adapter's private IP is '%s'" % ip) else: logger.warn("Could not find private IP for transport adapter '%s' on node '%s'" % (adapterName, node.name)) netmask = properties.get('netmask') if netmask: try: transportAdapter.netmask = netutils.parseNetMask(netmask) except: logger.warn("Failed parsing netmask: %s" % netmask) node.transportAdaptersByName[adapterName] = transportAdapter else: logger.warn("No transport adapters found for node '%s'" % node.name)