设为首页 收藏本站
查看: 593|回复: 0

[经验分享] Python 实现单笔业务日志完整提取

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-10-8 08:47:59 | 显示全部楼层 |阅读模式
#!/usr/bin/python
# -*- coding:utf-8 -*-
#auth kxr by 2019.9.12
import re
import os
import time
import cx_Oracle
print '############CBOSS测试环境日志提取脚本##########'
print '##########仅支持当天的流水和网状网日志#########'
print '####提取成功将在当前目录生成保存日志的文件#####'
while True:
input1=raw_input("业务类型:(落地'y',上发'n'):\n")
if any(input1 == i for i in ['n','N']):
start_tag='准备开始处理请求'
end_tag=['结束实时上发请求处理','实时上发CBOSS异常']
break
elif input1 in ('y','Y') :
start_tag="Begin of HttpServletRequest"
end_tag=['落地业务受理过程中发生异常','End of HttpServletRequest']
break
else :  pass
while True:
input2=raw_input("唯一关键字(transido,opr_num,seq...):\n")
if input2:
key_word=input2.strip()
break
line_num=0
th_num=None
match=''
f_log=[]
b_log=[]
c_log=[]
log_file='/app/aicboss/webapp/log/cboss_n2n.log'
file=open(log_file,'r')
while True:
line=file.readline()
line_num+=1
if key_word in line:
ofs=file.tell()
b_log.append(line)
file.seek(0)
match=re.search(r'httpWorkerThread-[0-9]+-[0-9]+',line)
lines=file.readlines()[line_num-1000 if line_num-1000 > 0 else 0 :line_num-1][::-1]
if match:
th_num=match.group()
for sub_line in lines:
if th_num:
if start_tag  in sub_line:
f_log.append(sub_line)
break
elif th_num in sub_line:
f_log.extend(c_log)
c_log=[]
f_log.append(sub_line)
elif 'httpWorkerThread' not in sub_line:
c_log.append(sub_line)
else:
c_log=[]
elif 'httpWorkerThread'  in sub_line:
match=re.search(r'httpWorkerThread-[0-9]+-[0-9]+',sub_line)
th_num=match.group()
f_log.append(sub_line)
else:
f_log.append(sub_line)
file.seek(ofs,0)
tag=True
for sub_line in file:
if any(t in sub_line for t in end_tag):
b_log.append(sub_line)
break
elif th_num in sub_line:
b_log.append(sub_line)
tag=True
elif 'httpWorkerThread' not in sub_line:
if  tag : b_log.append(sub_line)
else:tag=False
newf_name=key_word+'.log'
path=os.getcwd()
new_file=open(os.path.join(path,newf_name),'w')
f_log.reverse()
new_file.writelines(f_log)
new_file.writelines(b_log)
file.close()
new_file.close()
print 'log_ref complete ! \nbeging ref msg...'
break
if not line :
print 'file scan end !key_word no found !'
break
'''
if 'newf_name' in dir():
file=open(os.path.join(path,newf_name))
for line in file:
if 'DoneCode' in line:
match=re.search(r'DoneCode.*[0-9]+',line)
if match:
donecode=re.search(r'[0-9]+',match.group()).group()
break
file.close()
if 'donecode' in dir():
table_pos=time.strftime("%Y%m",time.localtime())
table_name='cboss.xml_info_'+table_pos
conn = cx_Oracle.connect('YAXIN_CESHI/bqg776^*@ 172.16.9.29/yydbtest')
cursor = conn.cursor()
req_msg=['请求报文:']
rep_msg=['应答报文:']
msgf_name=key_word+'.xml'
sql="SELECT actioncode,xml_data from "+table_name+" where done_code=:done_code"
param=[donecode]
cursor.execute(sql,param)
rows = cursor.fetchall()
for row in rows:
if row[0]==0:
req_msg.append(row[1].read())
elif row[0]==1:
rep_msg.append(row[1].read())
else:pass
msg_file=open(os.path.join(path,msgf_name),'w')
msg_file.writelines(map(lambda x :x+'\n',req_msg))
msg_file.writelines(map(lambda x :x+'\n',rep_msg))
msg_file.close()
cursor.close()
conn.close()
'''
if 'newf_name' in dir():
print '日志文件已生成:%s'%(newf_name)
#if 'msgf_name' in dir():
#    print '报文文件已生成:%s'%(msgf_name)
#172.16.9.29



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-282826-1-1.html 上篇帖子: 大家听说过黄哥Python视频吗? 下篇帖子: Python自定义模块导入
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表