#!/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