def get_dbm_agent_pid(): pids = [] processes = PSEFEGREP + '\"dbm_agent.py\"' + ' | ' + dlpxqa.EGREP + '-v egrep' process_count = processes + ' | ' + WCL if DEBUG: print 'get_dbm_agent_pid(): processes: ', processes print 'get_dbm_agent_pid(): process_count: ', process_count output = dlpxqa.exec_cmd(process_count) if DEBUG: print 'get_dbm_agent_pid(): count: ', output if int(output) != 0: lines = dlpxqa.exec_cmd(processes).split('\n') if DEBUG: print 'get_dbm_agent_pid(): processes: ', lines i = 0 while lines[i] != '': pid = lines[i].split()[1] pids.append(pid) i = i + 1 return pids
def is_process_running(pid): process_count = PSEFEGREP + '"' + pid + '"' + " | " + dlpxqa.EGREP + "-v egrep | " + WCL if DEBUG: print "is_process_running(): process_count: ", process_count output = dlpxqa.exec_cmd(process_count) if int(output) != 0: return True else: return False
def exposed_get_archive_dest_usage(self, database): """ Returns database's archive log destination's usage. :param database: database :type database: str :rtype: str """ dest_type = "" dest_usage = "" try: archive_dest = self.exposed_get_archive_dest(database) except Exception: raise if re.search(r"\+\S+", archive_dest): # archive destination is an ASM diskgroup dest_type = "ASM" # remove prefix '+' diskgroup_name = re.sub(r"\+", "", archive_dest) # get SID for ASM instance asm_sid = dlpxqa.get_dsource_asm_sid(database) if DEBUG: print "exposed_get_archive_dest_usage():\n", asm_sid try: dest_usage = self._run_sql( "select name,total_mb,free_mb from v$asm_diskgroup where name='%s';" % diskgroup_name, "sysdba", database, asm_sid, ) except Exception: raise if DEBUG: print "exposed_get_archive_dest_usage():\n", dest_usage elif re.search(r"\/\S+", archive_dest): # archive destination is on filesystem dest_type = "Filesystem" cmd = dlpxqa.DF + "-h " + archive_dest dest_usage = dlpxqa.exec_cmd(cmd) if DEBUG: print "exposed_get_archive_dest_usage():\n", dest_usage else: sys.exit("ERROR: un-recognized archive destination.") return dest_type, dest_usage
def _run_sql(self, query, role, sid, asm_sid=""): """ Runs SQL statement as specified role against specified SID. :param query: SQL statement :type query: str :param role: role :type role: str :param sid: SID :type sid: str :param asm_sid: SID of ASM instance :type asm_sid: str """ if asm_sid == "": # if an SID for database instance is specified os.putenv("ORACLE_SID", sid) os.putenv("ORACLE_HOME", dlpxqa.get_db_home_by_sid(sid)) SQLPLUS = os.path.join(dlpxqa.get_db_home_by_sid(sid), "bin", "sqlplus") else: # if an SID for ASM instance is specified # ORACLE_SID should set to ASM SID os.putenv("ORACLE_SID", asm_sid) cv = dlpxqa.get_crs_version() if DEBUG: print "_run_sql(): CRS version: ", cv if cv != "" and cv in ORACLE_11gR2: # for 11gR2, ASM home is GI home os.putenv("ORACLE_HOME", dlpxqa.get_crs_home()) SQLPLUS = os.path.join(dlpxqa.get_crs_home(), "bin", "sqlplus") elif cv != "" and not cv in ORACLE_11gR2: # for 10gR2 and 11gR1, ASM home is Oracle home os.putenv("ORACLE_HOME", dlpxqa.get_db_home_by_sid(sid)) SQLPLUS = os.path.join(dlpxqa.get_db_home_by_sid(sid), "bin", "sqlplus") else: os.putenv("ORACLE_HOME", dlpxqa.get_db_home_by_sid(sid)) SQLPLUS = os.path.join(dlpxqa.get_db_home_by_sid(sid), "bin", "sqlplus") if DEBUG: print "_run_sql(): SQLPLUS: ", SQLPLUS print "_run_sql(): query: ", query print "_run_sql(): SID: ", sid print "_run_sql(): ASM SID: ", asm_sid QUERY_HEADER = "set linesize 9000\nset pagesize 9999\nset newpage none\nset feedback off\nset verify off\nset echo off\nset heading off\n" QUERY_END = "\nquit;" # try to catch OSError and propagate it to to higher handler try: temp_dir = tempfile.mkdtemp() except OSError: raise # add read and execute permissions for others mode = os.stat(temp_dir).st_mode os.chmod(temp_dir, mode | stat.S_IROTH | stat.S_IXOTH) sql_filename = os.path.join(temp_dir, "%s.sql" % os.getpid()) sql_file = open(sql_filename, "w+b") try: sql_file.write(QUERY_HEADER) sql_file.write(query) sql_file.write(QUERY_END) finally: sql_file.close() os_login = dlpxqa.get_database_os_login(sid) os_user = re.search(r"^(\S+)\/", os_login).group(1) if DEBUG: print "_run_sql(): os_user: "******"' + "/ as " + role + '"' + " @" + sql_file.name sqlplus_cmd = dlpxqa.SU + os_user + " -c '" + sqlplus_cmd + "'" if DEBUG: print "_run_sql(): sqlplus_cmd: ", sqlplus_cmd output = dlpxqa.exec_cmd(sqlplus_cmd) shutil.rmtree(temp_dir) return output
else: UNINST = False # get current path current_dir = os.path.dirname(os.path.realpath(__file__)) if DEBUG: print 'Current directory is: %s' % current_dir script_path = os.path.join(current_dir, 'dbm_agent.py') # crontab entry for dbm_agent.py dbm_agent_job = '*/5 * * * * /usr/bin/python ' + script_path + ' > /dev/null 2&>1' # get current contents of crontab cmd = dlpxqa.CRONTAB + '-l' output = dlpxqa.exec_cmd(cmd) jobs = output.split('\n') print 'Current contents of crontab are:\n%s' % output if DEBUG: print type(output), '\n', output, '\n', output.split('\n') if not UNINST: print 'Following crontab job will be installed:\n%s' % dbm_agent_job else: print 'Following crontab job will be uninstalled:\n%s' % dbm_agent_job # create a temporary directory and file for storing new crontab temp_dir = tempfile.mkdtemp() filename = os.path.join(temp_dir, '%s.txt' % os.getpid())