def findDatabases(localClient, procToPortDict, dbInstanceDict, isWindows='true', wmiRegistryClient=None): try: ## DB2 cannot be discovered through an SNMP/WMI agent localClientType = localClient.getClientType() if localClientType not in ['telnet', 'ssh', 'ntadmin']: logger.error('[' + SCRIPT_NAME + ':findDatabase] DB2 discovery requires SSH/Telnet/NTCMD') return ## The best approach to find DB2 instances is to make a list of ## locations where DB2 may be installed and search through them. searchLocations = [] ## Extract information from process to port dictionary first ## For DB2, it is not possible to get database details from this ## dictionary. the best approach is to get possible install ## locations of DB2 and find databases later processProcToPortDict(localClient, isWindows, procToPortDict, searchLocations) ## Use the list of possible install locations to identify valid ## install locations instanceLocations = getInstancePaths(localClient, isWindows, searchLocations) # used for debugging purposes only - Daniel La for instancePath in instanceLocations: logger.debug('***********instance path is: ' + instancePath) ## Get databases using instance locations if instanceLocations: getDatabases(localClient, isWindows, instanceLocations, dbInstanceDict) except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':findDatabases] Exception: <%s>' % excInfo) pass
def getDbDirectory(localClient, listDbDirectoryCommand): try: DbDirectory = localClient.executeCmd(listDbDirectoryCommand) return DbDirectory except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':getDbDirectory] Exception: <%s>' % excInfo) pass
def findDatabases(localClient, procToPortDict, dbInstanceDict, database_ip_service_endpoints, isWindows='true', wmiRegistryClient=None): try: ## Extract information from process to port dictionary first processProcToPortDict(localClient, procToPortDict, dbInstanceDict, database_ip_service_endpoints) ## Search for MSSQL related stuff in the registry if localClient.getClientType() != 'snmp' or wmiRegistryClient != None: registryLookup(procToPortDict, dbInstanceDict, localClient, wmiRegistryClient) except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':findDatabases] Exception: <%s>' % excInfo) pass
def getProcListBySNMP(localClient): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcListBySNMP]') procToPortDict = {} HOST_IP = localClient.getIpAddress() ## Not using SNMP HR scripts since they are not split into a main discovery and ## utils script. Using them will result in those discoveries being executed for ## this destination and host resource data will be added to the CMDB ## Not getting services using SNMP because only running services are ## available, and if a service is running, we would already have the database ## in the list of running processes. Service path, which would be useful, is ## not available by SNMP ## Not getting software either because only installed software names are ## available. Version and path information are not available. ## Get a list of processes try: resultSet = localClient.executeQuery('1.3.6.1.2.1.25.4.2.1.1,1.3.6.1.2.1.25.4.2.1.2,string,1.3.6.1.2.1.25.4.2.1.4,string,1.3.6.1.2.1.25.4.2.1.5,string,1.3.6.1.2.1.25.5.1.1.2,string,1.3.6.1.2.1.25.5.1.1.1,string,1.3.6.1.2.1.25.4.2.1.2,string') while resultSet.next(): ## Name processName = resultSet.getString(7).strip() if processName == None or processName == '' or len(processName) <1: ## We don't care about nameless processes continue pid = resultSet.getString(2).strip() ## PID processPath = resultSet.getString(3).strip() ## Path processPath = string.replace(processPath, '"', '') processCmdline = resultSet.getString(4).strip() ## Command line processCmdline = string.replace(processCmdline, '"', '') ## Add this to the dictionary dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcListBySNMP] Got PROCESS <%s:%s> with path <%s> and command line <%s>' % (pid, processName, processPath, processCmdline)) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} procToPortDict[pid] = [processName, dbconnect_utils.UNKNOWN, HOST_IP, processPath, dbconnect_utils.UNKNOWN, 'Running', processCmdline] except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcListBySNMP] Unable to get a list of proceses: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcListBySNMP] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcListBySNMP] Returning EMPTY process to port dictionary') return None except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':getProcListBySNMP] Exception: <%s>' % excInfo) pass
def findDatabases(localClient, procToPortDict, dbInstanceDict, isWindows='true', wmiRegistryClient=None): try: ## Extract information from process to port dictionary first processProcToPortDict(localClient, procToPortDict, dbInstanceDict) ## Search for tnsnames.ora if we have a shell connection if localClient.getClientType() != 'wmi' and localClient.getClientType() != 'snmp': if not getInformationFromListeners(localClient, procToPortDict, dbInstanceDict): install_locs = parseEtcFiles(localClient, procToPortDict, dbInstanceDict, isWindows) findTnsnamesOra(localClient, procToPortDict, dbInstanceDict, isWindows, install_locs) except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':findDatabases] Exception: <%s>' % excInfo) pass
def getAIXpIDfromAddress(localClient, procAddress, USE_SUDO): try: kdbOut = '' kdbCmd = '' pidLine = '' kdbOutLines = [] try: kdbCmd = 'echo "sockinfo ' + procAddress + ' tcpcb" | kdb | grep ACTIVE' if USE_SUDO == 'true': kdbCmd = 'sudo ' + kdbCmd kdbOut = localClient.executeCmd(kdbCmd) ## Output: pvproc+00E000 56*inetd ACTIVE 003808A 00360AC 0000000001244400 0 0001 except: excInfo = logger.prepareJythonStackTrace('') logger.warn('[' + SCRIPT_NAME + ':getAIXpIDfromAddress] Error: Couldn\'t execute <%s>: <%s>' % (kdbCmd, excInfo)) return None ## if (kdbOut.find('do not allow') != -1): logger.debug('[' + SCRIPT_NAME + ':getAIXpIDfromAddress] Couldn\'t get info from kdb. Please set suid on /usr/sbin/kdb or use root credentials.') return None ## If output contains multiple lines, split them kdbOutLines = dbconnect_utils.splitCommandOutput(kdbOut.strip()) if kdbOutLines == None: kdbOutLines = kdbOut.strip() dbconnect_utils.debugPrint(5, '[' + SCRIPT_NAME + ':getAIXpIDfromAddress] kdbOutLines before extracting pidLine is <%s> (length=<%s>)' % (kdbOutLines, len(kdbOutLines))) ### We're only interested in the line with string "ACTIVE" in it if len(kdbOutLines) > 0: for kdbOutLine in kdbOutLines: if re.search('ACTIVE', kdbOutLine): pidLine = kdbOutLine.strip() else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getAIXpIDfromAddress] Unusable KDB output for address <%s>' % procAddress) return None ## Extract process ID hex from output of kbd #m = re.match('\S+\+\w+\s+\d+\*\S+\s+\S+\s+(\w+)\s+\w+\s+\w+\s+\w+\s+\w+\s+\w+\s+.*', pidLine) dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getAIXpIDfromAddress] pidLine is <%s>' % pidLine) m = re.match('.*ACTIVE\s+(\w+)\s+.*', pidLine) if (m): #thePID = str(int(m.group(1), 16)) thePID = str(int(m.group(1), 16)) dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getAIXpIDfromAddress] Found PID <%s> for address <%s>' % (thePID, procAddress)) return thePID else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getAIXpIDfromAddress] Couldn\'t find PID for address [' + procAddress + ']') return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getAIXpIDfromAddress] Exception: <%s>' % excInfo) pass
def findDatabases(localClient, procToPortDict, dbInstanceDict, isWindows='true', wmiRegistryClient=None): try: ## Extract information from process to port dictionary first processProcToPortDict(localClient, procToPortDict, dbInstanceDict) # Commented out below by Daniel La 06/01/11 - Been told not to detect Oracle DBs through these files. So only look # at services and processes to get DB instance names. Also tnsnames.ora may not necessarily be on the Oracle server. This file is used # on an Oracle client. ## Search for tnsnames.ora if we have a shell connection # if localClient.getClientType() != 'wmi' and localClient.getClientType() != 'snmp': # install_locs = parseEtcFiles(localClient, procToPortDict, dbInstanceDict, isWindows) # findTnsnamesOra(localClient, procToPortDict, dbInstanceDict, isWindows, install_locs) except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':findDatabases] Exception: <%s>' % excInfo) pass
def handleProcessToPortByLsof(USE_SUDO, localClient, procToPortDict): try: pidToPortMap = {} # # We need a local pid to port map lsofCmd = 'lsof -n -P -i | grep -i listen 2>/dev/null' if USE_SUDO: lsofCmd = 'sudo ' + lsofCmd lsofStr = localClient.executeCmd(lsofCmd) lsofLines = dbconnect_utils.splitCommandOutput(lsofStr.strip()) if lsofLines != None: for lsofLine in lsofLines: if len(lsofLine) < 1: continue lsofLine = lsofLine.strip() m = re.search( '\w+\s+(\d+)\s+\w+\s+\w+\s+IPv[4|6].+TCP\s+(\S+):(\d+)\s+\(\w+\)', lsofLine) if (m): pid = m.group(1).strip() ipAddress = m.group(2).strip() # # Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP( ipAddress, localClient.getIpAddress()) serverPort = m.group(3).strip() pidToPortMap[pid] = [ipAddress, serverPort] if pidToPortMap != None and len(pidToPortMap) > 0: for pid in pidToPortMap.keys(): if pid in procToPortDict.keys(): ipAddress = (pidToPortMap[pid])[0] # # Skip loopback IPs if re.search('127.0.0', ipAddress): continue serverPort = (pidToPortMap[pid])[1] dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Found port <%s:%s> for pid <%s>' % (ipAddress, serverPort, pid)) (procToPortDict[pid] )[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid] )[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] No TCP port associated with PID [' + pID + ']: ' + lsofLine) else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to make a process to port map using LSOF: ' + lsofStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to make a process to port map using LSOF: <%s>' % excInfo) pass
def handleProcessToPortByPFile(USE_SUDO, localClient, procToPortDict): # # Use PFILES to map each process to a port and create a dictionary ############################################ try: for pID in procToPortDict.keys(): pFilesCmd = 'pfiles ' + pID + ' 2>/dev/null | grep "sockname: AF_INET"' if USE_SUDO: pFilesCmd = 'sudo ' + pFilesCmd pFilesStr = localClient.executeCmd(pFilesCmd) if len(pFilesStr) < 1: continue pFilesLines = dbconnect_utils.splitCommandOutput(pFilesStr.strip()) if pFilesLines == None: dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Error: Invalid output from pfiles: ' + pFilesStr) continue for pFilesLine in pFilesLines: pFilesLine = pFilesLine.strip() m = re.search( '.+AF_INET\s+(\d+\.\d+\.\d+\.\d+)\s+port:\s*(\d+)', pFilesLine) if re.search('AFINET6', pFilesLine): m = re.search( '.+AF_INET6.*:(\d+\.\d+\.\d+\.\d+)\s+port:\s*(\d+)', pFilesLine) if (m) and m.group(2) != '0': ipAddress = m.group(1).strip() ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP( ipAddress, localClient.getIpAddress()) serverPort = m.group(2).strip() dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Adding port <%s:%s> for process <%s>' % (ipAddress, serverPort, (procToPortDict[pID] )[dbconnect_utils.PROCESSNAME_INDEX])) (procToPortDict[pID])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pID] )[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] No TCP port associated with PID [' + pID + ']: ' + pFilesLine) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Unable to make a process to port map using pfiles: <%s>' % excInfo) pass
def DiscoveryMain(Framework): # General variables OSHVResult = ObjectStateHolderVector() properties = Properties() protocol = Framework.getDestinationAttribute('Protocol') processToPortDict = {} ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} databaseDict = {} ## {instanceName/SID:[dbType, listeningPort, ipAddress, installPath, version, status]} database_ip_service_endpoints ={} ## {instanceName:[ip_address:port]} client = None secondClient = None isWindows = 'true' # Attempt to create a client try: client = Framework.createClient() ## We have a client ## Get processes running on the box if(client): dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':DiscoveryMain] Got client <%s>' % client.getClientType()) if client.getClientType() == 'wmi': ## Open a second client connection to the DEFAULT namespace for registry access props = Properties() props.setProperty(AgentConstants.PROP_WMI_NAMESPACE, 'root\\DEFAULT') secondClient = Framework.createClient(props) processToPortDict = dbconnect_agentutils.getProcListByWMI(client, secondClient) # elif client.getClientType() == 'snmp': # processToPortDict = dbconnect_agentutils.getProcListBySNMP(client) else: Framework.reportWarning('Unable to connect using WMI') ## We have process and port infromation ## Find databases, if any if processToPortDict != None and len(processToPortDict) > 0: for pid in processToPortDict.keys(): logger.debug('dddd: ', '[' + SCRIPT_NAME + ':DiscoveryMain] Got process/service/software <%s> listening on port <%s:%s>' % ((processToPortDict[pid])[dbconnect_utils.PROCESSNAME_INDEX], (processToPortDict[pid])[dbconnect_utils.IP_INDEX], (processToPortDict[pid])[dbconnect_utils.PORT_INDEX])) if Framework.getParameter('discover_oracle').strip().lower() == 'true': dbconnect_oracle.findDatabases(client, processToPortDict, databaseDict, isWindows, secondClient) if Framework.getParameter('discover_mssql').strip().lower() == 'true': dbconnect_mssql.findDatabases(client, processToPortDict, databaseDict, database_ip_service_endpoints, isWindows, secondClient) if databaseDict != None and len(databaseDict) > 0: for dbName in databaseDict.keys(): dbconnect_utils.debugPrint('Found <%s> instance <%s> (%s) with listener port <%s:%s> and installed in <%s>' % ((databaseDict[dbName])[dbconnect_utils.DBTYPE_INDEX], dbName, (databaseDict[dbName])[dbconnect_utils.STATUS_INDEX], (databaseDict[dbName])[dbconnect_utils.IP_INDEX], (databaseDict[dbName])[dbconnect_utils.PORT_INDEX], (databaseDict[dbName])[dbconnect_utils.PATH_INDEX])) OSHVResult.addAll(dbconnect_utils.makeDbOSHs(databaseDict)) else: Framework.reportWarning('No databases found') else: ## If we're here, we couldn't find any processes, service, or software ## and we have no data to search for databases Framework.reportWarning('Unable to get a list or processes, services, or installed software') else: dbconnect_utils.debugPrint('Unable to connect using WMI') except Exception, ex: exInfo = ex.getMessage() errormessages.resolveAndReport(exInfo, protocol, Framework)
def handleProcessToPortByLsof(USE_SUDO, localClient, procToPortDict): try: pidToPortMap = {} # # We need a local pid to port map lsofCmd = 'lsof -n -P -i | grep -i listen 2>/dev/null' if USE_SUDO: lsofCmd = 'sudo ' + lsofCmd lsofStr = localClient.executeCmd(lsofCmd) lsofLines = dbconnect_utils.splitCommandOutput(lsofStr.strip()) if lsofLines != None: for lsofLine in lsofLines: if len(lsofLine) < 1: continue lsofLine = lsofLine.strip() m = re.search('\w+\s+(\d+)\s+\w+\s+\w+\s+IPv[4|6].+TCP\s+(\S+):(\d+)\s+\(\w+\)', lsofLine) if (m): pid = m.group(1).strip() ipAddress = m.group(2).strip() # # Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP(ipAddress, localClient.getIpAddress()) serverPort = m.group(3).strip() pidToPortMap[pid] = [ipAddress, serverPort] if pidToPortMap != None and len(pidToPortMap) > 0: for pid in pidToPortMap.keys(): if pid in procToPortDict.keys(): ipAddress = (pidToPortMap[pid])[0] # # Skip loopback IPs if re.search('127.0.0', ipAddress): continue serverPort = (pidToPortMap[pid])[1] dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Found port <%s:%s> for pid <%s>' % ( ipAddress, serverPort, pid)) (procToPortDict[pid])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid])[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] No TCP port associated with PID [' + pID + ']: ' + lsofLine) else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to make a process to port map using LSOF: ' + lsofStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to make a process to port map using LSOF: <%s>' % excInfo) pass
def handleProcessToPortByPFile(USE_SUDO, localClient, procToPortDict): # # Use PFILES to map each process to a port and create a dictionary ############################################ try: for pID in procToPortDict.keys(): pFilesCmd = 'pfiles ' + pID + ' 2>/dev/null | grep "sockname: AF_INET"' if USE_SUDO: pFilesCmd = 'sudo ' + pFilesCmd pFilesStr = localClient.executeCmd(pFilesCmd) if len(pFilesStr) < 1: continue pFilesLines = dbconnect_utils.splitCommandOutput(pFilesStr.strip()) if pFilesLines == None: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Error: Invalid output from pfiles: ' + pFilesStr) continue for pFilesLine in pFilesLines: pFilesLine = pFilesLine.strip() m = re.search('.+AF_INET\s+(\d+\.\d+\.\d+\.\d+)\s+port:\s*(\d+)', pFilesLine) if re.search('AFINET6', pFilesLine): m = re.search('.+AF_INET6.*:(\d+\.\d+\.\d+\.\d+)\s+port:\s*(\d+)', pFilesLine) if (m) and m.group(2) != '0': ipAddress = m.group(1).strip() ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP(ipAddress, localClient.getIpAddress()) serverPort = m.group(2).strip() dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Adding port <%s:%s> for process <%s>' % ( ipAddress, serverPort, (procToPortDict[pID])[dbconnect_utils.PROCESSNAME_INDEX])) (procToPortDict[pID])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pID])[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] No TCP port associated with PID [' + pID + ']: ' + pFilesLine) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Unable to make a process to port map using pfiles: <%s>' % excInfo) pass
def getProcToPortDictOnWindows(localClient, localFramework): try: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows]') procToPortDict = {} shell = shellutils.ShellUtils(localClient) ntcmdErrStr = 'Remote command returned 1(0x1)' HOST_IP = localClient.getIpAddress() HOSTID = localFramework.getDestinationAttribute('hostId') ## Get process OSHs using NTCMD HR script ############################################ try: processOSHV = ObjectStateHolderVector() if (NTCMD_HR_Dis_Process_Lib.discoverProcessesByWmic( shell, processOSHV, HOSTID, localFramework)) == 1 or ( NTCMD_HR_Dis_Process_Lib.discoverProcesses( shell, processOSHV, HOSTID, localFramework)) == 1: ## We have an OSHV, extract OSHs from it oshvIndex = 0 try: while processOSHV.get(oshvIndex): someOSH = processOSHV.get(oshvIndex) if someOSH.getObjectClass() == 'process': processDict = dbconnect_utils.getAttributeValuesFromOSH( someOSH, [ 'process_pid', 'data_name', 'process_cmdline', 'process_path' ]) ## Name processName = processDict['data_name'] if processName == None or processName == '' or len( processName) < 1: ## We don't care about nameless processes continue pid = processDict['process_pid'] ## PID processPath = processDict['process_path'] ## Path processPath = string.replace(processPath, '"', '') processCmdline = processDict[ 'process_cmdline'] ## Command line processCmdline = string.replace( processCmdline, '"', '') ## Add this to the dictionary dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got PROCESS <%s:%s> with path <%s> and command line <%s>' % (pid, processName, processPath, processCmdline) ) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [processName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, processPath, dbconnect_utils.UNKNOWN, 'Running', processCmdline] if dbconnect_utils.populateProcToPortDict( procToPortDict, pid, processName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, processPath, dbconnect_utils.UNKNOWN, 'Running', processCmdline) == 0: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, processName, 'Running', processPath, processCmdline)) oshvIndex = oshvIndex + 1 except ArrayIndexOutOfBoundsException: dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Array OOB exception while getting process CIs from OSHV. Ignoring because this is expected...' ) pass else: ## We don't have an OSHV dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to get list of processes' ) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to get list of processes: <%s>' % excInfo) pass ## Add windows services to the list of processes ############################################ ## First try WMIC because we can get a PID ## If that doesn't work, fallback on the OOTB NTCMD HR script try: buffer = shell.execCmd( 'wmic service get displayname, pathname, processid, started /format:csv < %SystemRoot%\win.ini' ) dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Output for wmic process command: %s' % buffer) reg_mamRc = shell.getLastCmdReturnCode() if (reg_mamRc == 0): ## WMIC worked!! wmicLines = buffer.split('\n') fakePid = 0 # Each line: HOSTNAME,SERVICENAME,EXE-PATH,PID for wmicLine in wmicLines: tokens = wmicLine.split(',') numTokens = len(tokens) if (tokens == None) or (numTokens < 1): continue if tokens[0].strip() == 'Node': continue if (numTokens < 4): continue serviceName = tokens[numTokens - 4].strip() serviceStatus = dbconnect_utils.UNKNOWN if tokens[numTokens - 1].strip().lower() == 'true': serviceStatus = 'Running' else: serviceStatus = 'Not Running' pid = tokens[numTokens - 2].strip() ## Don't bother with SYSTEM services that have a pid of -1 if (pid != '-1' and pid.isnumeric()): # Get the command line serviceCmdline = tokens[numTokens - 3].strip() serviceCmdline = serviceCmdline.strip()[0:2499] serviceCmdline = string.replace( serviceCmdline, '"', '') # Set process path to command line servicePath = serviceCmdline ## While using services, we sometimes need a fake PID because ## the service may not be running and the corresponding PID will be 0 if pid == '0': pid = 'SERVICE ' + str(fakePid) fakePid = fakePid + 1 dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Using fake PID <%s> for service <%s>' % (pid, serviceName)) ## Got everything, make the array dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got SERVICE <%s (%s)> with PID <%s>, command line <%s>, command path <%s>' % (serviceName, serviceStatus, pid, serviceCmdline, servicePath)) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [serviceName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline] if dbconnect_utils.populateProcToPortDict( procToPortDict, pid, serviceName, dbconnect_utils.UNKNOWN, HOST_IP, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline) == 0: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add SERVICE <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, serviceName, serviceStatus, servicePath, serviceCmdline)) else: ## WMIC didn't work. Get service OSHs using NTCMD HR script dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] WMIC didn\'t work, trying NTCMD HR script' ) servicesOSHV = NTCMD_HR_REG_Service_Lib.doService( shell, modeling.createHostOSH(HOST_IP)) ## Extract OSHs from vector oshvIndex = 0 try: while servicesOSHV.get(oshvIndex): someOSH = servicesOSHV.get(oshvIndex) if someOSH.getObjectClass() == 'service': serviceDict = dbconnect_utils.getAttributeValuesFromOSH( someOSH, [ 'data_name', 'service_pathtoexec', 'service_commandline', 'service_operatingstatus' ]) ## Name serviceName = serviceDict['data_name'] if serviceName == None or serviceName == '' or len( serviceName) < 1: ## We don't care about nameless services continue pid = 'SERVICE ' + str(oshvIndex) ## PID (fake) servicePath = serviceDict[ 'service_pathtoexec'] ## Install path servicePath = string.replace(servicePath, '"', '') serviceCmdline = serviceDict[ 'service_commandline'] ## Command line serviceCmdline = string.replace( serviceCmdline, '"', '') serviceStatus = serviceDict[ 'service_operatingstatus'] ## Status ## Add this to the dictionary dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got <%s:%s> with installPath <%s> and commandline <%s>' % (pid, serviceName, servicePath, serviceCmdline) ) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [serviceName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline] if dbconnect_utils.populateProcToPortDict( procToPortDict, pid, serviceName, dbconnect_utils.UNKNOWN, HOST_IP, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline) == 0: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add SERVICE <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, serviceName, serviceStatus, servicePath, serviceCmdline)) oshvIndex = oshvIndex + 1 except ArrayIndexOutOfBoundsException: dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Array OOB exception while getting service CIs from OSHV. Ignoring because this is expected...' ) pass except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to get list of services: <%s>' % excInfo) pass ## Add installed software to the list of processes ############################################ try: ## Get installed software OSHs using NTCMD HR script softwareOSHV = ObjectStateHolderVector() gotSoftwareOshs = NTCMD_HR_REG_Software_Lib.doSoftware( shell, modeling.createHostOSH(HOST_IP), softwareOSHV) ## Extract OSHs from vector oshvIndex = 0 while gotSoftwareOshs and softwareOSHV.get(oshvIndex): someOSH = softwareOSHV.get(oshvIndex) if someOSH.getObjectClass() == 'software': softwareDict = dbconnect_utils.getAttributeValuesFromOSH( someOSH, [ 'data_name', 'software_installpath', 'software_version' ]) ## Name softwareName = softwareDict['data_name'] if not softwareName: ## We don't care about nameless software continue pid = 'SOFTWARE ' + str(oshvIndex) ## PID (fake) softwareInstallPath = softwareDict[ 'software_installpath'] ## Install path softwareVersion = softwareDict[ 'software_version'] ## Version ## Add this to the dictionary dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got <%s:%s> with installPath <%s> and version <%s>' % (pid, softwareName, softwareInstallPath, softwareVersion)) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [softwareName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, softwareInstallPath, softwareVersion, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN] if dbconnect_utils.populateProcToPortDict( procToPortDict, pid, softwareName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, softwareInstallPath, softwareVersion, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN) == 0: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add SOFTWARE <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, softwareName, dbconnect_utils.UNKNOWN, softwareInstallPath, dbconnect_utils.UNKNOWN)) oshvIndex = oshvIndex + 1 except ArrayIndexOutOfBoundsException: dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Array OOB exception while getting software CIs from OSHV. Ignoring because this is expected...' ) pass except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to get list of software: <%s>' % excInfo) pass ## Use NETSTAT output to create an array of server ports ## and map them to server processes ############################################ try: netstatLisStr = shell.execCmd( 'netstat -aon -p tcp | find "LISTENING"') nsStrOk = 'false' nsLisLines = None if netstatLisStr.find(ntcmdErrStr) != -1 or len(netstatLisStr) < 1: nsStrOk = 'false' elif re.search('\r\n', netstatLisStr): nsLisLines = netstatLisStr.split('\r\n') nsStrOk = 'true' elif re.search('\n', netstatLisStr): nsLisLines = netstatLisStr.split('\n') nsStrOk = 'true' if nsStrOk == 'true': for line in nsLisLines: line = line.strip() m = re.search( '\S+\s+(\d+.\d+.\d+.\d+):(\d+)\s+\d+.\d+.\d+.\d+:\d+\s+\S+\s+(\d+).*', line) if (m): ipAddress = m.group(1).strip() ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP( ipAddress, localClient.getIpAddress()) serverPort = m.group(2).strip() pid = m.group(3) dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got port <%s> for pid <%s>' % (serverPort, pid)) if pid != '-' and procToPortDict.has_key(pid): dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Adding port <%s:%s> for process <%s>' % (ipAddress, serverPort, (procToPortDict[pid])[0])) (procToPortDict[pid] )[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid] )[dbconnect_utils.PORT_INDEX] = serverPort dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got port <%s> for pid <%s>' % (serverPort, pid)) else: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Couldn\'t get process information (Most likely due to lack of user permissions): ' + line) else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Invalid output from netstat: <%s>' % netstatLisStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to make a process to port map using netstat: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Returning EMPTY process to port dictionary' ) return None except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Exception: <%s>' % excInfo) pass
def findTnsnamesOra(localClient, p2pDict, dbDict, isWindows, installLocs): try: ## Locals searchLocations = [] # Try locations in the database dictionary if len(dbDict) > 0: for sid in dbDict.keys(): if (dbDict[sid])[dbconnect_utils.PATH_INDEX] != dbconnect_utils.UNKNOWN: path = (dbDict[sid])[dbconnect_utils.PATH_INDEX].lower() if path in searchLocations: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [1] <%s> already in search locations' % path) continue elif path.find('\\') > 0 or path.find('/') >= 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':findTnsnamesOra] [1] Adding <%s> to search locations' % path) searchLocations.append(path) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [1] <%s> is not a valid path' % path) continue # Try locations in the p2p dictionary if len(p2pDict) > 0: for pid in p2pDict.keys(): processName = (p2pDict[pid])[dbconnect_utils.PROCESSNAME_INDEX].lower() path = (p2pDict[pid])[dbconnect_utils.PATH_INDEX].lower() if re.search('tns', processName) or re.search('dbconsole', processName) or re.search('jobscheduler', processName) or re.search('oradb', processName) or re.search('oracle', processName) or re.search('ora_', processName): ## Remove /bin/tnslsnr from TNS process path if re.search('/bin/tnslsnr', path): path = path[:path.find('/bin/tnslsnr')] if path in searchLocations: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [2] <%s> already in search locations' % path) continue elif path.find('\\') > 0 or path.find('/') >= 0: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [2] Adding <%s> to search locations' % path) searchLocations.append(path) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [2] <%s> is not a valid path' % path) continue # If we have no search locations so far, try some known/standard ones if 1: #len(searchLocations) < 1: if isWindows == 'true': searchLocations.append('%HOMEDRIVE%\oracle') searchLocations.append('%SYSTEMDRIVE%\oracle') searchLocations.append('%PROGRAMFILES%\oracle') searchLocations.append('%PROGRAMFILES(x86)%\oracle') searchLocations.append('%ORA_HOME%') searchLocations.append('%ORACLE_HOME%') #searchLocations.append('%ORACLE_HOME%\\network\\admin') else: searchLocations.append('/u01') searchLocations.append('/u02') searchLocations.append('/opt') searchLocations.append('/usr/local') searchLocations.append('$ORACLE_HOME') #searchLocations.append('$ORACLE_HOME/network/admin') searchLocations.append('$ORA_HOME') searchLocations.append('$ORACLE_BASE') # Add oracle paths found from other sources if installLocs and len(installLocs) > 0: for installLoc in installLocs: if installLoc and len(installLoc) > 0: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [3] Adding <%s> to search locations' % installLoc) searchLocations.append(installLoc) # Search filesystem and parse tnsnames.ora entries for location in searchLocations: tnsnamesLocations = dbconnect_utils.findFile(localClient, 'tnsnames.ora', location, isWindows) if tnsnamesLocations == None: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] No tnsnames.ora found in <%s>' % location) continue for tnsnamesLocation in tnsnamesLocations: # We don't want the sample TNSNAMES.ORA which is # installed by default if re.search('sample', tnsnamesLocation.lower()): dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] Skipping sample tnsnames.ora in <%s>' % tnsnamesLocation) continue tnsnamesContent = dbconnect_utils.getFileContent(localClient, tnsnamesLocation, isWindows) dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':findTnsnamesOra] Got content of <%s>: <%s>' % (tnsnamesLocation, tnsnamesContent)) if tnsnamesContent != None or tnsnamesContent != '' or len(tnsnamesContent) <1: tnsEntries = dbutils.parseTNSNames(tnsnamesContent, '') for tnsEntry in tnsEntries: sidFound = tnsEntry[3].strip().lower() ## Truncate domain name if this is fully qualified SID if sidFound.find('.') > 0: shortSID = sidFound[:sidFound.find('.')] dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':findTnsnamesOra] Stripping domain from SID <%s> to <%s>' % (sidFound, shortSID)) sidFound = shortSID tnslsnrPort = tnsEntry[2].strip().lower() ipAddress = dbconnect_utils.fixIP(tnsEntry[5].strip().lower(), localClient.getIpAddress()) if ipAddress == dbconnect_utils.UNKNOWN: dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':findTnsnamesOra] Skipping instance <%s> listening at port <%s> because it\'s IP address is not valid' % (sidFound, tnslsnrPort)) continue if sidFound in dbDict.keys(): installPath = (dbDict[sidFound])[dbconnect_utils.PATH_INDEX] version = (dbDict[sidFound])[dbconnect_utils.VERSION_INDEX] statusFlag = (dbDict[sidFound])[dbconnect_utils.STATUS_INDEX] # If port and IP are already populated, don't overwrite them # because this information from active processes (above) is # guaranteed to be correct and tnsnames.ora may not be up-to-date ## Vinay 01/04/2010 - Commenting out conditional update below ## because the port and IP for an Oracle instance is not on the Oracle ## process but on the TNS listener process which may be listening for ## multiple instances on different ports. This makes associating an ## Oracle instance to its corresponding port impossible. ## So any ports found in TNSnames.ora will be used to overwrite ## previously found ports # if (dbDict[sidFound])[dbconnect_utils.PORT_INDEX] != dbconnect_utils.UNKNOWN: # tnslsnrPort = (dbDict[sidFound])[dbconnect_utils.PORT_INDEX] # if (dbDict[sidFound])[dbconnect_utils.IP_INDEX] != dbconnect_utils.UNKNOWN: # ipAddress = (dbDict[sidFound])[dbconnect_utils.IP_INDEX] dbDict[sidFound] = ['oracle', tnslsnrPort, ipAddress, installPath, version, statusFlag] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] Found known Oracle instance <%s> listening at port <%s> on <%s>' % (sidFound, tnslsnrPort, ipAddress)) else: dbDict[sidFound] = ['oracle', tnslsnrPort, ipAddress, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] Added Oracle instance <%s> listening at port <%s> on <%s>' % (sidFound, tnslsnrPort, ipAddress)) else: logger.debug('[' + SCRIPT_NAME + ':findTnsnamesOra] Invalid TNSNAMES.ORA at <%s>: <%s>' % (tnsnamesLocation, tnsnamesContent)) except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':findTnsnamesOra] Exception: <%s>' % excInfo) pass
def processProcToPortDict(localClient, isWindows, p2pDict, possibleInstallLocations): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict]') localShellUtils = shellutils.ShellUtils(localClient) windowsServer = localShellUtils.isWinOs() ## Some locals to avoid searching the same locations more than once checkedPaths = [] for pid in p2pDict.keys(): processName = (p2pDict[pid])[dbconnect_utils.PROCESSNAME_INDEX].lower() listenerPort = (p2pDict[pid])[dbconnect_utils.PORT_INDEX] ipAddress = (p2pDict[pid])[dbconnect_utils.IP_INDEX] if ipAddress == dbconnect_utils.UNKNOWN: ipAddress = localClient.getIpAddress() path = (p2pDict[pid])[dbconnect_utils.PATH_INDEX] statusFlag = (p2pDict[pid])[dbconnect_utils.STATUS_INDEX] userName = (p2pDict[pid])[dbconnect_utils.USER_INDEX] ## **** This is a dummy filter for now and will be populated ## **** with more info as and when available if re.search('some_non_db2_process', processName): ## Filters: If we don't skip these, the next checks will ## catch them and identify incorrect instances dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':processProcToPortDict] (1) Found process name <%s>. Ignoring...' % processName) continue ## Look for DB2 install locations using known process/service/software names elif re.search('db2', processName): ## Daniel La: This box has DB2. We will look for userName in /etc/passwd to find it's home directory. This home directory is what should ## be used to search for possible DB2 install paths. This information was provided by Felix Iwan from DB2 team. if dbconnect_utils.isValidString(userName): passwdentry = None grepCommand = 'grep ' + userName.strip() + ' /etc/passwd | grep -v root' # ignore root user account passwdentry = localClient.executeCmd(grepCommand) m = re.search('.*:.*:.*:.*:.*:(.*):.*', passwdentry) if m: userHomeDirectory = m.group(1) # logger.debug('userHomeDirectory is: ', userHomeDirectory) if userHomeDirectory.lower() not in checkedPaths: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] (2.1) Found possible DB2 install path <%s>' % userHomeDirectory) possibleInstallLocations.append(userHomeDirectory) checkedPaths.append(userHomeDirectory.lower()) else: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':processProcToPortDict] (2.1) Skipping path <%s> since it has been processed before' % userHomeDirectory) else: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':processProcToPortDict] (2.1) Invalid username for process/service/software <%s>' % processName) # Daniel La - below is the original of the above.. I've commented it out as we are using the /etc/passwd file to determine paths to search # instead of prefixing usernames with /home/. ## This box has DB2. Check if a path and/or username are available to find DB2 instances #if dbconnect_utils.isValidString(userName): # userHomeDirectory = '/home/%s' % userName.strip() # if userHomeDirectory.lower() not in checkedPaths: # dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] (2.1) Found possible DB2 install path <%s>' % userHomeDirectory) # possibleInstallLocations.append(userHomeDirectory) # checkedPaths.append(userHomeDirectory.lower()) # else: # dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':processProcToPortDict] (2.1) Skipping path <%s> since it has been processed before' % userHomeDirectory) # else: # dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':processProcToPortDict] (2.1) Invalid username for process/service/software <%s>' % processName) if dbconnect_utils.isValidString(path): logger.debug('path is: ', path) # added for debugging - Daniel La ## Remove process name from the path db2Path = path[:path.rfind(processName)-1] ## For windows, the processName variable may contain service ## names, so extract process names if isWindows == 'true': procNameMatch = re.search(r'.*[\\|/](\w+\.exe)', path) if procNameMatch: procName = procNameMatch.group(1).strip() db2Path = path[:path.rfind(procName)-1] if db2Path.lower() not in checkedPaths: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] (2.2) Found possible DB2 install path <%s>' % db2Path) possibleInstallLocations.append(db2Path) checkedPaths.append(db2Path.lower()) else: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':processProcToPortDict] (2.2) Skipping path <%s> since it has been processed before' % path) else: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':processProcToPortDict] (2.2) Invalid path for process/service/software <%s>' % processName) except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':processProcToPortDict] Exception: <%s>' % excInfo) pass
def getDatabases(localClient, isWindows, instancePaths, dbDict): try: for instancePath in instancePaths: if isWindows == 'true': listInstancesCommand = '\"' + instancePath + '\\db2envar.bat\" && ' + '\"' + instancePath + '\\db2ilist\"' listInstancesOutput = localClient.executeCmd(listInstancesCommand) listInstances = dbconnect_utils.splitCommandOutput(listInstancesOutput) logger.debug('length is: ', len(listInstances)) for instance in listInstances: listenerPort = getListenerPort(localClient, isWindows, instancePath, instance) listDbDirectoryCommand = '(\"' + instancePath + '\\db2envar.bat\") && ' + '(set DB2INSTANCE=' + instance + ') && ' + '(\"' + instancePath + '\\db2cmd\" /c /w /i db2 list db directory)' listDbDirectoryOutput = localClient.executeCmd(listDbDirectoryCommand) if not listDbDirectoryOutput or not re.search('entry:', listDbDirectoryOutput): dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getDatabases] Invalid output from command db2 list db directory for instance at <%s>! Skipping...' % instancePath) continue ## Initialize variables dbNameAliasDict = {} ## Need to initialize the database alias here because the sequecne has alias ## followed by a name and there may be more than one of each databaseAlias = None ## Split the command output into individial lines listDbDirectoryOutputLines= dbconnect_utils.splitCommandOutput(listDbDirectoryOutput) ## Get DB details one line at a time for listDbDirectoryOutputLine in listDbDirectoryOutputLines: logger.debug('*** outputline is: ', listDbDirectoryOutputLine) ## Database alias m = re.search('Database alias\s+=\s*(\S+)', listDbDirectoryOutputLine.strip()) if (m): databaseAlias = m.group(1) dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getDatabases] Found Database Alias: <%s>' % databaseAlias) ## Database name m = re.search('Database name\s+=\s*(\S+)', listDbDirectoryOutputLine.strip()) if (m): databaseName = m.group(1) dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getDatabases] Found Database Name: <%s>' % databaseName) ## Directory entry type - tells whether database is local (indirect) or remote (remote) m = re.search('Directory entry type\s+=\s*Indirect', listDbDirectoryOutputLine.strip()) if (m): logger.debug('database is local: ', databaseName) if databaseName and databaseName not in dbNameAliasDict.keys(): if databaseAlias: dbNameAliasDict[databaseName] = databaseAlias else: dbNameAliasDict[databaseName] = databaseName # original.. without instance name. # for databaseName in dbNameAliasDict.keys(): # dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getDatabases] Adding DB2 instance <%s> listening at port <%s>, on <%s>, and installed in <%s>' % (databaseName, listenerPort, localClient.getIpAddress(), instancePath)) # dbDict[databaseName] = ['db2', listenerPort, localClient.getIpAddress(), instancePath, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN] # if databaseName != dbNameAliasDict[databaseName]: # dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getDatabases] Adding DB2 instance alias <%s> listening at port <%s>, on <%s>, and installed in <%s>' % (dbNameAliasDict[databaseName], listenerPort, localClient.getIpAddress(), instancePath)) # dbDict[dbNameAliasDict[databaseName]] = ['db2', listenerPort, localClient.getIpAddress(), instancePath, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN] for databaseName in dbNameAliasDict.keys(): dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getDatabases] Adding DB2 database <%s> in DB2 instance <%s>, listening at port <%s>, on <%s>, and installed in <%s>' % (databaseName, instance.upper(), listenerPort, localClient.getIpAddress(), instancePath)) dbDict[databaseName] = ['db2', listenerPort, localClient.getIpAddress(), instancePath, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, instance.upper()] if databaseName != dbNameAliasDict[databaseName]: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getDatabases] Adding DB2 database alias <%s> in DB2 instance <%s>, listening at port <%s>, on <%s>, and installed in <%s>' % (dbNameAliasDict[databaseName], instance.upper(), listenerPort, localClient.getIpAddress(), instancePath)) dbDict[dbNameAliasDict[databaseName]] = ['db2', listenerPort, localClient.getIpAddress(), instancePath, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, instance.upper()] else: # Unix ## Get instance port listenerPort = getListenerPort(localClient, isWindows, instancePath) listDbDirectoryOutput = None listDbDirectoryCommand = 'unset LIBPATH; cd ' + instancePath + '/../; . ./db2profile; ' + 'export DB2NODE=127.0.0.1; ' + instancePath + '/db2 list db directory; ' + instancePath + '/db2 terminate' # modified by Daniel La 22/11/10 logger.debug('before') listDbDirectoryOutput = getDbDirectory(localClient, listDbDirectoryCommand) logger.debug('after') if not listDbDirectoryOutput or not re.search('entry:', listDbDirectoryOutput): dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getDatabases] Invalid output from command db2 list db directory for instance at <%s>! Skipping...' % instancePath) continue logger.debug('after after') instanceCommand = 'echo $DB2INSTANCE' instance = (localClient.executeCmd(instanceCommand)).strip().upper() logger.debug('instance is: ', instance) ## Initialize variables dbNameAliasDict = {} ## Need to initialize the database alias here because the sequecne has alias ## followed by a name and there may be more than one of each databaseAlias = None ## Split the command output into individial lines listDbDirectoryOutputLines= dbconnect_utils.splitCommandOutput(listDbDirectoryOutput) ## Get DB details one line at a time for listDbDirectoryOutputLine in listDbDirectoryOutputLines: # logger.debug('*** outputline is: ', listDbDirectoryOutputLine) ## Database alias m = re.search('Database alias\s+=\s*(\S+)', listDbDirectoryOutputLine.strip()) if (m): databaseAlias = m.group(1) dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getDatabases] Found Database Alias: <%s>' % databaseAlias) ## Database name m = re.search('Database name\s+=\s*(\S+)', listDbDirectoryOutputLine.strip()) if (m): databaseName = m.group(1) dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getDatabases] Found Database Name: <%s>' % databaseName) ## Directory entry type - tells whether database is local (indirect) or remote (remote) m = re.search('Directory entry type\s+=\s*Indirect', listDbDirectoryOutputLine.strip()) if (m): logger.debug('database is local: ', databaseName) if databaseName and databaseName not in dbNameAliasDict.keys(): if databaseAlias: dbNameAliasDict[databaseName] = databaseAlias else: dbNameAliasDict[databaseName] = databaseName # for databaseName in dbNameAliasDict.keys(): # dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getDatabases] Adding DB2 instance <%s> listening at port <%s>, on <%s>, and installed in <%s>' % (databaseName, listenerPort, localClient.getIpAddress(), instancePath)) # dbDict[databaseName] = ['db2', listenerPort, localClient.getIpAddress(), instancePath, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN] # if databaseName != dbNameAliasDict[databaseName]: # dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getDatabases] Adding DB2 instance alias <%s> listening at port <%s>, on <%s>, and installed in <%s>' % (dbNameAliasDict[databaseName], listenerPort, localClient.getIpAddress(), instancePath)) # dbDict[dbNameAliasDict[databaseName]] = ['db2', listenerPort, localClient.getIpAddress(), instancePath, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN] for databaseName in dbNameAliasDict.keys(): dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getDatabases] Adding DB2 database <%s> in DB2 instance <%s>, listening at port <%s>, on <%s>, and installed in <%s>' % (databaseName, instance, listenerPort, localClient.getIpAddress(), instancePath)) dbDict[databaseName] = ['db2', listenerPort, localClient.getIpAddress(), instancePath, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, instance] if databaseName != dbNameAliasDict[databaseName]: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getDatabases] Adding DB2 database alias <%s> in DB2 instance <%s>, listening at port <%s>, on <%s>, and installed in <%s>' % (dbNameAliasDict[databaseName], instance, listenerPort, localClient.getIpAddress(), instancePath)) dbDict[dbNameAliasDict[databaseName]] = ['db2', listenerPort, localClient.getIpAddress(), instancePath, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, instance] except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':getDatabaseInstances] Exception: <%s>' % excInfo) pass
def DiscoveryMain(Framework): OSHVResult = ObjectStateHolderVector() discoverOracle = _getBoolJobParameter(Parameters.DISCOVER_ORACLE, Framework) discoverMssql = _getBoolJobParameter(Parameters.DISCOVER_MSSQL, Framework) useSudo = _getBoolJobParameter(Parameters.USE_SUDO, Framework) useLsof = _getBoolJobParameter(Parameters.USE_LSOF, Framework) protocol = Framework.getDestinationAttribute('Protocol') protocolDisplay = errormessages.protocolNames.get(protocol) or protocol if not discoverOracle and not discoverMssql: parameterNames = ", ".join( (Parameters.DISCOVER_MSSQL, Parameters.DISCOVER_ORACLE)) msg = "None of parameters [%s] are enabled. Set at least one to true to discover corresponding Database." % parameterNames errorObject = errorobject.createError( errorcodes.INTERNAL_ERROR_WITH_PROTOCOL_DETAILS, [protocolDisplay, msg], msg) logger.reportWarningObject(errorObject) logger.warn(msg) logger.warn( "Note: support for DB2 in this job is deprecated since CP13.") return OSHVResult client = None shell = None osName = None processToPortDict = { } ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} databaseDict = { } ## {instanceName/SID:[dbType, listeningPort, ipAddress, installPath, version, status]} database_ip_service_endpoints = {} ## {instanceName:[ip_address:port]} try: client = Framework.createClient() shell = shellutils.ShellUtils(client) isWindows = 'false' dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':DiscoveryMain] Got client!') if shell.isWinOs(): osName = 'Windows' isWindows = 'true' dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':DiscoveryMain] Client is Windows!') else: if shell.getClientType() == 'ssh': osName = netutils.getOSName(client, 'uname -a') else: osName = netutils.getOSName(client, 'uname') dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':DiscoveryMain] Client OS is <%s>' % osName) ## We have a shell client ## Get processes running on the box and ports they're listening on if (client and osName): osNameLower = osName.lower() if re.search("windows", osNameLower): processToPortDict = dbconnect_win_shellutils.getProcToPortDictOnWindows( client, Framework) elif re.search("aix", osNameLower): processToPortDict = dbconnect_unix_shellutils.getProcToPortDictOnAIX( client, useSudo, useLsof) elif re.search("linux", osNameLower): processToPortDict = dbconnect_unix_shellutils.getProcToPortDictOnLinux( client, useSudo, useLsof) elif re.search("sun", osNameLower): processToPortDict = dbconnect_unix_shellutils.getProcToPortDictOnSolaris( client, useSudo, useLsof) elif re.search("hp-ux", osNameLower): processToPortDict = dbconnect_unix_shellutils.getProcToPortDictOnHPUX( client, useSudo, useLsof) else: dbconnect_utils.debugPrint('Unknown operating system') ## We have process and port infromation ## Find databases, if any if processToPortDict != None and len(processToPortDict) > 0: for pid in processToPortDict.keys(): dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':DiscoveryMain] Got process/service/software <%s> listening on port <%s:%s>' % ((processToPortDict[pid] )[dbconnect_utils.PROCESSNAME_INDEX], (processToPortDict[pid])[dbconnect_utils.IP_INDEX], (processToPortDict[pid])[dbconnect_utils.PORT_INDEX])) if discoverOracle: dbconnect_oracle.findDatabases(client, processToPortDict, databaseDict, isWindows) if discoverMssql and re.search("windows", osNameLower): dbconnect_mssql.findDatabases( client, processToPortDict, databaseDict, database_ip_service_endpoints) if databaseDict != None and len(databaseDict) > 0: for dbName in databaseDict.keys(): logger.debug( 'Found <%s> instance <%s> (%s) with listener port <%s:%s> and installed in <%s>' % ((databaseDict[dbName] )[dbconnect_utils.DBTYPE_INDEX], dbName, (databaseDict[dbName] )[dbconnect_utils.STATUS_INDEX], (databaseDict[dbName])[dbconnect_utils.IP_INDEX], (databaseDict[dbName] )[dbconnect_utils.PORT_INDEX], (databaseDict[dbName] )[dbconnect_utils.PATH_INDEX])) if database_ip_service_endpoints and len( database_ip_service_endpoints) > 0: OSHVResult.addAll( dbconnect_utils.makeDbOSHs( databaseDict, database_ip_service_endpoints)) else: OSHVResult.addAll( dbconnect_utils.makeDbOSHs(databaseDict)) else: Framework.reportWarning('No databases found') else: ## If we're here, we couldn't find any processes, service, or software ## and we have no data to search for databases Framework.reportWarning( 'Unable to get a list or processes, services, or installed software using <%s>' % shell.getClientType()) else: logger.debug('Unable to connect using NTCMD, SSH , or Telnet') except JException, ex: exInfo = ex.getMessage() errormessages.resolveAndReport(exInfo, protocolDisplay, Framework)
def processProcToPortDict(localClient, p2pDict, dbDict, database_ip_service_endpoints): try: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':processProcToPortDict]') for pid in p2pDict.keys(): processName = ( p2pDict[pid])[dbconnect_utils.PROCESSNAME_INDEX].lower() listenerPort = (p2pDict[pid])[dbconnect_utils.PORT_INDEX] ipAddress = (p2pDict[pid])[dbconnect_utils.IP_INDEX] if ipAddress == dbconnect_utils.UNKNOWN: ipAddress = localClient.getIpAddress() path = (p2pDict[pid])[dbconnect_utils.PATH_INDEX].lower() commandLine = (p2pDict[pid])[dbconnect_utils.COMMANDLINE_INDEX] statusFlag = (p2pDict[pid])[dbconnect_utils.STATUS_INDEX] version = dbconnect_utils.UNKNOWN instanceNameFound = '' installPath = dbconnect_utils.UNKNOWN if re.search('mssqlserveradhelper', processName) or re.search( 'sql server analysis services', processName) or re.search( 'sql server agent', processName): ## Filters: If we don't skip these, the next checks will ## catch them and identify incorrect instances dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':processProcToPortDict] (1) Found process name <%s>. Ignoring...' % processName) continue ## Look for SQL Server instances using known process/service/software names elif re.search('sqlservr.exe', processName): #instanceNameFound = 'MicrosoftSQLServer' ## This is not a named instance # Get instance name from command line instanceNameFound = dbconnect_utils.getServerName(localClient) instanceNameRegexStr = re.search(r'\-s(\w+)$', commandLine) if instanceNameRegexStr: instanceNameFound = instanceNameFound + instanceNameRegexStr.group( 1).strip().lower() if path != dbconnect_utils.UNKNOWN: binPath = path[:path.find('sqlservr.exe')] installPath = binPath[:len(binPath) - 5] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':processProcToPortDict] (2) Found MSSQL Server instance <%s> at port <%s> from process name <%s> and its path is <%s>' % (instanceNameFound, listenerPort, processName, path)) dbDict[instanceNameFound] = [ 'MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, statusFlag ] if ipAddress and netutils.isValidIp( ipAddress) and listenerPort: ip_service_endpoints = database_ip_service_endpoints.get( instanceNameFound) or [] ip_service_endpoints.append("%s:%s" % (ipAddress, listenerPort)) database_ip_service_endpoints[ instanceNameFound] = ip_service_endpoints elif re.search('mssql\$', processName): instanceNameRegexStr = re.search('mssql\$(\w+)', processName) ## Skip if an instance name cannot be identified if instanceNameRegexStr: instanceNameFound = instanceNameRegexStr.group( 1).strip().lower() instanceNameFound = dbconnect_utils.getServerName( localClient) + '\\' + instanceNameFound else: continue if path != dbconnect_utils.UNKNOWN: binPath = path[:path.find('sqlservr.exe')] installPath = binPath[:len(binPath) - 5] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':processProcToPortDict] (3) Found MSSQL Server instance <%s> at port <%s> from process name <%s> and its path is <%s>' % (instanceNameFound, listenerPort, processName, path)) dbDict[instanceNameFound] = [ 'MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, statusFlag ] if ipAddress and netutils.isValidIp( ipAddress) and listenerPort: ip_service_endpoints = database_ip_service_endpoints.get( instanceNameFound) or [] ip_service_endpoints.append("%s:%s" % (ipAddress, listenerPort)) database_ip_service_endpoints[ instanceNameFound] = ip_service_endpoints elif re.search('sql server \(', processName): instanceNameRegexStr = re.search('sql server \((\w+)\)', processName) ## Skip if an instance name cannot be identified if instanceNameRegexStr: instanceNameFound = instanceNameRegexStr.group( 1).strip().lower() else: continue ## Fix SQL 2K5 instancename to something UCMDB can understand if instanceNameFound == 'mssqlserver': #instanceNameFound = 'MicrosoftSQLServer' instanceNameFound = dbconnect_utils.getServerName( localClient) else: instanceNameFound = dbconnect_utils.getServerName( localClient) + '\\' + instanceNameFound ## Get path if path != dbconnect_utils.UNKNOWN: binPath = path[:path.find('sqlservr.exe')] installPath = binPath[:len(binPath) - 5] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':processProcToPortDict] (4) Found MSSQL Server instance <%s> at port <%s> from process name <%s> and its path is <%s>' % (instanceNameFound, listenerPort, processName, path)) dbDict[instanceNameFound] = [ 'MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, statusFlag ] if ipAddress and netutils.isValidIp( ipAddress) and listenerPort: ip_service_endpoints = database_ip_service_endpoints.get( instanceNameFound) or [] ip_service_endpoints.append("%s:%s" % (ipAddress, listenerPort)) database_ip_service_endpoints[ instanceNameFound] = ip_service_endpoints elif re.search('mssqlserver\^w', processName): #instanceNameFound = 'MicrosoftSQLServer' ## This is not a named instance instanceNameFound = dbconnect_utils.getServerName(localClient) if path != dbconnect_utils.UNKNOWN: binPath = path[:path.find('sqlservr.exe')] installPath = binPath[:len(binPath) - 5] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':processProcToPortDict] (5) Found MSSQL Server instance <%s> at port <%s> from process name <%s> and its path is <%s>' % (instanceNameFound, listenerPort, processName, path)) dbDict[instanceNameFound] = [ 'MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, statusFlag ] if ipAddress and netutils.isValidIp( ipAddress) and listenerPort: ip_service_endpoints = database_ip_service_endpoints.get( instanceNameFound) or [] ip_service_endpoints.append("%s:%s" % (ipAddress, listenerPort)) database_ip_service_endpoints[ instanceNameFound] = ip_service_endpoints elif re.search('mssqlserver', processName): #instanceNameFound = 'MicrosoftSQLServer' ## This is not a named instance instanceNameFound = dbconnect_utils.getServerName(localClient) if path != dbconnect_utils.UNKNOWN: binPath = path[:path.find('sqlservr.exe')] installPath = binPath[:len(binPath) - 5] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':processProcToPortDict] (6) Found MSSQL Server instance <%s> at port <%s> from process name <%s> and its path is <%s>' % (instanceNameFound, listenerPort, processName, path)) dbDict[instanceNameFound] = [ 'MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, statusFlag ] if ipAddress and netutils.isValidIp( ipAddress) and listenerPort: ip_service_endpoints = database_ip_service_endpoints.get( instanceNameFound) or [] ip_service_endpoints.append("%s:%s" % (ipAddress, listenerPort)) database_ip_service_endpoints[ instanceNameFound] = ip_service_endpoints except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':processProcToPortDict] Exception: <%s>' % excInfo) pass
def DiscoveryMain(Framework): OSHVResult = ObjectStateHolderVector() discoverOracle = _getBoolJobParameter(Parameters.DISCOVER_ORACLE, Framework) discoverMssql = _getBoolJobParameter(Parameters.DISCOVER_MSSQL, Framework) useSudo = _getBoolJobParameter(Parameters.USE_SUDO, Framework) useLsof = _getBoolJobParameter(Parameters.USE_LSOF, Framework) protocol = Framework.getDestinationAttribute('Protocol') protocolDisplay = errormessages.protocolNames.get(protocol) or protocol if not discoverOracle and not discoverMssql: parameterNames = ", ".join((Parameters.DISCOVER_MSSQL, Parameters.DISCOVER_ORACLE)) msg = "None of parameters [%s] are enabled. Set at least one to true to discover corresponding Database." % parameterNames errorObject = errorobject.createError(errorcodes.INTERNAL_ERROR_WITH_PROTOCOL_DETAILS, [protocolDisplay, msg], msg) logger.reportWarningObject(errorObject) logger.warn(msg) logger.warn("Note: support for DB2 in this job is deprecated since CP13.") return OSHVResult client = None shell = None osName = None processToPortDict = {} ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} databaseDict = {} ## {instanceName/SID:[dbType, listeningPort, ipAddress, installPath, version, status]} database_ip_service_endpoints ={} ## {instanceName:[ip_address:port]} try: client = Framework.createClient() shell = shellutils.ShellUtils(client) isWindows = 'false' dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':DiscoveryMain] Got client!') if shell.isWinOs(): osName = 'Windows' isWindows = 'true' dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':DiscoveryMain] Client is Windows!') else: if shell.getClientType() == 'ssh': osName = netutils.getOSName(client, 'uname -a') else: osName = netutils.getOSName(client, 'uname') dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':DiscoveryMain] Client OS is <%s>' % osName) ## We have a shell client ## Get processes running on the box and ports they're listening on if(client and osName): osNameLower = osName.lower() if re.search("windows", osNameLower): processToPortDict = dbconnect_win_shellutils.getProcToPortDictOnWindows(client, Framework) elif re.search("aix", osNameLower): processToPortDict = dbconnect_unix_shellutils.getProcToPortDictOnAIX(client, useSudo, useLsof) elif re.search("linux", osNameLower): processToPortDict = dbconnect_unix_shellutils.getProcToPortDictOnLinux(client, useSudo, useLsof) elif re.search("sun", osNameLower): processToPortDict = dbconnect_unix_shellutils.getProcToPortDictOnSolaris(client, useSudo, useLsof) elif re.search("hp-ux", osNameLower): processToPortDict = dbconnect_unix_shellutils.getProcToPortDictOnHPUX(client, useSudo, useLsof) else: dbconnect_utils.debugPrint('Unknown operating system') ## We have process and port infromation ## Find databases, if any if processToPortDict != None and len(processToPortDict) > 0: for pid in processToPortDict.keys(): dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':DiscoveryMain] Got process/service/software <%s> listening on port <%s:%s>' % ((processToPortDict[pid])[dbconnect_utils.PROCESSNAME_INDEX], (processToPortDict[pid])[dbconnect_utils.IP_INDEX], (processToPortDict[pid])[dbconnect_utils.PORT_INDEX])) if discoverOracle: dbconnect_oracle.findDatabases(client, processToPortDict, databaseDict, isWindows) if discoverMssql and re.search("windows", osNameLower): dbconnect_mssql.findDatabases(client, processToPortDict, databaseDict, database_ip_service_endpoints) if databaseDict != None and len(databaseDict) > 0: for dbName in databaseDict.keys(): logger.debug('Found <%s> instance <%s> (%s) with listener port <%s:%s> and installed in <%s>' % ((databaseDict[dbName])[dbconnect_utils.DBTYPE_INDEX], dbName, (databaseDict[dbName])[dbconnect_utils.STATUS_INDEX], (databaseDict[dbName])[dbconnect_utils.IP_INDEX], (databaseDict[dbName])[dbconnect_utils.PORT_INDEX], (databaseDict[dbName])[dbconnect_utils.PATH_INDEX])) if database_ip_service_endpoints and len(database_ip_service_endpoints) > 0: OSHVResult.addAll(dbconnect_utils.makeDbOSHs(databaseDict, database_ip_service_endpoints)) else: OSHVResult.addAll(dbconnect_utils.makeDbOSHs(databaseDict)) else: Framework.reportWarning('No databases found') else: ## If we're here, we couldn't find any processes, service, or software ## and we have no data to search for databases Framework.reportWarning('Unable to get a list or processes, services, or installed software using <%s>' % shell.getClientType()) else: logger.debug('Unable to connect using NTCMD, SSH , or Telnet') except JException, ex: exInfo = ex.getMessage() errormessages.resolveAndReport(exInfo, protocolDisplay, Framework)
def getProcToPortDictOnWindows(localClient, localFramework): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows]') procToPortDict = {} shell = shellutils.ShellUtils(localClient) ntcmdErrStr = 'Remote command returned 1(0x1)' HOST_IP = localClient.getIpAddress() HOSTID = localFramework.getDestinationAttribute('hostId') ## Get process OSHs using NTCMD HR script ############################################ try: processOSHV = ObjectStateHolderVector() if (NTCMD_HR_Dis_Process_Lib.discoverProcessesByWmic(shell, processOSHV, HOSTID, localFramework)) == 1 or (NTCMD_HR_Dis_Process_Lib.discoverProcesses(shell, processOSHV, HOSTID, localFramework)) == 1: ## We have an OSHV, extract OSHs from it oshvIndex = 0 try: while processOSHV.get(oshvIndex): someOSH = processOSHV.get(oshvIndex) if someOSH.getObjectClass() == 'process': processDict = dbconnect_utils.getAttributeValuesFromOSH(someOSH, ['process_pid', 'data_name', 'process_cmdline', 'process_path']) ## Name processName = processDict['data_name'] if processName == None or processName == '' or len(processName) <1: ## We don't care about nameless processes continue pid = processDict['process_pid'] ## PID processPath = processDict['process_path'] ## Path processPath = string.replace(processPath, '"', '') processCmdline = processDict['process_cmdline'] ## Command line processCmdline = string.replace(processCmdline, '"', '') ## Add this to the dictionary dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got PROCESS <%s:%s> with path <%s> and command line <%s>' % (pid, processName, processPath, processCmdline)) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [processName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, processPath, dbconnect_utils.UNKNOWN, 'Running', processCmdline] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, processName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, processPath, dbconnect_utils.UNKNOWN, 'Running', processCmdline) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, processName, 'Running', processPath, processCmdline)) oshvIndex = oshvIndex + 1 except ArrayIndexOutOfBoundsException: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Array OOB exception while getting process CIs from OSHV. Ignoring because this is expected...') pass else: ## We don't have an OSHV dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to get list of processes') except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to get list of processes: <%s>' % excInfo) pass ## Add windows services to the list of processes ############################################ ## First try WMIC because we can get a PID ## If that doesn't work, fallback on the OOTB NTCMD HR script try: buffer = shell.execCmd('wmic service get displayname, pathname, processid, started /format:csv < %SystemRoot%\win.ini') dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Output for wmic process command: %s' % buffer) reg_mamRc = shell.getLastCmdReturnCode() if (reg_mamRc == 0): ## WMIC worked!! wmicLines = buffer.split('\n') fakePid = 0 # Each line: HOSTNAME,SERVICENAME,EXE-PATH,PID for wmicLine in wmicLines: tokens = wmicLine.split(',') numTokens = len(tokens) if (tokens == None) or (numTokens < 1) : continue if tokens[0].strip() == 'Node': continue if (numTokens < 4): continue serviceName = tokens[numTokens - 4].strip() serviceStatus = dbconnect_utils.UNKNOWN if tokens[numTokens - 1].strip().lower() == 'true': serviceStatus = 'Running' else: serviceStatus = 'Not Running' pid = tokens[numTokens - 2].strip() ## Don't bother with SYSTEM services that have a pid of -1 if(pid != '-1' and pid.isnumeric()): # Get the command line serviceCmdline = tokens[numTokens - 3].strip() serviceCmdline = serviceCmdline.strip()[0:2499] serviceCmdline = string.replace(serviceCmdline, '"', '') # Set process path to command line servicePath = serviceCmdline ## While using services, we sometimes need a fake PID because ## the service may not be running and the corresponding PID will be 0 if pid == '0': pid = 'SERVICE ' + str(fakePid) fakePid = fakePid + 1 dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Using fake PID <%s> for service <%s>' % (pid, serviceName)) ## Got everything, make the array dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got SERVICE <%s (%s)> with PID <%s>, command line <%s>, command path <%s>' % (serviceName, serviceStatus, pid, serviceCmdline, servicePath)) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [serviceName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, serviceName, dbconnect_utils.UNKNOWN, HOST_IP, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add SERVICE <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, serviceName, serviceStatus, servicePath, serviceCmdline)) else: ## WMIC didn't work. Get service OSHs using NTCMD HR script dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] WMIC didn\'t work, trying NTCMD HR script') servicesOSHV = NTCMD_HR_REG_Service_Lib.doService(shell, modeling.createHostOSH(HOST_IP)) ## Extract OSHs from vector oshvIndex = 0 try: while servicesOSHV.get(oshvIndex): someOSH = servicesOSHV.get(oshvIndex) if someOSH.getObjectClass() == 'service': serviceDict = dbconnect_utils.getAttributeValuesFromOSH(someOSH, ['data_name', 'service_pathtoexec', 'service_commandline', 'service_operatingstatus']) ## Name serviceName = serviceDict['data_name'] if serviceName == None or serviceName == '' or len(serviceName) <1: ## We don't care about nameless services continue pid = 'SERVICE ' + str(oshvIndex) ## PID (fake) servicePath = serviceDict['service_pathtoexec'] ## Install path servicePath = string.replace(servicePath, '"', '') serviceCmdline = serviceDict['service_commandline'] ## Command line serviceCmdline = string.replace(serviceCmdline, '"', '') serviceStatus = serviceDict['service_operatingstatus'] ## Status if serviceStatus == 'true': serviceStatus = 'Running' else: serviceStatus = 'Not Running' ## Add this to the dictionary dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got <%s:%s> with installPath <%s> and commandline <%s>' % (pid, serviceName, servicePath, serviceCmdline)) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [serviceName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, serviceName, dbconnect_utils.UNKNOWN, HOST_IP, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add SERVICE <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, serviceName, serviceStatus, servicePath, serviceCmdline)) oshvIndex = oshvIndex + 1 except ArrayIndexOutOfBoundsException: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Array OOB exception while getting service CIs from OSHV. Ignoring because this is expected...') pass except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to get list of services: <%s>' % excInfo) pass ## Add installed software to the list of processes ############################################ try: ## Get installed software OSHs using NTCMD HR script softwareOSHV = ObjectStateHolderVector() gotSoftwareOshs = NTCMD_HR_REG_Software_Lib.doSoftware(shell, modeling.createHostOSH(HOST_IP), softwareOSHV) ## Extract OSHs from vector oshvIndex = 0 while gotSoftwareOshs and softwareOSHV.get(oshvIndex): someOSH = softwareOSHV.get(oshvIndex) if someOSH.getObjectClass() == 'software': softwareDict = dbconnect_utils.getAttributeValuesFromOSH(someOSH, ['data_name', 'software_installpath', 'software_version']) ## Name softwareName = softwareDict['data_name'] if not softwareName: ## We don't care about nameless software continue pid = 'SOFTWARE ' + str(oshvIndex) ## PID (fake) softwareInstallPath = softwareDict['software_installpath'] ## Install path softwareVersion = softwareDict['software_version'] ## Version ## Add this to the dictionary dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got <%s:%s> with installPath <%s> and version <%s>' % (pid, softwareName, softwareInstallPath, softwareVersion)) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [softwareName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, softwareInstallPath, softwareVersion, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, softwareName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, softwareInstallPath, softwareVersion, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add SOFTWARE <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, softwareName, dbconnect_utils.UNKNOWN, softwareInstallPath, dbconnect_utils.UNKNOWN)) oshvIndex = oshvIndex + 1 except ArrayIndexOutOfBoundsException: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Array OOB exception while getting software CIs from OSHV. Ignoring because this is expected...') pass except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to get list of software: <%s>' % excInfo) pass ## Use NETSTAT output to create an array of server ports ## and map them to server processes ############################################ ## Updated by Daniel La: updated to handle situation where a process can listen on multiple ports. try: keydeletion = [] # keys to delete - Daniel La netstatLisStr = shell.execCmd('netstat -aon -p tcp | find "LISTENING"') nsStrOk = 'false' nsLisLines = None if netstatLisStr.find(ntcmdErrStr) != -1 or len(netstatLisStr) < 1: nsStrOk = 'false' elif re.search('\r\n', netstatLisStr): nsLisLines = netstatLisStr.split('\r\n') nsStrOk = 'true' elif re.search('\n', netstatLisStr): nsLisLines = netstatLisStr.split('\n') nsStrOk = 'true' if nsStrOk == 'true': for line in nsLisLines: line = line.strip() m = re.search('\S+\s+(\d+.\d+.\d+.\d+):(\d+)\s+\d+.\d+.\d+.\d+:\d+\s+\S+\s+(\d+).*', line) if (m): ipAddress = m.group(1).strip() ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP(ipAddress, localClient.getIpAddress()) serverPort = m.group(2).strip() pid = m.group(3) dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got port <%s> for pid <%s>' % (serverPort, pid)) # modified if statement slightly to include check to see if listener is listening on port 80, 2481, or 2482. If so, these can be ignored. - Daniel La if pid != '-' and procToPortDict.has_key(pid) and not (serverPort in ['80', '2481', '2482'] and re.search('tns', (procToPortDict[pid])[0].lower())): dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Adding port <%s:%s> for process <%s>' % (ipAddress, serverPort, (procToPortDict[pid])[0])) (procToPortDict[pid])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid])[dbconnect_utils.PORT_INDEX] = serverPort # create new key and add new entry - Daniel La pidnport = str(pid) + '.' + str(serverPort) if dbconnect_utils.populateProcToPortDict(procToPortDict, pidnport, (procToPortDict[pid])[dbconnect_utils.PROCESSNAME_INDEX], (procToPortDict[pid])[dbconnect_utils.PORT_INDEX], (procToPortDict[pid])[dbconnect_utils.IP_INDEX], (procToPortDict[pid])[dbconnect_utils.PATH_INDEX], (procToPortDict[pid])[dbconnect_utils.VERSION_INDEX], (procToPortDict[pid])[dbconnect_utils.STATUS_INDEX], (procToPortDict[pid])[dbconnect_utils.COMMANDLINE_INDEX], (procToPortDict[pid])[dbconnect_utils.USER_INDEX]) == 0: logger.debug('Unable to add ', (procToPortDict[pid])[dbconnect_utils.PROCESSNAME_INDEX]) else: # add key to list of keys to delete later as we've updated them with new entries. keydeletion.append(pid) dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Got port <%s> for pid <%s>' % (serverPort, pid)) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Couldn\'t get process information (Most likely due to lack of user permissions): ' + line) # delete keys which are of no value anymore - Daniel La for key in keydeletion: if procToPortDict.has_key(key): del procToPortDict[key] logger.debug('deleted key: ', key) else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Invalid output from netstat: <%s>' % netstatLisStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to make a process to port map using netstat: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Returning EMPTY process to port dictionary') return None except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Exception: <%s>' % excInfo) pass
def getProcListByWMI(localClient, wmiRegistryClient): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcListByWMI]') procToPortDict = {} HOST_IP = localClient.getIpAddress() ## Not using WMI HR scripts since they are not split into a main discovery and ## utils script. Using them will result in those discoveries being executed for ## this destination and host resource data will be added to the CMDB ## Get a list of processes try: resultSet = localClient.executeQuery('SELECT name, processid, executablepath, commandline FROM Win32_Process') while resultSet.next(): ## Name processName = resultSet.getString(1).strip() if processName == None or processName == '' or len(processName) <1: ## We don't care about nameless processes continue pid = resultSet.getString(2).strip() ## PID processPath = resultSet.getString(3).strip() ## Path processPath = string.replace(processPath, '"', '') processCmdline = resultSet.getString(4).strip() ## Command line processCmdline = string.replace(processCmdline, '"', '') ## Add this to the dictionary dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcListByWMI] Got PROCESS <%s:%s> with path <%s> and command line <%s>' % (pid, processName, processPath, processCmdline)) ## {PID:[processName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [processName, dbconnect_utils.UNKNOWN, HOST_IP, processPath, dbconnect_utils.UNKNOWN, 'Running', processCmdline] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, processName, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, processPath, dbconnect_utils.UNKNOWN, 'Running', processCmdline) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, processName, 'Running', processPath, processCmdline)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcListByWMI] Unable to get a list of proceses: <%s>' % excInfo) pass ## Get a list of services try: resultSet = localClient.executeQuery('SELECT displayname, processid, pathname, started FROM Win32_Service') fakePid = 0 while resultSet.next(): ## Name serviceName = resultSet.getString(1).strip() if serviceName == None or serviceName == '' or len(serviceName) <1: ## We don't care about nameless services continue pid = resultSet.getString(2).strip() ## PID if pid == '0': pid = 'SERVICE ' + str(fakePid) fakePid = fakePid + 1 dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcListByWMI] Using fake PID <%s> for service <%s>' % (pid, serviceName)) servicePath = resultSet.getString(3).strip() ## Path servicePath = string.replace(servicePath, '"', '') serviceStatus = resultSet.getString(4).strip() ## Status if serviceStatus.lower() == 'true': serviceStatus = 'Running' else: serviceStatus = 'Not Running' serviceCmdline = servicePath ## Add this to the dictionary dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcListByWMI] Got SERVICE <%s:%s> (%s) with path <%s> and command line <%s>' % (pid, serviceName, serviceStatus, servicePath, serviceCmdline)) ## {PID:[serviceName, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [serviceName, dbconnect_utils.UNKNOWN, HOST_IP, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, serviceName, dbconnect_utils.UNKNOWN, HOST_IP, servicePath, dbconnect_utils.UNKNOWN, serviceStatus, serviceCmdline) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcListByWMI] Unable to add SERVICE <%s:%s> (%s) with path <%s> and command line <%s> to the procToPort dictionary' % (pid, serviceName, serviceStatus, servicePath, serviceCmdline)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcListByWMI] Unable to get a list of services: <%s>' % excInfo) pass ## Get a list of installed software try: fakePid = 0 regKeypath = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall' swNames = dbconnect_utils.getRegValues(localClient, wmiRegistryClient, regKeypath, 'DisplayName') swPaths = dbconnect_utils.getRegValues(localClient, wmiRegistryClient, regKeypath, 'UninstallString') swVersions = dbconnect_utils.getRegValues(localClient, wmiRegistryClient, regKeypath, 'DisplayVersion') for swName in swNames.keys(): swName = swNames.get(swName) swPath = swPaths.get(swName) swVersion = swVersions.get(swName) pid = 'SOFTWARE ' + str(fakePid) fakePid = fakePid + 1 ## Add this to the dictionary dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcListByWMI] Got SOFTWARE <%s:%s> with path <%s> and version <%s>' % (pid, swName, swPath, swVersion)) ## {PID:[serviceName, listeningPort, ipAddress, path, version, status, processCommandline]} if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, swName, dbconnect_utils.UNKNOWN, HOST_IP, swPath, swVersion, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcListByWMI] Unable to add SOFTWARE <%s:%s> with path <%s> and version <%s>' % (pid, swName, swPath, swVersion)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcListByWMI] Unable to get a list of software: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcListByWMI] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcListByWMI] Returning EMPTY process to port dictionary') return None except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':getProcListByWMI] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnHPUX(localClient, USE_SUDO, USE_LSOF): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX]') procToPortDict = {} ## Get process OSHs ############################################ try: psOut = localClient.executeCmd('ps -ef') psLines = dbconnect_utils.splitCommandOutput(psOut.strip()) if psLines == None: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to get list of processes!') return None for psLine in psLines: ## Reg for processes with args res = re.search('(\w+)\s+?(\d+).*\s\d+\:\d\d\s([0-9a-zA-Z_.\[\]\-+:/]+)\s(.*)', psLine) if(res): cleanArgs = res.group(4) else: ## Reg for processes with no args res = re.search('(\w+)\s+?(\d+).*\s\d+\:\d\d\s([0-9a-zA-Z_.\-+:/]+)', psLine) if(res): cleanArgs = '' if(res): commandPath = res.group(3) pid = res.group(2) userName = res.group(1).strip() cleanCommand = '' if commandPath.find('/') == -1: cleanCommand = commandPath else: res2 = re.search('(.*/)([^/]+)', commandPath) if (res2): cleanCommand = res2.group(2) else: continue commandLine = cleanCommand + ' ' + cleanArgs dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Got PROCESS <%s:%s> with path <%s>, owner <%s>, and command line <%s>' % (pid, cleanCommand, commandPath, userName, commandLine)) ## {PID:[cleanCommand, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine, userName) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s>, owner <%s>, and command line <%s> to the procToPort dictionary' % (pid, cleanCommand, 'Running', commandPath, userName, commandLine)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to get list of processes: <%s>' % excInfo) return None ## Use LSOF to map each process to a port and create a dictionary ############################################ handleProcessToPortByLsof(USE_SUDO, localClient, procToPortDict) ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Returning EMPTY process to port dictionary') return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Exception: <%s>' % excInfo) pass
def registryLookup(procToPortDict, dbInstanceDict, localClient, wmiRegistryClient): try: # Store all found listening Port activeListenerPorts = [] for pid in procToPortDict.keys(): activeListenerPorts.append( (procToPortDict[pid])[dbconnect_utils.PORT_INDEX]) ## Locals logger.debug('Initial dbInstanceDict %s' % dbInstanceDict) instanceNameList = [] installNameTointernalInstanceName = {} # If SQL Server is present on this box, get instance names installedInstancesKeypath = 'SOFTWARE\\Microsoft\\Microsoft SQL Server' installedInstances = dbconnect_utils.getRegValues( localClient, wmiRegistryClient, installedInstancesKeypath, 'InstalledInstances') if installedInstances == None or str( installedInstances) == '[[], []]' or str( installedInstances) == '{}': if dbInstanceDict != None and len(dbInstanceDict) > 0: instancesString = '' for dbName in dbInstanceDict.keys(): instancesString = instancesString + dbName.upper() + '\n' installedInstances = {} installedInstances.update( {installedInstancesKeypath: instancesString[:-1]}) # chop last \n else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':registryLookup] SQL Server not installed on this box') return None logger.debug("Discovered installed instances %s" % installedInstances) if installedInstances: ## We have SQL Server dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] SQL Server present on this box <%s>' % installedInstances) installedInstanceNames = installedInstances[ installedInstancesKeypath] if installedInstanceNames.find( '\n') > 0 or installedInstanceNames.find(' _ ') > 0: ## Multiple SQL Server instances installedIstanceNameList = re.split(' _ |\n', installedInstanceNames) else: installedIstanceNameList = [installedInstanceNames] logger.debug('Installed instance name list %s' % installedIstanceNameList) for installedInstanceName in installedIstanceNameList: instanceNameList.append(installedInstanceName.strip()) dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Found SQL Server instance <%s>' % installedInstanceName.strip()) internalInstanceNameKeyPath = 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL' internalInstanceNameDict = dbconnect_utils.getRegValues( localClient, wmiRegistryClient, internalInstanceNameKeyPath, installedInstanceName) internalInstanceName = internalInstanceNameDict[ internalInstanceNameKeyPath] if internalInstanceName: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Found registry name <%s> for internal SQL instance name <%s>' % (internalInstanceName, installedInstanceName)) installNameTointernalInstanceName[ installedInstanceName.strip( )] = internalInstanceName.strip() else: installNameTointernalInstanceName[ installedInstanceName.strip( )] = installedInstanceName.strip() logger.debug("installNameTointernalInstanceName %s" % installNameTointernalInstanceName) logger.debug("instanceNameList %s " % instanceNameList) # If we're here, one or more SQL Server instances are present # Look for additional SQL Server information sqlServerDetailKeypaths = [ 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\MSSQLServer\\SuperSocketNetLib\\Tcp', 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\Setup', 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\MSSQLServer\\CurrentVersion', 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\Cluster', 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\Cluster' ] sqlServerDetailFilters = [ 'TcpPort', 'SQLPath', 'CurrentVersion', 'ClusterIpAddr', 'ClusterName' ] for instanceName in instanceNameList: sqlServerDetailValues = [] for sqlServerDetailIndex in range(len(sqlServerDetailKeypaths)): sqlServerDetailKeypath = '' ## Replace instance names in registry key path as appropriate if instanceName == 'MSSQLSERVER': if sqlServerDetailKeypaths[sqlServerDetailIndex].find( 'luster') > 0: sqlServerDetailKeypath = string.replace( sqlServerDetailKeypaths[sqlServerDetailIndex], 'iNsTaNcEnAmE', installNameTointernalInstanceName.get( instanceName)) else: sqlServerDetailKeypath = string.replace( sqlServerDetailKeypaths[sqlServerDetailIndex], 'Microsoft SQL Server\\iNsTaNcEnAmE', 'MSSQLServer') else: if sqlServerDetailKeypaths[sqlServerDetailIndex].find( 'luster') > 0: sqlServerDetailKeypath = string.replace( sqlServerDetailKeypaths[sqlServerDetailIndex], 'iNsTaNcEnAmE', installNameTointernalInstanceName.get( instanceName)) else: sqlServerDetailKeypath = string.replace( sqlServerDetailKeypaths[sqlServerDetailIndex], 'iNsTaNcEnAmE', instanceName) regValues = dbconnect_utils.getRegValues( localClient, wmiRegistryClient, sqlServerDetailKeypath, sqlServerDetailFilters[sqlServerDetailIndex]) if regValues == None or str(regValues) == '[[], []]' or str( regValues) == '{}': dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Got nothing for key <%s> with filter <%s>' % (sqlServerDetailKeypath, sqlServerDetailFilters[sqlServerDetailIndex])) sqlServerDetailValues.insert(sqlServerDetailIndex, None) else: sqlServerDetailValues.insert( sqlServerDetailIndex, regValues[sqlServerDetailKeypath]) dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Got value <%s> for key <%s> with filter <%s>' % (sqlServerDetailValues[sqlServerDetailIndex], sqlServerDetailKeypath, sqlServerDetailFilters[sqlServerDetailIndex])) logger.debug("instanceNameList %s " % instanceNameList) ## We should have all details for this instance now - add it to DB dictionary listenerPort = sqlServerDetailValues[0] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Got port <%s> for instance <%s>' % (listenerPort, instanceName)) installPath = sqlServerDetailValues[1] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Got path <%s> for instance <%s>' % (installPath, instanceName)) version = sqlServerDetailValues[2] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Got version <%s> for instance <%s>' % (version, instanceName)) ipAddress = dbconnect_utils.fixIP(sqlServerDetailValues[3], localClient.getIpAddress()) dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Got IP <%s> for instance <%s>' % (ipAddress, instanceName)) clusterName = sqlServerDetailValues[4] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Got Cluster Name <%s> for instance <%s>' % (clusterName, instanceName)) if clusterName: clusterIp = netutils.getHostAddress(clusterName) if clusterIp and netutils.isValidIp(clusterIp): ipAddress = clusterIp ## If the instance is already in the DB dict, don't overwrite all values if instanceName == 'MSSQLSERVER': dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Got unnamed SQL Server instance') instanceName = dbconnect_utils.getServerName(localClient) else: instanceName = dbconnect_utils.getServerName( localClient) + '\\' + instanceName.lower() installPath = installPath.lower() if instanceName in dbInstanceDict.keys(): statusFlag = ( dbInstanceDict[instanceName])[dbconnect_utils.STATUS_INDEX] # If port is already populated, don't overwrite it because # port number information from active processes (above) is # guaranteed to be correct and the registry may not be up-to-date if (dbInstanceDict[instanceName] )[dbconnect_utils.PORT_INDEX] != dbconnect_utils.UNKNOWN: if listenerPort not in activeListenerPorts: listenerPort = (dbInstanceDict[instanceName] )[dbconnect_utils.PORT_INDEX] dbInstanceDict[instanceName] = [ 'MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, statusFlag ] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Found known SQL Server <%s> instance <%s> listening at port <%s> on <%s> and installed in <%s>' % (version, instanceName, listenerPort, ipAddress, installPath)) else: dbInstanceDict[instanceName] = [ 'MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, dbconnect_utils.UNKNOWN ] dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Added SQL Server <%s> instance <%s> listening at port <%s> on <%s> and installed in <%s>' % (version, instanceName, listenerPort, ipAddress, installPath)) logger.debug("instanceNameList %s " % instanceNameList) logger.debug("dbInstanceDict %s" % dbInstanceDict) ## Replace dictionary entry of serverName\sqlInstanceName with clusterName\sqlInstanceName if clusterName and instanceName in dbInstanceDict.keys(): if instanceName.find('\\') > 0: newInstanceName = clusterName + '\\' + instanceName[ instanceName.find('\\') + 1:] else: newInstanceName = clusterName dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':registryLookup] Replacing SQL Server instance <%s> with <%s> because it is part of a cluster' % (instanceName, newInstanceName)) dbInstanceDict[newInstanceName] = dbInstanceDict[instanceName] del dbInstanceDict[instanceName] logger.debug("dbInstanceDict %s" % dbInstanceDict) #print dbInstanceDict logger.debug("instanceNameList %s " % instanceNameList) except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':registryLookup] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnLinux(localClient, USE_SUDO, USE_LSOF): try: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux]') procToPortDict = {} ## Get process OSHs ############################################ try: psOut = localClient.executeCmd( 'ps -e -o pid -o uid -o user -o cputime -o command --cols 4000' ) if psOut == None: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Unable to get list of processes!' ) return None psLines = dbconnect_utils.splitCommandOutput(psOut.strip()) if psLines == None: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Unable to get list of processes!' ) return None for psLine in psLines: psLine = psLine.strip() token = psLine.split(None, 4) # Some checks to make sure line is valid if (len(token) != 5): continue if (not re.search('^\d+$', token[0])): continue if (len(token[4]) < 2): continue spaceIndex = token[4].find(' ') commandPath = '' cleanArgs = '' if spaceIndex > -1: commandPath = token[4][:spaceIndex] try: cleanArgs = token[4][spaceIndex + 1:] except: cleanArgs = '' else: commandPath = token[4] cleanArgs = '' pid = token[0] userName = token[2] cleanCommand = '' if (commandPath.find('/') == -1) or (commandPath[0] == '['): cleanCommand = commandPath else: res2 = re.search('(.*/)([^/]+)', commandPath) if (res2): cleanCommand = res2.group(2) else: continue commandLine = cleanCommand + ' ' + cleanArgs dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Got PROCESS <%s:%s> with path <%s>, owner <%s>, and command line <%s>' % (pid, cleanCommand, commandPath, userName, commandLine)) ## {PID:[cleanCommand, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine] if dbconnect_utils.populateProcToPortDict( procToPortDict, pid, cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine, userName) == 0: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s>, owner <%s>, and command line <%s> to the procToPort dictionary' % (pid, cleanCommand, 'Running', commandPath, userName, commandLine)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Unable to get list of processes: <%s>' % excInfo) pass ## Use NETSTAT output to create an array of server ports ## and map them to server processes ############################################ try: netstatLisCmd = 'netstat -anp | grep "LISTEN"' if USE_SUDO: netstatLisCmd = 'sudo ' + netstatLisCmd netstatLisStr = localClient.executeCmd(netstatLisCmd) nsLisLines = dbconnect_utils.splitCommandOutput( netstatLisStr.strip()) if nsLisLines != None: for nsLine in nsLisLines: nsLine = nsLine.strip() dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Got nsLine <%s>' % nsLine) m = re.search('tcp.* (\S+):(\d+).*:.*\s+(\d+|-).*', nsLine) if (m): ipAddress = m.group(1).strip() ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP( ipAddress, localClient.getIpAddress()) serverPort = m.group(2).strip() pid = m.group(3).strip() dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Got port <%s> for pid <%s>' % (serverPort, pid)) if pid != '-' and procToPortDict.has_key(pid): dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Adding port <%s:%s> for process <%s>' % (ipAddress, serverPort, (procToPortDict[pid] )[dbconnect_utils.PROCESSNAME_INDEX])) (procToPortDict[pid] )[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid] )[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Couldn\'t get netstat information (Most likely due to lack of user permissions): ' + nsLine) else: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Invalid output from netstat: <%s>' % netstatLisStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Unable to make a process to port map using netstat: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Returning EMPTY process to port dictionary' ) return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Exception: <%s>' % excInfo) pass
def parseEtcFiles(localClient, p2pDict, dbDict, isWindows): try: pathsFound = [] ## Windows doesn't have /etc/oratab or /etc/oraInst.loc if isWindows == 'true': return else: ## Process oratab if found oratabLocation = dbconnect_utils.findFile(localClient, 'oratab', '/etc/', isWindows) if oratabLocation == None: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiles] oratab not found in /etc/') else: oratabContent = dbconnect_utils.getFileContent(localClient, oratabLocation[0], isWindows) if oratabContent: oratabLines = dbconnect_utils.splitCommandOutput(oratabContent) if oratabLines and len(oratabLines) > 0: for oratabLine in oratabLines: ## Ignore comment line or lines with nothing if len(oratabLine.strip()) < 1 or oratabLine.strip()[0] == '#': continue dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [1] Processing oratab line <%s>' % oratabLine) oratabLineSplit = oratabLine.strip().split(':') ## Ignore lines if the format is not sid:path:startup if len(oratabLineSplit) < 3: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [1] Ignoring oratab line <%s>' % oratabLine) continue ## We should have an instance and its path sidFound = oratabLineSplit[0].strip().lower() pathFound = oratabLineSplit[1].strip().lower() ipAddress = localClient.getIpAddress() ## If the SID is "*", ignore it and use the path if sidFound == "*": dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [1] Ignoring oracle SID <%s>' % sidFound) if pathFound not in pathsFound: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [1] Adding path <%s> to return array' % pathFound) pathsFound.append(pathFound) else: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [1] Found known path <%s>' % pathFound) continue ## If this SID already exists in the dbDict, overwrite the install path ## associated with it. If not, add and entry and path if sidFound in dbDict.keys(): (dbDict[sidFound])[dbconnect_utils.PATH_INDEX] = pathFound dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiiles] [1] Found known Oracle instance <%s> with path <%s> on <%s>' % (sidFound, pathFound, ipAddress)) else: dbDict[sidFound] = ['oracle', dbconnect_utils.UNKNOWN, ipAddress, pathFound, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiles] [1] Added Oracle instance <%s> with path <%s> on <%s>' % (sidFound, pathFound, ipAddress)) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiles] [1] Invalid entries /etc/oratab: <%s>!' % oratabContent) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiles] [1] Empty or invalid /etc/oratab!') ## Process oraInst.loc if found oraInstLocation = dbconnect_utils.findFile(localClient, 'oraInst.loc', '/etc/', isWindows) if oraInstLocation == None: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiles] oraInst.loc not found in /etc/') else: oraInstContent = dbconnect_utils.getFileContent(localClient, oraInstLocation[0], isWindows) if oraInstContent: oraInstLines = dbconnect_utils.splitCommandOutput(oraInstContent) if oraInstLines and len(oraInstLines) > 0: for oraInstLine in oraInstLines: ## Ignore comment line or lines with nothing if len(oraInstLine.strip()) < 1 or oraInstLine.strip()[0] == '#': continue dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [2] Processing oraInst line <%s>' % oraInstLine) oraInstLineSplit = oraInstLine.strip().split('=') ## Ignore lines if the format is not inventory_loc=<path> if len(oraInstLineSplit) < 2 or oraInstLineSplit[0].strip() != 'inventory_loc': dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [2] Ignoring oraInst line <%s>' % oraInstLine) continue ## We should have an install path pathFound = oraInstLineSplit[1].strip().lower() dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiles] [2] Found oracle installation path <%s>' % pathFound) if pathFound not in pathsFound: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [2] Adding path <%s> to return array' % pathFound) pathsFound.append(pathFound) else: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [2] Found known path <%s>' % pathFound) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiles] [2] Invalid entries /etc/oraInst.loc: <%s>' % oraInstContent) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiles] [2] Empty or invalid /etc/oraInst.loc!') return pathsFound except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':parseEtcFiles] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnHPUX(localClient, USE_SUDO, USE_LSOF): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX]') procToPortDict = {} ## Get process OSHs ############################################ try: psOut = localClient.executeCmd('ps -ef') psLines = dbconnect_utils.splitCommandOutput(psOut.strip()) if psLines == None: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to get list of processes!') return None for psLine in psLines: ## Reg for processes with args res = re.search('(\w+)\s+?(\d+).*\s\d+\:\d\d\s([0-9a-zA-Z_.\[\]\-+:/]+)\s(.*)', psLine) if(res): cleanArgs = res.group(4) else: ## Reg for processes with no args res = re.search('(\w+)\s+?(\d+).*\s\d+\:\d\d\s([0-9a-zA-Z_.\-+:/]+)', psLine) if(res): cleanArgs = '' if(res): commandPath = res.group(3) pid = res.group(2) userName = res.group(1).strip() cleanCommand = '' if commandPath.find('/') == -1: cleanCommand = commandPath else: res2 = re.search('(.*/)([^/]+)', commandPath) if (res2): cleanCommand = res2.group(2) else: continue commandLine = cleanCommand + ' ' + cleanArgs dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Got PROCESS <%s:%s> with path <%s>, owner <%s>, and command line <%s>' % (pid, cleanCommand, commandPath, userName, commandLine)) ## {PID:[cleanCommand, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine, userName) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s>, owner <%s>, and command line <%s> to the procToPort dictionary' % (pid, cleanCommand, 'Running', commandPath, userName, commandLine)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to get list of processes: <%s>' % excInfo) return None ## Use LSOF to map each process to a port and create a dictionary ############################################ try: pidToPortMap = {} ## We need a local pid to port map # lsofCmd = 'lsof -n -P -i | grep -i listen 2>/dev/null' lsofCmd = '/usr/local/bin/lsof -n -P -i | grep -i listen 2>/dev/null' # need to specify fullpath - Daniel La if USE_SUDO == 'true': lsofCmd = 'sudo ' + lsofCmd lsofStr = localClient.executeCmd(lsofCmd) lsofLines = dbconnect_utils.splitCommandOutput(lsofStr.strip()) if lsofLines != None: for lsofLine in lsofLines: if len(lsofLine) <1: continue lsofLine = lsofLine.strip() m = re.search('\w+\s+(\d+)\s+\w+\s+\w+\s+IPv[4|6].+TCP\s+(\S+):(\d+)\s+\(\w+\)', lsofLine) if (m): pid = m.group(1).strip() ipAddress = m.group(2).strip() ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP(ipAddress, localClient.getIpAddress()) serverPort = m.group(3).strip() pidToPortMap[pid] = [ipAddress, serverPort] if pidToPortMap != None and len(pidToPortMap) > 0: for pid in pidToPortMap.keys(): if pid in procToPortDict.keys(): ipAddress = (pidToPortMap[pid])[0] ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue serverPort = (pidToPortMap[pid])[1] dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Found port <%s:%s> for pid <%s>' % (ipAddress, serverPort, pid)) (procToPortDict[pid])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid])[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] No TCP port associated with PID [' + pID + ']: ' + lsofLine) else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to make a process to port map using LSOF: ' + lsofStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to make a process to port map using LSOF: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Returning EMPTY process to port dictionary') return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Exception: <%s>' % excInfo) pass
def getInstancePaths(localClient, isWindows, possibleInstallLocations): try: instancePaths = [] ## Prepopulate possible install locations with some common directories if len(possibleInstallLocations) < 1: if isWindows == 'true': possibleInstallLocations.append('%HOMEDRIVE%\ibm') possibleInstallLocations.append('%SYSTEMDRIVE%\ibm') possibleInstallLocations.append('%PROGRAMFILES%\ibm') possibleInstallLocations.append('%PROGRAMFILES(x86)%\ibm') possibleInstallLocations.append('%HOMEDRIVE%\db2') possibleInstallLocations.append('%SYSTEMDRIVE%\db2') possibleInstallLocations.append('%PROGRAMFILES%\db2') possibleInstallLocations.append('%PROGRAMFILES(x86)%\db2') possibleInstallLocations.append('%DB2_HOME%') possibleInstallLocations.append('%DB2HOME%') else: possibleInstallLocations.append('/u01') possibleInstallLocations.append('/u02') possibleInstallLocations.append('/opt') possibleInstallLocations.append('/usr/opt') possibleInstallLocations.append('/usr/local') possibleInstallLocations.append('$DB2_HOME') possibleInstallLocations.append('$DB2HOME') for location in possibleInstallLocations: logger.debug('location to search is: ', location) # added for debugging - Daniel La ## Search for DB2 command processor executable db2cmdLocations = [] ## DB2 command processor executable has a different name on Windows if isWindows == 'true': db2cmdLocations = dbconnect_utils.findFile(localClient, 'db2cmd.exe', location, isWindows) else: db2cmdLocations = dbconnect_utils.findFile(localClient, 'db2', location, isWindows) ## If an executable was found, check if it is indeed a DB2 ## instance and extract the install path if db2cmdLocations and len(db2cmdLocations) > 0: logger.debug('location is ', db2cmdLocations) # added for debugging - Daniel La for db2cmdLocation in db2cmdLocations: if not dbconnect_utils.isValidString(db2cmdLocation): dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getInstancePaths] (1) DB2 command processor executable found in an invalid location <%s>! Skipping...' % location) continue if db2cmdLocation.lower().find('sqllib') > 0: logger.debug('we are here') #instancePath = db2cmdLocation[:db2cmdLocation.lower().find('sqllib')-1] # Instance path is upto the "sqllib" string minus trailing slash instancePath = '' if isWindows == 'true': instancePath = db2cmdLocation[:db2cmdLocation.rfind('\\')] else: instancePath = db2cmdLocation[:db2cmdLocation.rfind('/')] if dbconnect_utils.isValidString(instancePath) and instancePath not in instancePaths: # instanceName = '' # if isWindows == 'true': # instanceName = instancePath[instancePath.rfind('\\'):] # else: # instanceName = instancePath[instancePath.rfind('/'):] # if dbconnect_utils.isValidString(instanceName) and not instanceName.strip().lower() == 'ibm': ## dbDict[instanceName] = ['db2', dbconnect_utils.UNKNOWN, instancePath, dbconnect_utils.UNKNOWN, localClient.getIpAddress()] # dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getInstancePaths] (1) Found DB2 command processor for instance <%s> at <%s>. Appending <%s> to install locations' % (instanceName, location, instancePath)) # else: # dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getInstancePaths] (1) Found DB2 command processor at <%s> with an invalid instance name! Skipping...' % location) # continue instancePaths.append(instancePath) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getInstancePaths] (1) Found DB2 command processor at <%s> already in install locations' % location) continue else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getInstancePaths] (1) DB2 command processor executable found in <%s> does not contain "sqllib" in its path. This is most likely not valid for DB2...skipping!' % location) continue else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getInstancePaths] (1) No DB2 command processor found in <%s>' % location) continue # ## Search for db2set executable # db2setLocations = dbconnect_utils.findFile(localClient, 'db2set', location, isWindows) # ## If an executable was found, check if it is indeed a DB2 # ## instance and extract the install path # if db2setLocations and len(db2setLocations) > 1: # for db2setLocation in db2setLocations: # if not dbconnect_utils.isValidString(db2setLocation): # dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getInstancePaths] (2) db2set executable found in an invalid location...skipping!' % location) # continue # if db2setLocation.lower().find('sqllib') > 0: # instancePath = db2setLocation[:db2setLocation.lower().find('sqllib')] # if dbconnect_utils.isValidString(instancePath) and instancePath not in instancePaths: # instanceName = '' # if isWindows == 'true': # instanceName = instancePath[instancePath.rfind('\\'):] # else: # instanceName = instancePath[instancePath.rfind('/'):] # if dbconnect_utils.isValidString(instanceName): ## dbDict[instanceName] = ['db2', dbconnect_utils.UNKNOWN, instancePath, dbconnect_utils.UNKNOWN, localClient.getIpAddress()] # dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getInstancePaths] (1) Found DB2 command processor for instance <%s> at <%s>. Appending <%s> to install locations' % (instanceName, location, instancePath)) # else: # dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getInstancePaths] (1) Found DB2 command processor at <%s> with an invalid instance name...skipping!' % location) # continue # instancePaths.append(instancePath) # else: # dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getInstancePaths] (2) Found db2set at <%s> already in install locations' % location) # else: # dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getInstancePaths] (2) db2set executable found in <%s> does not contain "sqllib" in its path. This is most likely not valid for DB2...skipping!' % location) # continue # else: # dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getInstancePaths] (2) No db2set executable found in <%s>' % location) # continue return instancePaths except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':getInstancePaths] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnAIX(localClient, USE_SUDO, USE_LSOF): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX]') procToPortDict = {} ## Get process OSHs ############################################ try: psOut = localClient.executeCmd("ps -e -o 'user,pid,time,args'") psLines = dbconnect_utils.splitCommandOutput(psOut.strip()) if psLines == None: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to get list of processes!') return None for psLine in psLines: if(re.search('TIME COMMAND', psLine)): continue ## Reg for processes with args res = re.search('(\w+)\s+?(\d+).*:\d\d\s([0-9a-zA-Z_.\[\]\-+:/]+)\s(.*)', psLine) if(res): cleanArgs = res.group(4) else: ## Reg for processes with no args res = re.search('(\w+)\s+?(\d+).*:\d\d\s([0-9a-zA-Z_.\[\]\-+:/]+)', psLine) if(res): cleanArgs = '' if(res): commandPath = res.group(3) pid = res.group(2) userName = res.group(1) cleanCommand = '' if commandPath.find('/') == -1: cleanCommand = commandPath else: res2 = re.search('(.*/)([^/]+)', commandPath) if (res2): cleanCommand = res2.group(2) else: continue commandLine = cleanCommand + ' ' + cleanArgs dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Got PROCESS <%s:%s> with path <%s>, owner <%s>, and command line <%s>' % (pid, cleanCommand, commandPath, userName, commandLine)) if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine, userName) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s>, owner <%s>, and command line <%s> to the procToPort dictionary' % (pid, cleanCommand, 'Running', commandPath, userName, commandLine)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to get list of processes: <%s>' % excInfo) pass if USE_LSOF.lower().strip() == 'true': ## Use LSOF to map each process to a port and create a dictionary ############################################ try: keydeletion = [] # keys to delete - Daniel La pidToPortMap = {} ## We need a local pid to port map # lsofCmd = 'lsof -n -P -i | grep -i listen 2>/dev/null' lsofCmd = '/usr/local/bin/lsof -n -P -i | grep -i listen 2>/dev/null' # Daniel La need to specify full path to lsof. 22/11/10 if USE_SUDO == 'true': lsofCmd = 'sudo ' + lsofCmd lsofStr = localClient.executeCmd(lsofCmd) lsofLines = dbconnect_utils.splitCommandOutput(lsofStr.strip()) if lsofLines != None: for lsofLine in lsofLines: if len(lsofLine) <1: continue lsofLine = lsofLine.strip() m = re.search('\w+\s+(\d+)\s+\w+\s+\w+\s+IPv[4|6].+TCP\s+(\S+):(\d+)\s+\(\w+\)', lsofLine) if (m): pid = m.group(1).strip() ipAddress = m.group(2).strip() ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP(ipAddress, localClient.getIpAddress()) serverPort = m.group(3).strip() # pidToPortMap[pid] = [ipAddress, serverPort] # below lines were added - Daniel La logger.debug('serverport is: ', serverPort, ',pid is: ', pid, ',ip is: ', ipAddress) pidnport = str(pid) + '.' + str(serverPort) pidToPortMap[pidnport] = [ipAddress, serverPort] # end of add. if pidToPortMap != None and len(pidToPortMap) > 0: for pid in pidToPortMap.keys(): logger.debug('before pid is: ', pid) m = re.search('(\w+)\.\w+', pid) pidsplit = m.group(1).strip() logger.debug('after found pid is: ', pidsplit) # if pid in procToPortDict.keys(): if pidsplit in procToPortDict.keys(): ipAddress = (pidToPortMap[pid])[0] ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue serverPort = (pidToPortMap[pid])[1] #dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Found port <%s:%s> for pid <%s>' % (ipAddress, serverPort, pid)) #(procToPortDict[pid])[dbconnect_utils.IP_INDEX] = ipAddress #(procToPortDict[pid])[dbconnect_utils.PORT_INDEX] = serverPort dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Found port <%s:%s> for pid <%s>' % (ipAddress, serverPort, pidsplit)) (procToPortDict[pidsplit])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pidsplit])[dbconnect_utils.PORT_INDEX] = serverPort # create new key and add new entry - Daniel La logger.debug('creating new entry for: ', (procToPortDict[pidsplit])[dbconnect_utils.PROCESSNAME_INDEX], ' pid: ', pidsplit, ' port: ', serverPort) pidnport = str(pidsplit) + '.' + str(serverPort) if dbconnect_utils.populateProcToPortDict(procToPortDict, pidnport, (procToPortDict[pidsplit])[dbconnect_utils.PROCESSNAME_INDEX], (procToPortDict[pidsplit])[dbconnect_utils.PORT_INDEX], (procToPortDict[pidsplit])[dbconnect_utils.IP_INDEX], (procToPortDict[pidsplit])[dbconnect_utils.PATH_INDEX], (procToPortDict[pidsplit])[dbconnect_utils.VERSION_INDEX], (procToPortDict[pidsplit])[dbconnect_utils.STATUS_INDEX], (procToPortDict[pidsplit])[dbconnect_utils.COMMANDLINE_INDEX], (procToPortDict[pidsplit])[dbconnect_utils.USER_INDEX]) == 0: logger.debug('Unable to add ', (procToPortDict[pidsplit])[dbconnect_utils.PROCESSNAME_INDEX]) else: # add key to list of keys to delete later as we've updated them with new entries. keydeletion.append(pidsplit) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] No TCP port associated with PID [' + pID + ']: ' + lsofLine) else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to make a process to port map using LSOF: ' + lsofStr) # delete keys which are of no value anymore - Daniel La for key in keydeletion: logger.debug('key is: ', key) if procToPortDict.has_key(key): logger.debug('deleted key: ', key, ' process: ', (procToPortDict[key])[dbconnect_utils.PROCESSNAME_INDEX]) del procToPortDict[key] except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to make a process to port map using LSOF: <%s>' % excInfo) pass else: ## Try using netstat and KDB try: pidToPortMap = {} ## We need a local pid to port map netstatLisCmd = 'netstat -Aanf inet | grep "LISTEN"' if USE_SUDO == 'true': netstatLisCmd = 'sudo ' + netstatLisCmd netstatLisStr = localClient.executeCmd(netstatLisCmd) nsLisLines = dbconnect_utils.splitCommandOutput(netstatLisStr.strip()) if nsLisLines != None: for nsLine in nsLisLines: nsLine = nsLine.strip() # m = re.search('(\w+)\s+tcp\d?\s+\d+\s+\d+\s+(\*|\d+.\d+.\d+.\d+).(\d+)\s+(\*|\d+.\d+.\d+.\d+).(\*|\d+)\s+\S+', nsLine) m = re.search('(\w+)\s+tcp\d?\s+\d+\s+\d+\s+(\*|\d+.\d+.\d+.\d+).(\d+)\s+(\*|\d+.\d+.\d+.\d+).(\*|\d+).*', nsLine) if (m): ipAddress = m.group(2).strip() ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP(ipAddress, localClient.getIpAddress()) serverPort = m.group(3).strip() pid = getAIXpIDfromAddress(localClient, m.group(1).strip(), USE_SUDO) if pid != None: pidToPortMap[pid] = [ipAddress, serverPort] if pidToPortMap != None and len(pidToPortMap) > 0: for pid in pidToPortMap.keys(): if pid in procToPortDict.keys(): ipAddress = (pidToPortMap[pid])[0] ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue serverPort = (pidToPortMap[pid])[1] dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Found port <%s:%s> for pid <%s>' % (ipAddress, serverPort, pid)) (procToPortDict[pid])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid])[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to make a process to port map using netstat and kdb: <%s>' % netstatLisStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to make a process to port map using netstat and kdb: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Returning EMPTY process to port dictionary') return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Exception: <%s>' % excInfo) pass
def processProcToPortDict(localClient, p2pDict, dbDict): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict]') tnslsnrPort = dbconnect_utils.UNKNOWN tnslsnrIp = dbconnect_utils.UNKNOWN installPath = dbconnect_utils.UNKNOWN for pid in p2pDict.keys(): processName = (p2pDict[pid])[dbconnect_utils.PROCESSNAME_INDEX].lower() listenerPort = (p2pDict[pid])[dbconnect_utils.PORT_INDEX] ipAddress = (p2pDict[pid])[dbconnect_utils.IP_INDEX] if ipAddress == dbconnect_utils.UNKNOWN: ipAddress = localClient.getIpAddress() path = (p2pDict[pid])[dbconnect_utils.PATH_INDEX] version = dbconnect_utils.UNKNOWN statusFlag = (p2pDict[pid])[dbconnect_utils.STATUS_INDEX] sidFound = '' ## See if a TNS listener is present ## If present, get the listener port and install path if re.search('tnslsnr', processName) or re.search('tnslistener', processName): tnslsnrPort = listenerPort tnslsnrIp = ipAddress binPath = path[:path.strip().lower().find('tnslsnr')] installPath = binPath[:len(binPath)-4] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] (1) Found TNS Listener at port <%s> from process <%s> with path <%s>' % (listenerPort, processName, path)) ## Next, check for oracle process and service names to extract SID elif re.search('dbconsole', processName) or re.search('jobscheduler', processName) or re.search('oradb10g', processName) or re.search('oracleora9ias_', processName) or re.search('oracle-oracleas_', processName) or re.search('oradb11g', processName) or re.search('mtsrecovery', processName) or re.search('remexec', processName): dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':processProcToPortDict] (2) Found process name <%s>. Ignoring...' % processName) ## If we don't filter these out, the next check for "oracle" ## will catch it and create a database with incorrect SIDs continue elif re.search('oracleservice', processName): sidRegexStr = re.search('oracleservice(\w+)', processName) if sidRegexStr: sidFound = sidRegexStr.group(1).strip() dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] (3) Found Oracle instance <%s> from process name <%s> and its path is <%s>' % (sidFound, processName, path)) elif re.search('oracle', processName): sidRegexStr = re.search('oracle(\w+)', processName) if sidRegexStr: sidFound = sidRegexStr.group(1).strip() dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] (4) Found Oracle instance <%s> from process name <%s> and its path is <%s>' % (sidFound, processName, path)) elif re.search('ora_pmon', processName): sidRegexStr = re.search('ora_pmon_(\w+)', processName) if sidRegexStr: sidFound = sidRegexStr.group(1).strip() dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] (5) Found Oracle instance <%s> from process name <%s> and its path is <%s>' % (sidFound, processName, path)) if sidFound != None and sidFound != '' and len(sidFound) >0 and sidFound not in dbDict.keys(): dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] Adding Oracle instance <%s> listening at port <%s>, on <%s>, and installed in <%s>' % (sidFound, tnslsnrPort, ipAddress, installPath)) dbDict[sidFound] = ['oracle', tnslsnrPort, tnslsnrIp, installPath, version, statusFlag] ## Set path and port to latest available info from TNS process if unknown for sid in dbDict.keys(): ## Set port to latest TNS listener port if unknown if (dbDict[sid])[dbconnect_utils.PORT_INDEX] == dbconnect_utils.UNKNOWN: #(dbDict[sid])[dbconnect_utils.PORT_INDEX] = tnslsnrPort (dbDict[sid])[dbconnect_utils.IP_INDEX] = tnslsnrIp ## Set path to latest available path from tns listener process if unknown if (dbDict[sid])[dbconnect_utils.PATH_INDEX] == dbconnect_utils.UNKNOWN: (dbDict[sid])[dbconnect_utils.PATH_INDEX] = installPath except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':processProcToPortDict] Exception: <%s>' % excInfo) pass
def processProcToPortDict(localClient, p2pDict, dbDict): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict]') tnslsnrPort = dbconnect_utils.UNKNOWN tnslsnrIp = dbconnect_utils.UNKNOWN installPath = dbconnect_utils.UNKNOWN # check how many TNS listener entries there are. If there are more than one, # then we don't set listening port information for Oracle DBs. # If there is only one, then we can set. # Daniel La totaltnslsnr = 0; for pid in p2pDict.keys(): processName = (p2pDict[pid])[dbconnect_utils.PROCESSNAME_INDEX].lower() if re.search('tnslsnr', processName) or re.search('tnslistener', processName): logger.debug('listener pid: ', pid, ' listener procname: ', processName, ' port: ', (p2pDict[pid])[dbconnect_utils.PORT_INDEX]) if (p2pDict[pid])[dbconnect_utils.PORT_INDEX] != 'UNKNOWN': totaltnslsnr += 1 logger.debug('total tnslsnr entries with valid listening port: ', totaltnslsnr) for pid in p2pDict.keys(): processName = (p2pDict[pid])[dbconnect_utils.PROCESSNAME_INDEX].lower() listenerPort = (p2pDict[pid])[dbconnect_utils.PORT_INDEX] ipAddress = (p2pDict[pid])[dbconnect_utils.IP_INDEX] if ipAddress == dbconnect_utils.UNKNOWN: ipAddress = localClient.getIpAddress() path = (p2pDict[pid])[dbconnect_utils.PATH_INDEX] version = dbconnect_utils.UNKNOWN statusFlag = (p2pDict[pid])[dbconnect_utils.STATUS_INDEX] sidFound = '' ## See if a TNS listener is present ## If present, get the listener port and install path if re.search('tnslsnr', processName) or re.search('tnslistener', processName): tnslsnrPort = listenerPort tnslsnrIp = ipAddress binPath = path[:path.strip().lower().find('tnslsnr')] installPath = binPath[:len(binPath)-4] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] (1) Found TNS Listener at port <%s> from process <%s> with path <%s>' % (listenerPort, processName, path)) ## Next, check for oracle process and service names to extract SID elif re.search('dbconsole', processName) or re.search('jobscheduler', processName) or re.search('oradb10g', processName) or re.search('oracleora9ias_', processName) or re.search('oracle-oracleas_', processName) or re.search('oradb11g', processName) or re.search('mtsrecovery', processName) or re.search('remexec', processName): dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':processProcToPortDict] (2) Found process name <%s>. Ignoring...' % processName) ## If we don't filter these out, the next check for "oracle" ## will catch it and create a database with incorrect SIDs continue elif re.search('oracleservice', processName): sidRegexStr = re.search('oracleservice(\w+)', processName) if sidRegexStr: sidFound = sidRegexStr.group(1).strip() dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] (3) Found Oracle instance <%s> from process name <%s> and its path is <%s>' % (sidFound, processName, path)) # Commented out by Daniel La 11/01/11 # Creating wrong Oracle DB CI's. # elif re.search('oracle', processName): # sidRegexStr = re.search('oracle(\w+)', processName) # if sidRegexStr: # sidFound = sidRegexStr.group(1).strip() # dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] (4) Found Oracle instance <%s> from process name <%s> and its path is <%s>' % (sidFound, processName, path)) elif re.search('ora_pmon', processName): sidRegexStr = re.search('ora_pmon_(\w+)', processName) if sidRegexStr: sidFound = sidRegexStr.group(1).strip() dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] (5) Found Oracle instance <%s> from process name <%s> and its path is <%s>' % (sidFound, processName, path)) if sidFound != None and sidFound != '' and len(sidFound) >0 and sidFound not in dbDict.keys(): #dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] Adding Oracle instance <%s> listening at port <%s>, on <%s>, and installed in <%s>' % (sidFound, tnslsnrPort, ipAddress, installPath)) #dbDict[sidFound] = ['oracle', tnslsnrPort, tnslsnrIp, installPath, version, statusFlag] if totaltnslsnr > 1: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] Adding Oracle instance <%s> listening at port UNKNOWN, on <%s>, and installed in UNKNOWN' % (sidFound, ipAddress)) dbDict[sidFound] = ['oracle', dbconnect_utils.UNKNOWN, ipAddress, dbconnect_utils.UNKNOWN, version, statusFlag] else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] Adding Oracle instance <%s> listening at port <%s>, on <%s>, and installed in <%s>' % (sidFound, tnslsnrPort, ipAddress, installPath)) dbDict[sidFound] = ['oracle', tnslsnrPort, tnslsnrIp, installPath, version, statusFlag] ## Set path and port to latest available info from TNS process if unknown for sid in dbDict.keys(): ## Set port to latest TNS listener port if unknown # if (dbDict[sid])[dbconnect_utils.PORT_INDEX] == dbconnect_utils.UNKNOWN: if (dbDict[sid])[dbconnect_utils.PORT_INDEX] == dbconnect_utils.UNKNOWN and totaltnslsnr == 1: (dbDict[sid])[dbconnect_utils.PORT_INDEX] = tnslsnrPort # not sure why this line is commented out - Daniel La (dbDict[sid])[dbconnect_utils.IP_INDEX] = tnslsnrIp ## Set path to latest available path from tns listener process if unknown # if (dbDict[sid])[dbconnect_utils.PATH_INDEX] == dbconnect_utils.UNKNOWN: if (dbDict[sid])[dbconnect_utils.PATH_INDEX] == dbconnect_utils.UNKNOWN and totaltnslsnr == 1: (dbDict[sid])[dbconnect_utils.PATH_INDEX] = installPath except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':processProcToPortDict] Exception: <%s>' % excInfo) pass
def getListenerPort(localClient, isWindows, instancePath, instance=None): try: returnPort = dbconnect_utils.UNKNOWN getDbmConfigOutput = None # getDbmConfigCommand = 'export DB2NODE=127.0.0.1; ' + instancePath + '/db2 get dbm config; db2 terminate' original # getDbmConfigCommand = 'export DB2NODE=127.0.0.1; ' + instancePath + '/db2 get dbm config; ' + instancePath + '/db2 terminate' # modified by Daniel La # getDbmConfigCommand = '. ' + instancePath + '/../db2profile; ' + 'export DB2NODE=127.0.0.1; ' + instancePath + '/db2 get dbm config; ' + instancePath + '/db2 terminate' # modified by Daniel La getDbmConfigCommand = 'unset LIBPATH; cd ' + instancePath + '/../; . ./db2profile; ' + 'export DB2NODE=127.0.0.1; ' + instancePath + '/db2 get dbm config; ' + instancePath + '/db2 terminate' # modified by Daniel La if isWindows == 'true': getDbmConfigCommand = '(\"' + instancePath + '\\db2envar.bat\") && ' + '(set DB2INSTANCE=' + instance + ') && ' + '(\"' + instancePath + '\\db2cmd\" /c /w /i db2 get dbm config)' getDbmConfigOutput = localClient.executeCmd(getDbmConfigCommand) if not getDbmConfigOutput or not re.search('Database Manager Configuration', getDbmConfigOutput): dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getListenerPort] Invalid output from command db2 list db directory for instance at <%s>! Skipping...' % instancePath) return returnPort ## Split the command output into individial lines getDbmConfigOutputLines= dbconnect_utils.splitCommandOutput(getDbmConfigOutput) serviceName = None ## Get service name of this instance for getDbmConfigOutputLine in getDbmConfigOutputLines: ## Only one line will have the service name and ## nothing else is required from this command output if serviceName: continue ## Service name m = re.search('TCP/IP [Ss]ervice [Nn]ame\s+\(([^)]+)\)\s*=\s*(\S+)', getDbmConfigOutputLine) if (m): serviceName = m.group(2) dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getListenerPort] (1) Found service name <%s> for instance in path <%s>' % (serviceName, instancePath)) ## This may be in two separate lines parseService = 0 if (re.search('TCP/IP [Ss]ervice', getDbmConfigOutputLine)): parseService = 1 continue m = re.search('[Nn]ame\s+\(([^)]+)\)\s*=\s*(\S+)', getDbmConfigOutputLine) if parseService and m: parseService = 0 serviceName = m.group(2) dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getListenerPort] (2) Found service name <%s> for instance in path <%s>' % (serviceName, instancePath)) ## Get the port number from services config file if serviceName: # getPortCommand = 'cat /etc/services | grep %s' % serviceName # original getPortCommand = 'cat /etc/services | grep -w %s' % serviceName # updated to include -w option for direct word match - Daniel La 24/11/10 getPortCommand2 = 'type %%WINDIR%%\\system32\\drivers\\etc\\services' # altered by Daniel La getPortCommand2 = 'echo %%WINDIR%%' # altered by Daniel La getPortCommandOutput2 = localClient.executeCmd(getPortCommand2) if isWindows == 'true': #getPortCommand = 'type %%WINDIR%%\\system32\\drivers\\etc\\services | find "%s"' % serviceName getPortCommand = 'type %%WINDIR%%\\system32\\drivers\\etc\\services | findstr /I "%s\>"' % serviceName # altered by Daniel La getPortCommandOutput = localClient.executeCmd(getPortCommand) if not getPortCommandOutput or not re.search('tcp', getPortCommandOutput): dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getListenerPort] Unable to get port number from services file for instance at <%s> with service name <%s>' % (instancePath, serviceName)) return returnPort m = re.search('^\s*(\S+)\s+(\d+).*$', getPortCommandOutput.strip()) if (m): returnPort = m.group(2) return returnPort except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':getListenerPort] Exception: <%s>' % excInfo) pass
def findTnsnamesOra(localClient, p2pDict, dbDict, isWindows, installLocs): try: ## Locals searchLocations = [] # Try locations in the database dictionary if len(dbDict) > 0: logger.debug('dbDict locations') for sid in dbDict.keys(): if (dbDict[sid])[dbconnect_utils.PATH_INDEX] != dbconnect_utils.UNKNOWN: path = (dbDict[sid])[dbconnect_utils.PATH_INDEX].lower() if path in searchLocations: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [1] <%s> already in search locations' % path) continue elif path.find('\\') > 0 or path.find('/') >= 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':findTnsnamesOra] [1] Adding <%s> to search locations' % path) searchLocations.append(path) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [1] <%s> is not a valid path' % path) continue # Try locations in the p2p dictionary if len(p2pDict) > 0: logger.debug('p2pDict locations') for pid in p2pDict.keys(): processName = (p2pDict[pid])[dbconnect_utils.PROCESSNAME_INDEX].lower() path = (p2pDict[pid])[dbconnect_utils.PATH_INDEX].lower() if re.search('tns', processName) or re.search('dbconsole', processName) or re.search('jobscheduler', processName) or re.search('oradb', processName) or re.search('oracle', processName) or re.search('ora_', processName): ## Remove /bin/tnslsnr from TNS process path if re.search('/bin/tnslsnr', path): path = path[:path.find('/bin/tnslsnr')] if path in searchLocations: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [2] <%s> already in search locations' % path) continue elif path.find('\\') > 0 or path.find('/') >= 0: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [2] Adding <%s> to search locations' % path) searchLocations.append(path) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [2] <%s> is not a valid path' % path) continue # If we have no search locations so far, try some known/standard ones if 1: #len(searchLocations) < 1: if isWindows == 'true': logger.debug('additional locations') searchLocations.append('%HOMEDRIVE%\oracle') searchLocations.append('%SYSTEMDRIVE%\oracle') searchLocations.append('%PROGRAMFILES%\oracle') searchLocations.append('%PROGRAMFILES(x86)%\oracle') searchLocations.append('%ORA_HOME%') searchLocations.append('%ORACLE_HOME%') #searchLocations.append('%ORACLE_HOME%\\network\\admin') else: searchLocations.append('/u01') searchLocations.append('/u02') searchLocations.append('/opt') searchLocations.append('/usr/local') searchLocations.append('$ORACLE_HOME') #searchLocations.append('$ORACLE_HOME/network/admin') searchLocations.append('$ORA_HOME') searchLocations.append('$ORACLE_BASE') # Add oracle paths found from other sources if installLocs and len(installLocs) > 0: logger.debug('other locations') for installLoc in installLocs: if installLoc and len(installLoc) > 0: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] [3] Adding <%s> to search locations' % installLoc) searchLocations.append(installLoc) logger.debug('list of locations...') for location in searchLocations: logger.debug('*** location: ', location) # Search filesystem and parse tnsnames.ora entries for location in searchLocations: tnsnamesLocations = dbconnect_utils.findFile(localClient, 'tnsnames.ora', location, isWindows) if tnsnamesLocations == None: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] No tnsnames.ora found in <%s>' % location) continue for tnsnamesLocation in tnsnamesLocations: # We don't want the sample TNSNAMES.ORA which is # installed by default if re.search('sample', tnsnamesLocation.lower()): dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] Skipping sample tnsnames.ora in <%s>' % tnsnamesLocation) continue tnsnamesContent = dbconnect_utils.getFileContent(localClient, tnsnamesLocation, isWindows) dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':findTnsnamesOra] Got content of <%s>: <%s>' % (tnsnamesLocation, tnsnamesContent)) if tnsnamesContent != None or tnsnamesContent != '' or len(tnsnamesContent) <1: tnsEntries = dbutils.parseTNSNames(tnsnamesContent, '') for tnsEntry in tnsEntries: sidFound = tnsEntry[3].strip().lower() ## Truncate domain name if this is fully qualified SID if sidFound.find('.') > 0: shortSID = sidFound[:sidFound.find('.')] dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':findTnsnamesOra] Stripping domain from SID <%s> to <%s>' % (sidFound, shortSID)) sidFound = shortSID tnslsnrPort = tnsEntry[2].strip().lower() ipAddress = dbconnect_utils.fixIP(tnsEntry[5].strip().lower(), localClient.getIpAddress()) if ipAddress == dbconnect_utils.UNKNOWN: dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':findTnsnamesOra] Skipping instance <%s> listening at port <%s> because it\'s IP address is not valid' % (sidFound, tnslsnrPort)) continue if sidFound in dbDict.keys(): installPath = (dbDict[sidFound])[dbconnect_utils.PATH_INDEX] version = (dbDict[sidFound])[dbconnect_utils.VERSION_INDEX] statusFlag = (dbDict[sidFound])[dbconnect_utils.STATUS_INDEX] # If port and IP are already populated, don't overwrite them # because this information from active processes (above) is # guaranteed to be correct and tnsnames.ora may not be up-to-date ## Vinay 01/04/2010 - Commenting out conditional update below ## because the port and IP for an Oracle instance is not on the Oracle ## process but on the TNS listener process which may be listening for ## multiple instances on different ports. This makes associating an ## Oracle instance to its corresponding port impossible. ## So any ports found in TNSnames.ora will be used to overwrite ## previously found ports # if (dbDict[sidFound])[dbconnect_utils.PORT_INDEX] != dbconnect_utils.UNKNOWN: # tnslsnrPort = (dbDict[sidFound])[dbconnect_utils.PORT_INDEX] # if (dbDict[sidFound])[dbconnect_utils.IP_INDEX] != dbconnect_utils.UNKNOWN: # ipAddress = (dbDict[sidFound])[dbconnect_utils.IP_INDEX] dbDict[sidFound] = ['oracle', tnslsnrPort, ipAddress, installPath, version, statusFlag] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] Found known Oracle instance <%s> listening at port <%s> on <%s>' % (sidFound, tnslsnrPort, ipAddress)) # Don't want to add non running databases - Daniel La # else: # dbDict[sidFound] = ['oracle', tnslsnrPort, ipAddress, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN] # dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':findTnsnamesOra] Added Oracle instance <%s> listening at port <%s> on <%s>' % (sidFound, tnslsnrPort, ipAddress)) else: logger.debug('[' + SCRIPT_NAME + ':findTnsnamesOra] Invalid TNSNAMES.ORA at <%s>: <%s>' % (tnsnamesLocation, tnsnamesContent)) except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':findTnsnamesOra] Exception: <%s>' % excInfo) pass
def registryLookup(procToPortDict, dbInstanceDict, localClient, wmiRegistryClient): try: # Store all found listening Port activeListenerPorts = [] for pid in procToPortDict.keys(): activeListenerPorts.append((procToPortDict[pid])[dbconnect_utils.PORT_INDEX]) ## Locals logger.debug('Initial dbInstanceDict %s' % dbInstanceDict) instanceNameList = [] installNameTointernalInstanceName = {} # If SQL Server is present on this box, get instance names installedInstancesKeypath = 'SOFTWARE\\Microsoft\\Microsoft SQL Server' installedInstances = dbconnect_utils.getRegValues(localClient, wmiRegistryClient, installedInstancesKeypath, 'InstalledInstances') if installedInstances == None or str(installedInstances) == '[[], []]' or str(installedInstances) == '{}': if dbInstanceDict != None and len(dbInstanceDict) > 0: instancesString = '' for dbName in dbInstanceDict.keys(): instancesString = instancesString + dbName.upper() + '\n' installedInstances = {} installedInstances.update({installedInstancesKeypath:instancesString[:-1]}) # chop last \n else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':registryLookup] SQL Server not installed on this box') return None logger.debug("Discovered installed instances %s" % installedInstances) if installedInstances: ## We have SQL Server dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] SQL Server present on this box <%s>' % installedInstances) installedInstanceNames = installedInstances[installedInstancesKeypath] if installedInstanceNames.find('\n') > 0 or installedInstanceNames.find(' _ ') > 0: ## Multiple SQL Server instances installedIstanceNameList = re.split(' _ |\n', installedInstanceNames) else: installedIstanceNameList = [installedInstanceNames] logger.debug('Installed instance name list %s' % installedIstanceNameList) for installedInstanceName in installedIstanceNameList: instanceNameList.append(installedInstanceName.strip()) dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Found SQL Server instance <%s>' % installedInstanceName.strip()) internalInstanceNameKeyPath = 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL' internalInstanceNameDict = dbconnect_utils.getRegValues(localClient, wmiRegistryClient, internalInstanceNameKeyPath, installedInstanceName) internalInstanceName = internalInstanceNameDict[internalInstanceNameKeyPath] if internalInstanceName: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Found registry name <%s> for internal SQL instance name <%s>' % (internalInstanceName, installedInstanceName)) installNameTointernalInstanceName[installedInstanceName.strip()] = internalInstanceName.strip() else: installNameTointernalInstanceName[installedInstanceName.strip()] = installedInstanceName.strip() logger.debug("installNameTointernalInstanceName %s" % installNameTointernalInstanceName) logger.debug("instanceNameList %s " % instanceNameList) # If we're here, one or more SQL Server instances are present # Look for additional SQL Server information sqlServerDetailKeypaths = ['SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\MSSQLServer\\SuperSocketNetLib\\Tcp', 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\Setup', 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\MSSQLServer\\CurrentVersion', 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\Cluster', 'SOFTWARE\\Microsoft\\Microsoft SQL Server\\iNsTaNcEnAmE\\Cluster'] sqlServerDetailFilters = ['TcpPort', 'SQLPath', 'CurrentVersion', 'ClusterIpAddr', 'ClusterName'] for instanceName in instanceNameList: sqlServerDetailValues = [] for sqlServerDetailIndex in range(len(sqlServerDetailKeypaths)): sqlServerDetailKeypath = '' ## Replace instance names in registry key path as appropriate if instanceName == 'MSSQLSERVER': if sqlServerDetailKeypaths[sqlServerDetailIndex].find('luster') > 0: sqlServerDetailKeypath = string.replace(sqlServerDetailKeypaths[sqlServerDetailIndex], 'iNsTaNcEnAmE', installNameTointernalInstanceName.get(instanceName)) else: sqlServerDetailKeypath = string.replace(sqlServerDetailKeypaths[sqlServerDetailIndex], 'Microsoft SQL Server\\iNsTaNcEnAmE', 'MSSQLServer') else: if sqlServerDetailKeypaths[sqlServerDetailIndex].find('luster') > 0: sqlServerDetailKeypath = string.replace(sqlServerDetailKeypaths[sqlServerDetailIndex], 'iNsTaNcEnAmE', installNameTointernalInstanceName.get(instanceName)) else: sqlServerDetailKeypath = string.replace(sqlServerDetailKeypaths[sqlServerDetailIndex], 'iNsTaNcEnAmE', instanceName) regValues = dbconnect_utils.getRegValues(localClient, wmiRegistryClient, sqlServerDetailKeypath, sqlServerDetailFilters[sqlServerDetailIndex]) if regValues == None or str(regValues) == '[[], []]' or str(regValues) == '{}': dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Got nothing for key <%s> with filter <%s>' % (sqlServerDetailKeypath, sqlServerDetailFilters[sqlServerDetailIndex])) sqlServerDetailValues.insert(sqlServerDetailIndex, None) else: sqlServerDetailValues.insert(sqlServerDetailIndex, regValues[sqlServerDetailKeypath]) dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Got value <%s> for key <%s> with filter <%s>' % (sqlServerDetailValues[sqlServerDetailIndex], sqlServerDetailKeypath, sqlServerDetailFilters[sqlServerDetailIndex])) logger.debug("instanceNameList %s " % instanceNameList) ## We should have all details for this instance now - add it to DB dictionary listenerPort = sqlServerDetailValues[0] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Got port <%s> for instance <%s>' % (listenerPort, instanceName)) installPath = sqlServerDetailValues[1] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Got path <%s> for instance <%s>' % (installPath, instanceName)) version = sqlServerDetailValues[2] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Got version <%s> for instance <%s>' % (version, instanceName)) ipAddress = dbconnect_utils.fixIP(sqlServerDetailValues[3], localClient.getIpAddress()) dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Got IP <%s> for instance <%s>' % (ipAddress, instanceName)) clusterName = sqlServerDetailValues[4] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Got Cluster Name <%s> for instance <%s>' % (clusterName, instanceName)) if clusterName: clusterIp = netutils.getHostAddress(clusterName) if clusterIp and netutils.isValidIp(clusterIp): ipAddress = clusterIp ## If the instance is already in the DB dict, don't overwrite all values if instanceName == 'MSSQLSERVER': dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Got unnamed SQL Server instance') instanceName = dbconnect_utils.getServerName(localClient) else: instanceName = dbconnect_utils.getServerName(localClient) + '\\' + instanceName.lower() installPath = installPath.lower() if instanceName in dbInstanceDict.keys(): statusFlag = (dbInstanceDict[instanceName])[dbconnect_utils.STATUS_INDEX] # If port is already populated, don't overwrite it because # port number information from active processes (above) is # guaranteed to be correct and the registry may not be up-to-date if (dbInstanceDict[instanceName])[dbconnect_utils.PORT_INDEX] != dbconnect_utils.UNKNOWN: if listenerPort not in activeListenerPorts: listenerPort = (dbInstanceDict[instanceName])[dbconnect_utils.PORT_INDEX] dbInstanceDict[instanceName] = ['MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, statusFlag] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Found known SQL Server <%s> instance <%s> listening at port <%s> on <%s> and installed in <%s>' % (version, instanceName, listenerPort, ipAddress, installPath)) else: dbInstanceDict[instanceName] = ['MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, dbconnect_utils.UNKNOWN] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Added SQL Server <%s> instance <%s> listening at port <%s> on <%s> and installed in <%s>' % (version, instanceName, listenerPort, ipAddress, installPath)) logger.debug("instanceNameList %s " % instanceNameList) logger.debug("dbInstanceDict %s" % dbInstanceDict) ## Replace dictionary entry of serverName\sqlInstanceName with clusterName\sqlInstanceName if clusterName and instanceName in dbInstanceDict.keys(): if instanceName.find('\\') > 0 : newInstanceName = clusterName + '\\' + instanceName[instanceName.find('\\')+1:] else: newInstanceName = clusterName dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':registryLookup] Replacing SQL Server instance <%s> with <%s> because it is part of a cluster' % (instanceName, newInstanceName)) dbInstanceDict[newInstanceName] = dbInstanceDict[instanceName] del dbInstanceDict[instanceName] logger.debug("dbInstanceDict %s" % dbInstanceDict) #print dbInstanceDict logger.debug("instanceNameList %s " % instanceNameList) except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':registryLookup] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnHPUX(localClient, USE_SUDO, USE_LSOF): try: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX]') procToPortDict = {} ## Get process OSHs ############################################ try: psOut = localClient.executeCmd('ps -ef') psLines = dbconnect_utils.splitCommandOutput(psOut.strip()) if psLines == None: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to get list of processes!' ) return None for psLine in psLines: ## Reg for processes with args res = re.search( '(\w+)\s+?(\d+).*\s\d+\:\d\d\s([0-9a-zA-Z_.\[\]\-+:/]+)\s(.*)', psLine) if (res): cleanArgs = res.group(4) else: ## Reg for processes with no args res = re.search( '(\w+)\s+?(\d+).*\s\d+\:\d\d\s([0-9a-zA-Z_.\-+:/]+)', psLine) if (res): cleanArgs = '' if (res): commandPath = res.group(3) pid = res.group(2) userName = res.group(1).strip() cleanCommand = '' if commandPath.find('/') == -1: cleanCommand = commandPath else: res2 = re.search('(.*/)([^/]+)', commandPath) if (res2): cleanCommand = res2.group(2) else: continue commandLine = cleanCommand + ' ' + cleanArgs dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Got PROCESS <%s:%s> with path <%s>, owner <%s>, and command line <%s>' % (pid, cleanCommand, commandPath, userName, commandLine) ) ## {PID:[cleanCommand, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine] if dbconnect_utils.populateProcToPortDict( procToPortDict, pid, cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine, userName) == 0: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s>, owner <%s>, and command line <%s> to the procToPort dictionary' % (pid, cleanCommand, 'Running', commandPath, userName, commandLine)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Unable to get list of processes: <%s>' % excInfo) return None ## Use LSOF to map each process to a port and create a dictionary ############################################ handleProcessToPortByLsof(USE_SUDO, localClient, procToPortDict) ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Returning EMPTY process to port dictionary' ) return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnHPUX] Exception: <%s>' % excInfo) pass
def processProcToPortDict(localClient, p2pDict, dbDict, database_ip_service_endpoints): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict]') for pid in p2pDict.keys(): processName = (p2pDict[pid])[dbconnect_utils.PROCESSNAME_INDEX].lower() listenerPort = (p2pDict[pid])[dbconnect_utils.PORT_INDEX] ipAddress = (p2pDict[pid])[dbconnect_utils.IP_INDEX] if ipAddress == dbconnect_utils.UNKNOWN: ipAddress = localClient.getIpAddress() path = (p2pDict[pid])[dbconnect_utils.PATH_INDEX].lower() commandLine = (p2pDict[pid])[dbconnect_utils.COMMANDLINE_INDEX] statusFlag = (p2pDict[pid])[dbconnect_utils.STATUS_INDEX] version = dbconnect_utils.UNKNOWN instanceNameFound = '' installPath = dbconnect_utils.UNKNOWN if re.search('mssqlserveradhelper', processName) or re.search('sql server analysis services', processName) or re.search('sql server agent', processName): ## Filters: If we don't skip these, the next checks will ## catch them and identify incorrect instances dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':processProcToPortDict] (1) Found process name <%s>. Ignoring...' % processName) continue ## Look for SQL Server instances using known process/service/software names elif re.search('sqlservr.exe', processName): #instanceNameFound = 'MicrosoftSQLServer' ## This is not a named instance # Get instance name from command line instanceNameFound = dbconnect_utils.getServerName(localClient) instanceNameRegexStr = re.search(r'\-s(\w+)$', commandLine) if instanceNameRegexStr: instanceNameFound = instanceNameFound + instanceNameRegexStr.group(1).strip().lower() if path != dbconnect_utils.UNKNOWN: binPath = path[:path.find('sqlservr.exe')] installPath = binPath[:len(binPath)-5] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] (2) Found MSSQL Server instance <%s> at port <%s> from process name <%s> and its path is <%s>' % (instanceNameFound, listenerPort, processName, path)) dbDict[instanceNameFound] = ['MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, statusFlag] if ipAddress and netutils.isValidIp(ipAddress) and listenerPort: ip_service_endpoints = database_ip_service_endpoints.get(instanceNameFound) or [] ip_service_endpoints.append("%s:%s" %(ipAddress, listenerPort)) database_ip_service_endpoints[instanceNameFound] = ip_service_endpoints elif re.search('mssql\$', processName): instanceNameRegexStr = re.search('mssql\$(\w+)', processName) ## Skip if an instance name cannot be identified if instanceNameRegexStr: instanceNameFound = instanceNameRegexStr.group(1).strip().lower() instanceNameFound = dbconnect_utils.getServerName(localClient) + '\\' + instanceNameFound else: continue if path != dbconnect_utils.UNKNOWN: binPath = path[:path.find('sqlservr.exe')] installPath = binPath[:len(binPath)-5] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] (3) Found MSSQL Server instance <%s> at port <%s> from process name <%s> and its path is <%s>' % (instanceNameFound, listenerPort, processName, path)) dbDict[instanceNameFound] = ['MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, statusFlag] if ipAddress and netutils.isValidIp(ipAddress) and listenerPort: ip_service_endpoints = database_ip_service_endpoints.get(instanceNameFound) or [] ip_service_endpoints.append("%s:%s" %(ipAddress, listenerPort)) database_ip_service_endpoints[instanceNameFound] = ip_service_endpoints elif re.search('sql server \(', processName): instanceNameRegexStr = re.search('sql server \((\w+)\)', processName) ## Skip if an instance name cannot be identified if instanceNameRegexStr: instanceNameFound = instanceNameRegexStr.group(1).strip().lower() else: continue ## Fix SQL 2K5 instancename to something UCMDB can understand if instanceNameFound == 'mssqlserver': #instanceNameFound = 'MicrosoftSQLServer' instanceNameFound = dbconnect_utils.getServerName(localClient) else: instanceNameFound = dbconnect_utils.getServerName(localClient) + '\\' + instanceNameFound ## Get path if path != dbconnect_utils.UNKNOWN: binPath = path[:path.find('sqlservr.exe')] installPath = binPath[:len(binPath)-5] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] (4) Found MSSQL Server instance <%s> at port <%s> from process name <%s> and its path is <%s>' % (instanceNameFound, listenerPort, processName, path)) dbDict[instanceNameFound] = ['MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, statusFlag] if ipAddress and netutils.isValidIp(ipAddress) and listenerPort: ip_service_endpoints = database_ip_service_endpoints.get(instanceNameFound) or [] ip_service_endpoints.append("%s:%s" %(ipAddress, listenerPort)) database_ip_service_endpoints[instanceNameFound] = ip_service_endpoints elif re.search('mssqlserver\^w', processName): #instanceNameFound = 'MicrosoftSQLServer' ## This is not a named instance instanceNameFound = dbconnect_utils.getServerName(localClient) if path != dbconnect_utils.UNKNOWN: binPath = path[:path.find('sqlservr.exe')] installPath = binPath[:len(binPath)-5] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] (5) Found MSSQL Server instance <%s> at port <%s> from process name <%s> and its path is <%s>' % (instanceNameFound, listenerPort, processName, path)) dbDict[instanceNameFound] = ['MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, statusFlag] if ipAddress and netutils.isValidIp(ipAddress) and listenerPort: ip_service_endpoints = database_ip_service_endpoints.get(instanceNameFound) or [] ip_service_endpoints.append("%s:%s" %(ipAddress, listenerPort)) database_ip_service_endpoints[instanceNameFound] = ip_service_endpoints elif re.search('mssqlserver', processName): #instanceNameFound = 'MicrosoftSQLServer' ## This is not a named instance instanceNameFound = dbconnect_utils.getServerName(localClient) if path != dbconnect_utils.UNKNOWN: binPath = path[:path.find('sqlservr.exe')] installPath = binPath[:len(binPath)-5] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':processProcToPortDict] (6) Found MSSQL Server instance <%s> at port <%s> from process name <%s> and its path is <%s>' % (instanceNameFound, listenerPort, processName, path)) dbDict[instanceNameFound] = ['MicrosoftSQLServer', listenerPort, ipAddress, installPath, version, statusFlag] if ipAddress and netutils.isValidIp(ipAddress) and listenerPort: ip_service_endpoints = database_ip_service_endpoints.get(instanceNameFound) or [] ip_service_endpoints.append("%s:%s" %(ipAddress, listenerPort)) database_ip_service_endpoints[instanceNameFound] = ip_service_endpoints except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':processProcToPortDict] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnAIX(localClient, USE_SUDO, USE_LSOF): try: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX]') procToPortDict = {} ## Get process OSHs ############################################ try: psOut = localClient.executeCmd("ps -e -o 'user,pid,time,args'") psLines = dbconnect_utils.splitCommandOutput(psOut.strip()) if psLines == None: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to get list of processes!' ) return None for psLine in psLines: if (re.search('TIME COMMAND', psLine)): continue ## Reg for processes with args res = re.search( '(\w+)\s+?(\d+).*:\d\d\s([0-9a-zA-Z_.\[\]\-+:/]+)\s(.*)', psLine) if (res): cleanArgs = res.group(4) else: ## Reg for processes with no args res = re.search( '(\w+)\s+?(\d+).*:\d\d\s([0-9a-zA-Z_.\[\]\-+:/]+)', psLine) if (res): cleanArgs = '' if (res): commandPath = res.group(3) pid = res.group(2) userName = res.group(1) cleanCommand = '' if commandPath.find('/') == -1: cleanCommand = commandPath else: res2 = re.search('(.*/)([^/]+)', commandPath) if (res2): cleanCommand = res2.group(2) else: continue commandLine = cleanCommand + ' ' + cleanArgs dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Got PROCESS <%s:%s> with path <%s>, owner <%s>, and command line <%s>' % (pid, cleanCommand, commandPath, userName, commandLine) ) if dbconnect_utils.populateProcToPortDict( procToPortDict, pid, cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine, userName) == 0: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s>, owner <%s>, and command line <%s> to the procToPort dictionary' % (pid, cleanCommand, 'Running', commandPath, userName, commandLine)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to get list of processes: <%s>' % excInfo) pass if USE_LSOF: ## Use LSOF to map each process to a port and create a dictionary ############################################ try: pidToPortMap = {} ## We need a local pid to port map lsofCmd = 'lsof -n -P -i | grep -i listen 2>/dev/null' if USE_SUDO: lsofCmd = 'sudo ' + lsofCmd lsofStr = localClient.executeCmd(lsofCmd) lsofLines = dbconnect_utils.splitCommandOutput(lsofStr.strip()) if lsofLines != None: for lsofLine in lsofLines: if len(lsofLine) < 1: continue lsofLine = lsofLine.strip() m = re.search( '\w+\s+(\d+)\s+\w+\s+\w+\s+IPv[4|6].+TCP\s+(\S+):(\d+)\s+\(\w+\)', lsofLine) if (m): pid = m.group(1).strip() ipAddress = m.group(2).strip() ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP( ipAddress, localClient.getIpAddress()) serverPort = m.group(3).strip() pidToPortMap[pid] = [ipAddress, serverPort] if pidToPortMap != None and len(pidToPortMap) > 0: for pid in pidToPortMap.keys(): if pid in procToPortDict.keys(): ipAddress = (pidToPortMap[pid])[0] ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue serverPort = (pidToPortMap[pid])[1] dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Found port <%s:%s> for pid <%s>' % (ipAddress, serverPort, pid)) (procToPortDict[pid] )[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid] )[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] No TCP port associated with PID [' + pID + ']: ' + lsofLine) else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to make a process to port map using LSOF: ' + lsofStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to make a process to port map using LSOF: <%s>' % excInfo) pass else: ## Try using netstat and KDB try: pidToPortMap = {} ## We need a local pid to port map netstatLisCmd = 'netstat -Aanf inet | grep "LISTEN"' if USE_SUDO: netstatLisCmd = 'sudo ' + netstatLisCmd netstatLisStr = localClient.executeCmd(netstatLisCmd) nsLisLines = dbconnect_utils.splitCommandOutput( netstatLisStr.strip()) if nsLisLines != None: for nsLine in nsLisLines: nsLine = nsLine.strip() # m = re.search('(\w+)\s+tcp\d?\s+\d+\s+\d+\s+(\*|\d+.\d+.\d+.\d+).(\d+)\s+(\*|\d+.\d+.\d+.\d+).(\*|\d+)\s+\S+', nsLine) m = re.search( '(\w+)\s+tcp\d?\s+\d+\s+\d+\s+(\*|\d+.\d+.\d+.\d+).(\d+)\s+(\*|\d+.\d+.\d+.\d+).(\*|\d+).*', nsLine) if (m): ipAddress = m.group(2).strip() ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP( ipAddress, localClient.getIpAddress()) serverPort = m.group(3).strip() pid = getAIXpIDfromAddress(localClient, m.group(1).strip(), USE_SUDO) if pid != None: pidToPortMap[pid] = [ipAddress, serverPort] if pidToPortMap != None and len(pidToPortMap) > 0: for pid in pidToPortMap.keys(): if pid in procToPortDict.keys(): ipAddress = (pidToPortMap[pid])[0] ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue serverPort = (pidToPortMap[pid])[1] dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Found port <%s:%s> for pid <%s>' % (ipAddress, serverPort, pid)) (procToPortDict[pid] )[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid] )[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to make a process to port map using netstat and kdb: <%s>' % netstatLisStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Unable to make a process to port map using netstat and kdb: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Returning EMPTY process to port dictionary' ) return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnAIX] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnSolaris(localClient, USE_SUDO, USE_LSOF): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris]') procToPortDict = {} ## Get process OSHs ############################################ try: psCmd = 'ps -e -o pid -o uid -o user -o time -o args' if USE_SUDO == 'true': psCmd = 'sudo ' + psCmd psOut = localClient.executeCmd(psCmd) psLines = dbconnect_utils.splitCommandOutput(psOut.strip()) if psLines == None: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Unable to get list of processes!') return None for line in psLines: line = line.strip() token=line.split(None,4) # Some checks to make sure line is valid if (len(token) != 5): continue if (not re.search('^\d+$',token[0])): continue if (len(token[4]) < 2): continue spaceIndex = token[4].find(' ') commandPath = '' cleanArgs = '' if spaceIndex > -1: commandPath = token[4][:spaceIndex] try: cleanArgs = token[4][spaceIndex+1:] except: cleanArgs = '' else: commandPath = token[4] cleanArgs = '' pid = token[0] userName = token[2] cleanCommand = '' cleanPath = '' if (commandPath.find('/') == -1) or (commandPath[0] == '['): cleanCommand = commandPath cleanPath = '' else: res2 = re.search('(.*/)([^/]+)', commandPath) if (res2): cleanPath = res2.group(1) cleanCommand = res2.group(2) else: continue commandLine = cleanCommand + ' ' + cleanArgs dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Got PROCESS <%s:%s> with path <%s>, owner <%s>, and command line <%s>' % (pid, cleanCommand, commandPath, userName, commandLine)) ## {PID:[cleanCommand, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine, userName) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s>, owner <%s>, and command line <%s> to the procToPort dictionary' % (pid, cleanCommand, 'Running', commandPath, userName, commandLine)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Unable to get list of processes: <%s>' % excInfo) return None ## Use PFILES to map each process to a port and create a dictionary ############################################ try: for pID in procToPortDict.keys(): pFilesCmd = 'pfiles ' + pID + ' 2>/dev/null | grep "sockname: AF_INET"' if USE_SUDO == 'true': pFilesCmd = 'sudo ' + pFilesCmd pFilesStr = localClient.executeCmd(pFilesCmd) if len(pFilesStr) <1: continue pFilesLines = dbconnect_utils.splitCommandOutput(pFilesStr.strip()) if pFilesLines == None: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Error: Invalid output from pfiles: ' + pFilesStr) continue for pFilesLine in pFilesLines: pFilesLine = pFilesLine.strip() m = re.search('.+AF_INET\s+(\d+\.\d+\.\d+\.\d+)\s+port:\s*(\d+)', pFilesLine) if re.search('AFINET6', pFilesLine): m = re.search('.+AF_INET6.*:(\d+\.\d+\.\d+\.\d+)\s+port:\s*(\d+)', pFilesLine) if (m) and m.group(2) != '0': ipAddress = m.group(1).strip() ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP(ipAddress, localClient.getIpAddress()) serverPort = m.group(2).strip() dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Adding port <%s:%s> for process <%s>' % (ipAddress, serverPort, (procToPortDict[pID])[dbconnect_utils.PROCESSNAME_INDEX])) (procToPortDict[pID])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pID])[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] No TCP port associated with PID [' + pID + ']: ' + pFilesLine) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Unable to make a process to port map using pfiles: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Returning EMPTY process to port dictionary') return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnSolaris(localClient, USE_SUDO, USE_LSOF): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris]') procToPortDict = {} ## Get process OSHs ############################################ try: psCmd = 'ps -e -o pid -o uid -o user -o time -o args' if USE_SUDO: psCmd = 'sudo ' + psCmd psOut = localClient.executeCmd(psCmd) psLines = dbconnect_utils.splitCommandOutput(psOut.strip()) if psLines == None: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Unable to get list of processes!') return None for line in psLines: line = line.strip() token=line.split(None,4) # Some checks to make sure line is valid if (len(token) != 5): continue if (not re.search('^\d+$',token[0])): continue if (len(token[4]) < 2): continue spaceIndex = token[4].find(' ') commandPath = '' cleanArgs = '' if spaceIndex > -1: commandPath = token[4][:spaceIndex] try: cleanArgs = token[4][spaceIndex+1:] except: cleanArgs = '' else: commandPath = token[4] cleanArgs = '' pid = token[0] userName = token[2] cleanCommand = '' cleanPath = '' if (commandPath.find('/') == -1) or (commandPath[0] == '['): cleanCommand = commandPath cleanPath = '' else: res2 = re.search('(.*/)([^/]+)', commandPath) if (res2): cleanPath = res2.group(1) cleanCommand = res2.group(2) else: continue commandLine = cleanCommand + ' ' + cleanArgs dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Got PROCESS <%s:%s> with path <%s>, owner <%s>, and command line <%s>' % (pid, cleanCommand, commandPath, userName, commandLine)) ## {PID:[cleanCommand, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine, userName) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s>, owner <%s>, and command line <%s> to the procToPort dictionary' % (pid, cleanCommand, 'Running', commandPath, userName, commandLine)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Unable to get list of processes: <%s>' % excInfo) return None allowPFiles = getGlobalSetting().getPropertyBooleanValue('allowPFilesOnSunOS', False) if USE_LSOF: handleProcessToPortByLsof(USE_SUDO, localClient, procToPortDict) elif allowPFiles: handleProcessToPortByPFile(USE_SUDO, localClient, procToPortDict)## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Returning EMPTY process to port dictionary') return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnSolaris(localClient, USE_SUDO, USE_LSOF): try: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris]') procToPortDict = {} ## Get process OSHs ############################################ try: psCmd = 'ps -e -o pid -o uid -o user -o time -o args' if USE_SUDO: psCmd = 'sudo ' + psCmd psOut = localClient.executeCmd(psCmd) psLines = dbconnect_utils.splitCommandOutput(psOut.strip()) if psLines == None: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Unable to get list of processes!' ) return None for line in psLines: line = line.strip() token = line.split(None, 4) # Some checks to make sure line is valid if (len(token) != 5): continue if (not re.search('^\d+$', token[0])): continue if (len(token[4]) < 2): continue spaceIndex = token[4].find(' ') commandPath = '' cleanArgs = '' if spaceIndex > -1: commandPath = token[4][:spaceIndex] try: cleanArgs = token[4][spaceIndex + 1:] except: cleanArgs = '' else: commandPath = token[4] cleanArgs = '' pid = token[0] userName = token[2] cleanCommand = '' cleanPath = '' if (commandPath.find('/') == -1) or (commandPath[0] == '['): cleanCommand = commandPath cleanPath = '' else: res2 = re.search('(.*/)([^/]+)', commandPath) if (res2): cleanPath = res2.group(1) cleanCommand = res2.group(2) else: continue commandLine = cleanCommand + ' ' + cleanArgs dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Got PROCESS <%s:%s> with path <%s>, owner <%s>, and command line <%s>' % (pid, cleanCommand, commandPath, userName, commandLine)) ## {PID:[cleanCommand, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine] if dbconnect_utils.populateProcToPortDict( procToPortDict, pid, cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine, userName) == 0: logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s>, owner <%s>, and command line <%s> to the procToPort dictionary' % (pid, cleanCommand, 'Running', commandPath, userName, commandLine)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug( '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Unable to get list of processes: <%s>' % excInfo) return None allowPFiles = getGlobalSetting().getPropertyBooleanValue( 'allowPFilesOnSunOS', False) if USE_LSOF: handleProcessToPortByLsof(USE_SUDO, localClient, procToPortDict) elif allowPFiles: handleProcessToPortByPFile( USE_SUDO, localClient, procToPortDict) ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Returning EMPTY process to port dictionary' ) return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnSolaris] Exception: <%s>' % excInfo) pass
def getAIXpIDfromAddress(localClient, procAddress, USE_SUDO): try: kdbOut = '' kdbCmd = '' pidLine = '' kdbOutLines = [] try: kdbCmd = 'echo "sockinfo ' + procAddress + ' tcpcb" | kdb | grep ACTIVE' if USE_SUDO: kdbCmd = 'sudo ' + kdbCmd kdbOut = localClient.executeCmd(kdbCmd) ## Output: pvproc+00E000 56*inetd ACTIVE 003808A 00360AC 0000000001244400 0 0001 except: excInfo = logger.prepareJythonStackTrace('') logger.warn( '[' + SCRIPT_NAME + ':getAIXpIDfromAddress] Error: Couldn\'t execute <%s>: <%s>' % (kdbCmd, excInfo)) return None ## if (kdbOut.find('do not allow') != -1): logger.debug( '[' + SCRIPT_NAME + ':getAIXpIDfromAddress] Couldn\'t get info from kdb. Please set suid on /usr/sbin/kdb or use root credentials.' ) return None ## If output contains multiple lines, split them kdbOutLines = dbconnect_utils.splitCommandOutput(kdbOut.strip()) if kdbOutLines == None: kdbOutLines = kdbOut.strip() dbconnect_utils.debugPrint( 5, '[' + SCRIPT_NAME + ':getAIXpIDfromAddress] kdbOutLines before extracting pidLine is <%s> (length=<%s>)' % (kdbOutLines, len(kdbOutLines))) ### We're only interested in the line with string "ACTIVE" in it if len(kdbOutLines) > 0: for kdbOutLine in kdbOutLines: if re.search('ACTIVE', kdbOutLine): pidLine = kdbOutLine.strip() else: dbconnect_utils.debugPrint( 3, '[' + SCRIPT_NAME + ':getAIXpIDfromAddress] Unusable KDB output for address <%s>' % procAddress) return None ## Extract process ID hex from output of kbd #m = re.match('\S+\+\w+\s+\d+\*\S+\s+\S+\s+(\w+)\s+\w+\s+\w+\s+\w+\s+\w+\s+\w+\s+.*', pidLine) dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getAIXpIDfromAddress] pidLine is <%s>' % pidLine) m = re.match('.*ACTIVE\s+(\w+)\s+.*', pidLine) if (m): #thePID = str(int(m.group(1), 16)) thePID = str(int(m.group(1), 16)) dbconnect_utils.debugPrint( 4, '[' + SCRIPT_NAME + ':getAIXpIDfromAddress] Found PID <%s> for address <%s>' % (thePID, procAddress)) return thePID else: dbconnect_utils.debugPrint( 2, '[' + SCRIPT_NAME + ':getAIXpIDfromAddress] Couldn\'t find PID for address [' + procAddress + ']') return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getAIXpIDfromAddress] Exception: <%s>' % excInfo) pass
def getProcToPortDictOnLinux(localClient, USE_SUDO, USE_LSOF): try: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux]') procToPortDict = {} ## Get process OSHs ############################################ try: psOut = localClient.executeCmd('ps -e -o pid -o uid -o user -o cputime -o command --cols 4000') if psOut == None: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Unable to get list of processes!') return None psLines = dbconnect_utils.splitCommandOutput(psOut.strip()) if psLines == None: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Unable to get list of processes!') return None for psLine in psLines: psLine = psLine.strip() token = psLine.split(None, 4) # Some checks to make sure line is valid if(len(token) != 5): continue if(not re.search('^\d+$',token[0])): continue if(len(token[4]) < 2): continue spaceIndex = token[4].find(' ') commandPath = '' cleanArgs = '' if spaceIndex > -1: commandPath = token[4][:spaceIndex] try: cleanArgs = token[4][spaceIndex+1:] except: cleanArgs = '' else: commandPath = token[4] cleanArgs = '' pid = token[0] userName = token[2] cleanCommand = '' if (commandPath.find('/') == -1) or (commandPath[0] == '['): cleanCommand = commandPath else: res2 = re.search('(.*/)([^/]+)',commandPath) if (res2): cleanCommand = res2.group(2) else: continue commandLine = cleanCommand + ' ' + cleanArgs dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Got PROCESS <%s:%s> with path <%s>, owner <%s>, and command line <%s>' % (pid, cleanCommand, commandPath, userName, commandLine)) ## {PID:[cleanCommand, listeningPort, ipAddress, path, version, status, processCommandline]} # procToPortDict[pid] = [cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine] if dbconnect_utils.populateProcToPortDict(procToPortDict, pid, cleanCommand, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN, commandPath, dbconnect_utils.UNKNOWN, 'Running', commandLine, userName) == 0: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnWindows] Unable to add PROCESS <%s:%s> (%s) with path <%s>, owner <%s>, and command line <%s> to the procToPort dictionary' % (pid, cleanCommand, 'Running', commandPath, userName, commandLine)) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Unable to get list of processes: <%s>' % excInfo) pass ## Use NETSTAT output to create an array of server ports ## and map them to server processes ############################################ try: netstatLisCmd = 'netstat -anp | grep "LISTEN"' if USE_SUDO == 'true': netstatLisCmd = 'sudo ' + netstatLisCmd netstatLisStr = localClient.executeCmd(netstatLisCmd) nsLisLines = dbconnect_utils.splitCommandOutput(netstatLisStr.strip()) if nsLisLines != None: for nsLine in nsLisLines: nsLine = nsLine.strip() dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Got nsLine <%s>' % nsLine) m = re.search('tcp.* (\S+):(\d+).*:.*\s+(\d+|-).*', nsLine) if (m): ipAddress = m.group(1).strip() ## Skip loopback IPs if re.search('127.0.0', ipAddress): continue ## Set the IP address to that of the destination if it is "*", "::", or "0.0.0.0" ipAddress = dbconnect_utils.fixIP(ipAddress, localClient.getIpAddress()) serverPort = m.group(2).strip() pid = m.group(3).strip() dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Got port <%s> for pid <%s>' % (serverPort, pid)) if pid != '-' and procToPortDict.has_key(pid): dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Adding port <%s:%s> for process <%s>' % (ipAddress, serverPort, (procToPortDict[pid])[dbconnect_utils.PROCESSNAME_INDEX])) (procToPortDict[pid])[dbconnect_utils.IP_INDEX] = ipAddress (procToPortDict[pid])[dbconnect_utils.PORT_INDEX] = serverPort else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Couldn\'t get netstat information (Most likely due to lack of user permissions): ' + nsLine) else: logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Invalid output from netstat: <%s>' % netstatLisStr) except: excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Unable to make a process to port map using netstat: <%s>' % excInfo) pass ## Should have proc to port map if len(procToPortDict) > 0: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Returning process to port dictionary with <%s> items' % len(procToPortDict)) return procToPortDict else: dbconnect_utils.debugPrint(2, '[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Returning EMPTY process to port dictionary') return None except: #excInfo = str(sys.exc_info()[1]) excInfo = logger.prepareJythonStackTrace('') logger.debug('[' + SCRIPT_NAME + ':getProcToPortDictOnLinux] Exception: <%s>' % excInfo) pass
def parseEtcFiles(localClient, p2pDict, dbDict, isWindows): try: pathsFound = [] ## Windows doesn't have /etc/oratab or /etc/oraInst.loc if isWindows == 'true': return else: ## Process oratab if found oratabLocation = dbconnect_utils.findFile(localClient, 'oratab', '/etc/', isWindows) if oratabLocation == None: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiles] oratab not found in /etc/') else: oratabContent = dbconnect_utils.getFileContent(localClient, oratabLocation[0], isWindows) if oratabContent: oratabLines = dbconnect_utils.splitCommandOutput(oratabContent) if oratabLines and len(oratabLines) > 0: for oratabLine in oratabLines: ## Ignore comment line or lines with nothing if len(oratabLine.strip()) < 1 or oratabLine.strip()[0] == '#': continue if oratabLine.strip().lower().endswith(":n"): #we do not want to process potentially non running instances continue dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [1] Processing oratab line <%s>' % oratabLine) oratabLineSplit = oratabLine.strip().split(':') ## Ignore lines if the format is not sid:path:startup if len(oratabLineSplit) < 3: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [1] Ignoring oratab line <%s>' % oratabLine) continue ## We should have an instance and its path sidFound = oratabLineSplit[0].strip().lower() pathFound = oratabLineSplit[1].strip().lower() ipAddress = localClient.getIpAddress() ## If the SID is "*", ignore it and use the path if sidFound == "*": dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [1] Ignoring oracle SID <%s>' % sidFound) if pathFound not in pathsFound: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [1] Adding path <%s> to return array' % pathFound) pathsFound.append(pathFound) else: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [1] Found known path <%s>' % pathFound) continue ## If this SID already exists in the dbDict, overwrite the install path ## associated with it. If not, add and entry and path if sidFound in dbDict.keys(): (dbDict[sidFound])[dbconnect_utils.PATH_INDEX] = pathFound dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiiles] [1] Found known Oracle instance <%s> with path <%s> on <%s>' % (sidFound, pathFound, ipAddress)) else: dbDict[sidFound] = ['oracle', dbconnect_utils.UNKNOWN, ipAddress, pathFound, dbconnect_utils.UNKNOWN, dbconnect_utils.UNKNOWN] dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiles] [1] Added Oracle instance <%s> with path <%s> on <%s>' % (sidFound, pathFound, ipAddress)) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiles] [1] Invalid entries /etc/oratab: <%s>!' % oratabContent) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiles] [1] Empty or invalid /etc/oratab!') ## Process oraInst.loc if found oraInstLocation = dbconnect_utils.findFile(localClient, 'oraInst.loc', '/etc/', isWindows) if oraInstLocation == None: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiles] oraInst.loc not found in /etc/') else: oraInstContent = dbconnect_utils.getFileContent(localClient, oraInstLocation[0], isWindows) if oraInstContent: oraInstLines = dbconnect_utils.splitCommandOutput(oraInstContent) if oraInstLines and len(oraInstLines) > 0: for oraInstLine in oraInstLines: ## Ignore comment line or lines with nothing if len(oraInstLine.strip()) < 1 or oraInstLine.strip()[0] == '#': continue dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [2] Processing oraInst line <%s>' % oraInstLine) oraInstLineSplit = oraInstLine.strip().split('=') ## Ignore lines if the format is not inventory_loc=<path> if len(oraInstLineSplit) < 2 or oraInstLineSplit[0].strip() != 'inventory_loc': dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [2] Ignoring oraInst line <%s>' % oraInstLine) continue ## We should have an install path pathFound = oraInstLineSplit[1].strip().lower() dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiles] [2] Found oracle installation path <%s>' % pathFound) if pathFound not in pathsFound: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [2] Adding path <%s> to return array' % pathFound) pathsFound.append(pathFound) else: dbconnect_utils.debugPrint(4, '[' + SCRIPT_NAME + ':parseEtcFiles] [2] Found known path <%s>' % pathFound) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiles] [2] Invalid entries /etc/oraInst.loc: <%s>' % oraInstContent) else: dbconnect_utils.debugPrint(3, '[' + SCRIPT_NAME + ':parseEtcFiles] [2] Empty or invalid /etc/oraInst.loc!') return pathsFound except: excInfo = logger.prepareJythonStackTrace('') dbconnect_utils.debugPrint('[' + SCRIPT_NAME + ':parseEtcFiles] Exception: <%s>' % excInfo) pass