linux tomcat日志统计,python 统计tomcat access日志访问时延

#!/usr/bin/python2.7

# -*- coding:utf-8 -*-

import re

import time

import sys

import os

#定义日志文件位置

log = '/data/program/tomcat/payment-pre-interface_https/node/logs/payment-pre-interface_https_access_log.%s.log' %(time.strftime('%Y-%m-%d'))

#定义原始日志文件位置

old_endseek = ''

#定义新日志文件位置

new_endseek = ''

#只读方式打开日志文件

with open(log,'r') as obj:

#把位置指向文件末尾

obj.seek(0,2)

#定义新文件位置

new_endseek = obj.tell()

#record.txt文件需要自己先定义好格式

#第一行文件seek位置

#第二行日期

#0

#20151201

#以上是首次定义此文件内容

with open('/tmp/record.txt','r') as obj:

old = obj.readlines()

#获取原始seek位置

old_endseek = int(old[0])

#获取原始时间

old_time = str(old[1])

#获取当前时间

current_time = time.strftime('%Y%m%d')

#判断原始时间与当前时间是否相等

if old_time.strip('\n') == current_time:

with open('/tmp/record.txt','w+') as record:

record.writelines(str(new_endseek))

record.writelines('\n' + time.strftime('%Y%m%d'))

else:

#如果时间不相等初始化record.txt文件

os.popen('/bin/echo "0" > /tmp/record.txt')

os.popen('/bin/echo `date +%Y%m%d` >> /tmp/record.txt')

#定义新获取文件内容

new_content = ''

#定义获取的文件内容时延的列表

time_list = []

with open(log,'r') as obj:

obj.seek(old_endseek)

new_content = obj.readlines()

for i in new_content:

if re.findall('(.*?)/auth/pay.do',i,re.S):

time_list.append(re.findall('(\d+)',i,re.S))

current_time = 0

max_time = 0

for i in time_list:

current_time = int(i[-1])

if current_time > max_time:

max_time = current_time

print 'pre /auth/pay.do take time %s | /auth/pay.do=%s;10000;15000;0' %(max_time,max_time)

if max_time >= 10000:

sys.exit(2)

elif max_time >= 5000 and max_time 

sys.exit(1)

elif max_time 

sys.exit(0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值