最近在学习python,随手写了一个实时监控alert日志的小程序:
import time
ol = []
nl = []
lweek = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
fname = '/home/db/oracle/admin/sms/bdump/alert_sms1.log'
while True:
ol = nl[:]
nl = []
v = ''
k = ''
l = []
f = open(fname)
for line in f:
tl = line.split()
#判断是否是时间戳,如果是,将前一个时间戳和当前时间戳之间的有警告信息的内容放到nl中
if ( tl[0] in lweek and int(tl[2]) in range(1,32) and tl[4] == 'CST'):
#根据告警关键词过滤,可以自己根据需要添加
if "ORA-" in v or "Errors" in v or "Deadlock" in v:
s = k + "|" + v
nl.append(s)
v = ''
k = line
#如果不是时间戳,将内容叠加到变量v中
else:
v = v + '|' + line
f.close()
if "ORA-" in v or "Errors" in v or "Deadlock" in v:
s = k + "|" + v
nl.append(s)
#取前后两次告警信息的差集并输出
l = list(set(nl).difference(set(ol)))
if l is not None:
for x in l:
print x
time.sleep(10)
测试:
oracle@bjmbsdb01[/home/db/oracle/admin/sms/bdump]python mon_alert.py
Tue Mar 17 14:15:28 CST 2015
||ORA-1119 signalled during: alter tablespace users add datafile 'a'...
Tue Mar 17 14:16:21 CST 2015
||ORA-1119 signalled during: alter tablespace users add datafile 'a'...
Tue Mar 17 14:15:53 CST 2015
||Global Enqueue Services Deadlock detected. More info in file
| /home/db/oracle/admin/sms/bdump/sms1_lmd0_6620.trc.
Tue Mar 17 14:20:00 CST 2015
||alter tablespace users add datafile 'a'
|ORA-1119 signalled during: alter tablespace users add datafile 'a'...
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10972173/viewspace-1463041/
,如需转载,请注明出处,否则将追究法律责任。