def doReadFile(shellUtils, fileName, OSHVResult, tnsFile): tns_entries = dbutils.parseTNSNames(tnsFile, '', shellUtils) # parseTnsEntries(fileName, shellUtils, tns_entries, OSHVResult) if (len(tns_entries)==0): logger.info('no entries returns from ', fileName, '. Please verify if the file exists and it is valid TNS file.') return logger.debug('Found ', len(tns_entries), ' entries in tnsnames.ora file.') oracleList = [] for tns_entry in tns_entries: try: db_type = 'oracle' connectString = tns_entry[0] host_dns= tns_entry[1] db_port = tns_entry[2] db_sid = tns_entry[3].upper() host_ip = tns_entry[5] if (netutils.isValidIp(host_ip)): hashName = host_ip + db_sid if ((hashName in oracleList) == 0) : oracleList.append(hashName) hostOSH = modeling.createHostOSH(host_ip) oracleOSH = modeling.createDatabaseOSH(db_type, db_sid, db_port, host_ip, hostOSH) oracleOSH.setAttribute('database_dbconnectstring', connectString) oracleOSH.setContainer(hostOSH) OSHVResult.add(hostOSH) OSHVResult.add(oracleOSH) else: logger.warn("Can not resolve the IP from the TNS entry's host name (", host_dns, ") - TNS entry skipped.") except: logger.debugException('Unexpected TNS Parsing Exception:')
def discoverOracleDB(oracleServerName, protocol, ip_address, shellUtils, appServerOSH, Framework, OSHVResult): if not oracleServerName: return oraHomePaths = None oraHomePathsParam = Framework.getParameter('oracle_name') if oraHomePathsParam: logger.debug('Provided ora_home parameter with value:', oraHomePathsParam) oraHomePaths = oraHomePathsParam.split(',') if oraHomePaths is None: oraHome = getOracleHome(protocol, ip_address, shellUtils, Framework) if oraHome != '': oraHomePaths = [oraHome] if oraHomePaths != None: for oraHome in oraHomePaths: logger.debug('trying oracle home ', oraHome) # get the contents of the tnsnames.ora file tns_buffer = '' try: tns_buffer = getOracleTNSNames(oraHome, protocol, shellUtils) except: logger.debug( 'Failed to load content of tnsnames.ora from oracle_home:', oraHome) continue if tns_buffer.upper().find(oracleServerName.upper()) == -1: continue db_domain = getOracleDefaultDomain(oraHome, protocol, shellUtils) logger.debug('db_domain', db_domain) # parse tns entries and upload DB clients and servers tns_entries = dbutils.parseTNSNames(tns_buffer, db_domain) for tns_entry in tns_entries: tnsname = tns_entry[0] logger.debug('checking tnsname:', tnsname) # upload only siebel related DBs if re.search(re.escape(oracleServerName), tnsname, re.I): logger.debug('building objects for tnsname:', tnsname, 'for appserverId:', Framework.getDestinationAttribute('id')) buildTNSObjects(tns_entry, db_domain, appServerOSH, OSHVResult)
def doReadFile(shellUtils, fileName, OSHVResult, tnsFile): tns_entries = dbutils.parseTNSNames(tnsFile, "", shellUtils) # parseTnsEntries(fileName, shellUtils, tns_entries, OSHVResult) if len(tns_entries) == 0: logger.info( "no entries returns from ", fileName, ". Please verify if the file exists and it is valid TNS file." ) return logger.debug("Found ", len(tns_entries), " entries in tnsnames.ora file.") oracleList = [] for tns_entry in tns_entries: try: db_type = "oracle" connectString = tns_entry[0] host_dns = tns_entry[1] db_port = tns_entry[2] db_sid = tns_entry[3].upper() host_ip = tns_entry[5] if netutils.isValidIp(host_ip): hashName = host_ip + db_sid if (hashName in oracleList) == 0: oracleList.append(hashName) hostOSH = modeling.createHostOSH(host_ip) oracleOSH = modeling.createDatabaseOSH(db_type, db_sid, db_port, host_ip, hostOSH) oracleOSH.setAttribute("database_dbconnectstring", connectString) oracleOSH.setContainer(hostOSH) OSHVResult.add(hostOSH) OSHVResult.add(oracleOSH) else: logger.warn( "Can not resolve the IP from the TNS entry's host name (", host_dns, ") - TNS entry skipped." ) except: logger.debugException("Unexpected TNS Parsing Exception:")
def discoverOracleDB(oracleServerName, protocol, ip_address, shellUtils, appServerOSH, Framework, OSHVResult): if not oracleServerName: return oraHomePaths = None oraHomePathsParam = Framework.getParameter('oracle_name') if oraHomePathsParam: logger.debug('Provided ora_home parameter with value:', oraHomePathsParam) oraHomePaths = oraHomePathsParam.split(',') if oraHomePaths is None: oraHome = getOracleHome(protocol, ip_address, shellUtils, Framework) if oraHome != '': oraHomePaths = [oraHome] if oraHomePaths != None: for oraHome in oraHomePaths: logger.debug('trying oracle home ', oraHome) # get the contents of the tnsnames.ora file tns_buffer = '' try: tns_buffer = getOracleTNSNames(oraHome, protocol, shellUtils) except: logger.debug('Failed to load content of tnsnames.ora from oracle_home:', oraHome) continue if tns_buffer.upper().find(oracleServerName.upper()) == -1: continue db_domain = getOracleDefaultDomain(oraHome, protocol, shellUtils) logger.debug('db_domain', db_domain) # parse tns entries and upload DB clients and servers tns_entries = dbutils.parseTNSNames(tns_buffer, db_domain) for tns_entry in tns_entries: tnsname = tns_entry[0] logger.debug('checking tnsname:', tnsname) # upload only siebel related DBs if re.search(re.escape(oracleServerName), tnsname, re.I): logger.debug('building objects for tnsname:', tnsname, 'for appserverId:', Framework.getDestinationAttribute('id')) buildTNSObjects(tns_entry, db_domain, appServerOSH, OSHVResult)
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 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