背景如上一文档
本文主要是监控databus同步数据的延迟问题,和其运行端口的监控。
引用模块代码如下:
import cx_Oracle
import re
import requests
import json
由于我们调用企业微信发信息这块代码先忽略,如果不涉及信息提醒可以直接用print()打出如下:
def msg(text):
--modify by yourself
print(text)
根据记录的最大txn和client记录的txn比对超过500个事务报警:
def select_scn(scn):
conn = cx_Oracle.connect('user','pass','ip:prot/instance')
cursor = conn.cursor()
sqlmaxtxn = 'select max(txn) from SY$TXLOG'
cursor.execute(sqlmaxtxn)
maxval = cursor.fetchone()
if maxval is None:
#SY$TXLOG no data
print('Warning:TXLOG no data!')
return
#according max txn get sequence
sqlscn = 'select txn,to_char(scn) from SY$TXLOG where scn={}'.format(scn)
ayscid = cursor.execute(sqlscn)
curval = cursor.fetchone()
#behind 500 transaction alert error
if maxval[0] - curval[0] > 500:
msg('Behind transaction > 500+',maxval[0],' ',curval[0])
else:
print(maxval[0],' ',curval[0])
cursor.close()
conn.close()
读取client的日志文件 得到txn
def getScnFromFile(file_path):
with open(file_path,'rb') as f:
f.seek(-5000,2)
#read last 500 byte
contentlf = f.read()
rseq = re.findall(b'Sequence=\d+',contentlf)
if len(rseq) < 0:
#may be get error
print(contentlf)
return contentlf
else:
sequence = rseq[len(rseq) -1] #get last one
#split sequence [b'Sequence=11508772259']
strseq = bytes.decode(sequence)
_,intseq = strseq.split('=')
#intseq is current sequence
return intseq
测试端口是否正常:
def checkport():
reurl = 'http://localhost:11116/sources'
res = requests.get(reurl)
if 'Failed' in res:
msg('Error: Service port not start!')
clurl = 'http://localhost:11116/relayStats/outbound/http/clients'
res = requests.get(clurl)
if 'Failed' in res:
msg('Error: Service port not start!')
主函数:
if __name__ == '__main__':
checkport()
res = getScnFromFile('/databus-master/client/logs/client.log')
print(res)
if len(res)>300:
#error can't get sequce
print(res)
msg(res)
else:
select_scn(res)
9066



被折叠的 条评论
为什么被折叠?



