Sunday, February 28, 2016

Use Python to filter key messages in Oracle alert log in UNIX tail fusion


# 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())