python分析log日志_python日志实时分析

本文介绍了如何利用Python进行日志分析,特别是在一个区块链项目中,通过解析日志定位出块时间不稳定的问题。主要使用了datetime和subprocess库,通过tail -f命令实时获取日志文件新增内容。代码示例展示了如何监测并计算区块出块时间间隔。
摘要由CSDN通过智能技术生成

python随着人工智能的发展,越来越火热。但其实python在运维测试方面,也是一把利器。 最近就碰到了个需求,就顺手写了个python程序。用惯了go,不过发现python好像更简单点 :-)

涉及到的主要python包和系统命令:

1. datetime #用于处理时间

2. subprocess #用于调用命令行工具

3. tail -f logFile #获取logFile新增内容

废话不多说,下面说一下场景需求和具体解决方案。

1. [场景需求]

最近参与了一个区块链项目,在项目测试过程中,通过日志发现出块不稳定,为了定位问题,需要精确的统计出块时间。截取一段日志如下:

I[2019-07-11|16:57:24.139][45712] reapMaxBytesMaxGas module=mempool mempoolTxNum=0 reapTxNum=0 maxNumPerBlock=2000

I[2019-07-11|16:57:24.220][45712] [60]---------- BeginBlock[4367] ---------- module=main

I[2019-07-11|16:57:24.224][45712] Executed block module=state height=4367 validTxs=0 invalidTxs=0

D[2019-07-11|16:57:24.307][45712] Commit synced module=main commit=436F6D6D697449447B5B333520342031383720363120343120313336203137312031203137302032343120323220313230203836203233382031333120313634203231302031323420313530203233322036203434203130342033322036302031303220343120323530203131352031323720313138203134355D3A313130467D

I[2019-07-11|16:57:24.307][45712] BlockHeight<4367>, BeginBlock<0ms>, DeliverTx<0ms>, EndBlock<0ms>, Commit<82ms>, Tx<0> module=main

I[2019-07-11|16:57:24.307][45712] BlockHeight<4367>, Abci<82ms>, Tx<0>, fullfilled<0>, pending<0>, canceled<0>, expired<0>, partialfilled<0> module=main

I[2019-07-11|16:57:24.307][45712] Committed state module=state height=4367 txs=0 appHash=2304BB3D2988AB01AAF1167856EE83A4D27C96E8062C68203C6629FA737F7691

I[2019-07-11|16:57:25.208][45712] reapMaxBytesMaxGas module=mempool mempoolTxNum=0 reapTxNum=0 maxNumPerBlock=2000

I[2019-07-11|16:57:25.284][45712] [60]---------- BeginBlock[4368] ---------- module=main

2. [日志分析]

日志中Committed state代表状态持久化,也就是区块生命周期的末尾。 因此我们使用Committed state来对日志进行过滤,并用最新一条日志的时间戳减去上条日志时间戳作为本区块出块时间。

3. [代码]

#!/usr/bin/python

#encoding=utf-8

#Filename: block_time.py

import subprocess

from datetime import datetime

import time

logFile = "./blockchain.log"

lastDateTime = datetime.now()

#主程序

def monitorLog(logFile):

print("monitor logFile: %s" % logFile)

stopTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()+100)) #脚本100秒后退出

popen = subprocess.Popen(["tail", "-f", logFile], stdout=subprocess.PIPE, stderr=subprocess.PIPE) #启动子进程执行tail命令

while True:

line = popen.stdout.readline().strip() #按行获取日志

if line:

if 'Committed state' in line:

getBlockInterval(line) #分析日志

now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())) #当前时间

if now >= stopTime: #如果当前时间超过stopTime,表示不再分析日志,退出程序

popen.kill()

print("kill subprocess")

break

#计算连续两条日志的时间差

def getBlockInterval(line):

t = getDateTime(line)

global lastDateTime

blockInterval = (t-lastDateTime).total_seconds() #将datetime时间差用seconds计算

blockHeight = getBlockHeight(line)

lastDateTime = t

print('%s, interval=%s' % (blockHeight, blockInterval)) #打印结果

#获取区块高度

def getBlockHeight(line):

return line.split('module')[1].split(' ')[1]

#获取每行日志的时间戳

def getDateTime(line):

line = line.split('[')[1][:-1].split('.')[0].replace('|', ' ')

t = datetime.strptime(line, "%Y-%m-%d %H:%M:%S")

return t

if __name__ == '__main__':

monitorLog(logFile)

4. [日志分析结果]

height=4343, interval=1.0

height=4344, interval=1.0

height=4345, interval=1.0

height=4346, interval=1.0

height=4347, interval=1.0

height=4348, interval=1.0

height=4349, interval=2.0

height=4350, interval=1.0

height=4351, interval=1.0

原文:https://www.cnblogs.com/informatics/p/11171718.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值