# Author: Donghua # Purpose: Filter key messages in Oracle alert log in UNIX tail fusion import io import datetime import time import traceback DayList=['Sun','Mon','Tue','Wed','Thu','Fri','Sat'] KeyWordList=['ORA-','Error','Starting ORACLE instance','Shutting down instance'] SkipOldEventMinutes=5AlertLogFile=r'D:\oracle\diag\rdbms\orcl\orcl\trace\alert_orcl.log' SkipOldEventDateTimeDelta=datetime.timedelta(minutes=SkipOldEventMinutes) EventDate=datetime.datetime(1, 1, 1, 0, 0) try: with io.open(AlertLogFile,mode='r') as f: while True: line=f.readline() # print ('[Debug] %s' % line.rstrip('\n')) if len(line) > 3 and line[0:3] in DayList: EventDate=datetime.datetime.strptime(line.rstrip('\n'), '%a %b %d %H:%M:%S %Y') if EventDate < datetime.datetime.now()-SkipOldEventDateTimeDelta : continue elif len(line) > 3: if EventDate < datetime.datetime.now()-SkipOldEventDateTimeDelta : continue for w in KeyWordList: if w in line: print('[%s] %s' % (EventDate, line.rstrip('\n'))) elif len(line) == 0: time.sleep(0.5) else: continueexcept: print(traceback.format_exc())
Sunday, February 28, 2016
Use Python to filter key messages in Oracle alert log in UNIX tail fusion
Use Python to format Oracle alert log based on keywords and time range
# Author: Donghua# Purpose: Filter key messages in Oracle alert log for last 48 hoursimport io import datetime import traceback DayList=['Sun','Mon','Tue','Wed','Thu','Fri','Sat'] KeyWordList=['ORA-','Error','Starting ORACLE instance','Shutting down instance'] OutputList=[] SkipOldEventHours=48AlertLogFile=r'D:\oracle\diag\rdbms\orcl\orcl\trace\alert_orcl.log' SkipOldEventDateTimeDelta=datetime.timedelta(hours=SkipOldEventHours) EventDate=datetime.datetime(1, 1, 1, 0, 0) try: with io.open(AlertLogFile,mode='r') as f: for line in f: if len(line)>3 and line[0:3] in DayList: # OutputList.append(line) EventDate=datetime.datetime.strptime(line.rstrip('\n'), '%a %b %d %H:%M:%S %Y') if EventDate < datetime.datetime.now()-SkipOldEventDateTimeDelta : continue elif len(line)>3: if EventDate < datetime.datetime.now()-SkipOldEventDateTimeDelta : continue for w in KeyWordList: if w in line: OutputList.append([EventDate,line.rstrip('\n')]) else: continueexcept: print(traceback.format_exc()) for o in OutputList: # use o[0].strftime('%a %b %d %H:%M:%S %Y') to get original Oracle style Format print('[%s] %s' % (o[0], o[1]))
Saturday, February 27, 2016
Python’s winreg: Editing the Windows Registry
# Author: Donghua# Purpose: Demonstration import sys import winreg import traceback RegPath='SOFTWARE\Python\DemoKey'NewKey='Key1'NewValueName='Val1'NewStringValue='DemoValue' def QueryKeyValue(): # Query the default value for KEY1 try: with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, RegPath) as key: ExistValue=winreg.QueryValue(key,NewKey) print('New Key (Default) value:',ExistValue) except FileNotFoundError: print(traceback.format_exc()) # Set the string value (Val1) under KEY1 try: with winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE, RegPath,0,access=winreg.KEY_READ) as key: ExistValue=winreg.QueryValueEx(key,NewValueName) print('New String value:',ExistValue[0]) except FileNotFoundError: print(traceback.format_exc()) return def SetKeyValue(): # set the default value for KEY1 try: with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, RegPath) as key: winreg.SetValue(key,NewKey,winreg.REG_SZ,'Default Key Value Demo') except FileNotFoundError: try: key=winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, RegPath) winreg.SetValue(key,NewKey,winreg.REG_SZ,'Default Value Demo') except: print(traceback.format_exc()) except: print(traceback.format_exc()) # Set the string key/value pair under KEY1 try: with winreg.OpenKeyEx(winreg.HKEY_LOCAL_MACHINE, RegPath,reserved=0,access=winreg.KEY_WRITE) as key: winreg.SetValueEx(key,NewValueName,0,winreg.REG_SZ, NewStringValue) except: try: key=winreg.CreateKeyEx(winreg.HKEY_LOCAL_MACHINE, RegPath,reserved=0,access=winreg.KEY_WRITE) winreg.SetValueEx(key,NewValueName,0,winreg.REG_SZ, NewStringValue) except: print(traceback.format_exc()) return def DelKeyValue(): try: with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, RegPath, reserved=0,access=winreg.KEY_WRITE) as key: # Delete Value Val1 under Key1 try: winreg.DeleteValue(key,NewValueName) print('Value deleted:', NewValueName) except: print(traceback.format_exc()) # Delete Key Key1 try: winreg.DeleteKey(key,NewKey) print('Key deleted:', NewKey) except: print(traceback.format_exc()) except FileNotFoundError: print(traceback.format_exc()) return if __name__ == "__main__": SetKeyValue() QueryKeyValue() DelKeyValue()
Subscribe to:
Posts (Atom)