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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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