|
#_*_ coding:utf-8 _*_
# auth: xinsir
# date: 2017/10/02
# version:3.0
import systemmongo,os
# 写一个方法,判断文件是否存在.返回布尔值
def judgment_file(FileName):
if os.path.exists( FileName ):
return True
else:
return False
# 写一个方法,把字符串切割成列表,return list
def SplitStr(StrName, Format,):
return StrName.split(Format)
# 读取日志文件,获取文件中的所有包含action的记录,并写入到新的log文件中
def read_file(file, new_file,):
ReadPosition = 0
LastLine = 0
FileSize = 0
FileDic = {}
# 打开两个文件,一个是日志文件,一个是临时文件,把日志文件进行更改后,以字典的格式序列化到日志文件中
with open(file, 'r') as log_file, open(new_file, 'w') as new_log_file:
# 查看文件总长多少,写入临时文件
FileSizeNew = os.path.getsize(file)
log_file.seek(ReadPosition)
if FileSize < FileSizeNew:
for (num, line) in enumerate(log_file):
if '.action' in line.strip():
new_line = line.strip().split(sep='\" \"')
ListFirstValue = SplitStr(new_line[0], '\"')
ListMostValue = SplitStr(new_line[-1], '\"')
del new_line[0]
del new_line[-1]
new_line.insert(0, ListFirstValue[1])
new_line.append(ListMostValue[0])
Method = str(new_line[3]).split()[0]
request = str(new_line[3]).split()[1]
HttpVersion = str(new_line[3]).split()[2]
if '?' in request:
Uri = request.split(sep='?')[0]
Query_string = request.split(sep='?')[1]
else:
Uri = request.split( sep='?' )[0]
Query_string = ''
if '.action' in Uri:
# if LogFileStatus :
FileDic[num + 1 + LastLine] = {
'remote_addr': new_line[0],
'host': new_line[1],
'time_local': new_line[2],
'request': request,
'uri':Uri,
'query_string':Query_string,
'eethod': Method,
'HttpVersion': HttpVersion,
'body_bytes_sent': new_line[4],
'http_referer': new_line[5],
'http_user_agent': new_line[6],
'http_x_forwarded_for': new_line[7],
'server_addr': new_line[8],
'status': new_line[9],
'request_time': new_line[10],
}
else:
print('静态请求不做记录!')
continue
IsNot = systemmongo.SechMongo(
'http://' + FileDic[num + 1 + LastLine]['host'] + FileDic[num + 1 + LastLine]['uri'])
if IsNot:
systemmongo.Update(
'http://' + FileDic[num + 1 + LastLine]['host'] + FileDic[num + 1 + LastLine]['uri'])
print('更新记录成功:''http://' + FileDic[num + 1 + LastLine]['host'] + FileDic[num + 1 + LastLine]['uri'])
else:
print('action请求不存在!不做记录!')
else:
print('静态请求不做处理!')
else:
print('日志文件没有发生改变!')
new_log_file.write(str(FileDic))
log_file.close()
new_log_file.close()
if __name__ == '__main__':
LOGPATH_new = '../nginxlog/b.txt'
for fpathe, dirs, fs in os.walk('E:\python工程\jlj-nginx-log-web\jlj-nginx-log-web\\nginxlog\log'):
#循环目录下的所有日志文件
for f in fs:
LOGPATH = os.path.join(fpathe, f)
read_file(LOGPATH, LOGPATH_new) |
|
|