自动重命名alert日志,并清理trc文件
#!/usr/bin/python import linecache import os import time # def log def rlog(log): LTIME=time.strftime('%Y-%m-%d %H:%M:%S') f=open('/tmp/poracle.log','a') f.write("\033[32;1m"+LTIME+' '+log+"\n\033[0m") f.close() def wlog(log): LTIME=time.strftime('%Y-%m-%d %H:%M:%S') f=open('/tmp/poracle.log','a') f.write("\033[31;1m"+LTIME+' '+log+"\n\033[0m") f.close() #get DUMP_DIR parameter f=open('/tmp/tmp.sql','w') f.write("spool /tmp/output.txt\n") f.write("select value from v$parameter where name='background_dump_dest';\n") f.write("spool off\n") f.write("exit") f.close() try: os.system("sqlplus / as sysdba @/tmp/tmp.sql") except: pass DUMP_DIR=str.strip(linecache.getline(r'/tmp/output.txt',4)) os.remove('/tmp/tmp.sql') os.remove('/tmp/output.txt') #create trace_dir directory BASE_DIR=DUMP_DIR+'/'+time.strftime('%Y') print BASE_DIR TRACE_DIR=BASE_DIR+'/trace'+time.strftime('%m') ISEXISTS=os.path.exists(BASE_DIR) if not ISEXISTS: os.mkdir(BASE_DIR) rlog("create "+BASE_DIR+" success!") else: pass ISEXISTS=os.path.exists(TRACE_DIR) if not ISEXISTS: os.mkdir(TRACE_DIR) rlog("create "+TRACE_DIR+" success!") else: pass #move trace tmpfile to trace_dir try: CMD=os.system("mv %s/*.trc %s" %(DUMP_DIR,TRACE_DIR)) except: pass if CMD==0: rlog(" Move"+DUMP_DIR+"tempfile trc to "+TRACE_DIR+" success!\n") else: wlog(" Move"+DUMP_DIR+"tempfile trc to "+TRACE_DIR+" faild!\n") try: CMD=os.system("mv %s/*.trm %s" %(DUMP_DIR,TRACE_DIR)) except: pass if CMD==0: rlog(" Move"+DUMP_DIR+"tempfile trm to "+TRACE_DIR+" success!\n") else: wlog(" Move"+DUMP_DIR+"tempfile trm to "+TRACE_DIR+" faild!\n") #get instance_name f=open('/tmp/instance.sql','w') f.write("spool /tmp/output_instance.txt\n") f.write("select instance_name from v$instance;\n") f.write("spool off\n") f.write("exit") f.close() try: os.system("sqlplus / as sysdba @/tmp/instance.sql") except: pass INSTANCE_NAME=str.strip(linecache.getline(r'/tmp/output_instance.txt',4)) print INSTANCE_NAME ALERT_NAME="alert_"+INSTANCE_NAME+'.log' print ALERT_NAME ALERT_TIME=time.strftime('%Y%m%d%H%M') os.remove('/tmp/instance.sql') os.remove('/tmp/output_instance.txt') #rename alert log try: CMD=os.system("mv %s/%s %s/%s.%s" %(DUMP_DIR,ALERT_NAME,DUMP_DIR,ALERT_NAME,ALERT_TIME)) except: pass if CMD==0: rlog(" Rename"+ALERT_NAME+" to "+ALERT_NAME+ALERT_TIME+" success!\n") else: wlog(" Rename"+ALERT_NAME+" to "+ALERT_NAME+ALERT_TIME+" faild!\n")
坚持,专注