1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
| #!/usr/bin/python
# _*_coding:utf-8 _*_
import os
import shutil
import sys
import re
import random
import time
from xlrd.formula import nop
#初始化系统
reload(sys)
sys.setdefaultencoding('utf-8')
#逻辑为:
#1、将目标文件拷贝到临时目录
#2、分析目标临时文件,先找到里面包含哪些日期
#3、根据日期创建每个日期的访问日志记录文件
#4、用匹配出来的日期,拼接匹配访问url的正则表达式
#5、逐行分析临时文件,将对应日期,符合表达式的放到对应日期的日志记录里
#6、分析完成之后对各文件数行,然后写入总记录文件
#7、可以实现:根据日期统计每日url访问量,每条访问的access日志详情
####################################
#搜索结果保存路径
save_result="/Users/liuli/Desktop/access"
log_file_source="/Users/liuli/Desktop/test.log"
####################################
#拷贝文件
def copyFiles(sourceFile, targetFile):
open(targetFile, "wb").write(open(sourceFile, "rb").read())
if os.path.exists(save_result):
shutil.rmtree(save_result)
os.makedirs(save_result)
else:
os.makedirs(save_result)
#数文件行数
def count_lines(file):
try:
fp = open(file, "r")
return str(len(fp.readlines()))
except Exception,e:
return
print e
sys.exit(0)
finally:
fp.close()
#正则匹配
def iscontain(regrex, strings):
try:
pattern = re.compile(regrex, re.S)
item = pattern.search(strings).group()
return item
except Exception, e:
return
print e
sys.exit(0)
#获取今天对应的月份
def get_today_month3():
ISOTIMEFORMAT = '%B'
return str(time.strftime(ISOTIMEFORMAT, time.localtime())[0:3])
#获取今天对应的日期
def get_today_day():
ISOTIMEFORMAT = '%d'
return str(time.strftime(ISOTIMEFORMAT, time.localtime()))
#往文件中写内容
def write_to_file(file,strings):
if os.path.isfile(file):
try:
file_object = open(file, "a")
file_object.write(strings)
except Exception, e:
print e
finally:
file_object.close()
else:
try:
file_object = open(file, "w")
file_object.write(strings)
except Exception, e:
print e
finally:
file_object.close()
#将nginx的日志格式写入到日志文件中!
write_to_file(save_result + "/log_format.txt",'$remote_addr - $remote_user [$time_local] \"$request\" ' '$status $body_bytes_sent \"$http_referer\" ' '\"$http_user_agent\" $http_x_forwarded_for \"$upstream_addr\" \"$upstream_status\" \"$upstream_response_time\" \"$request_time\"')
#初始化
num = random.randrange(10086, 1008611)
log_file = "/tmp/nginx_counter_tmp_" + str(num) + ".log"
#不在源文件上分析,将源文件拷贝到临时目录
copyFiles(log_file_source, log_file)
days=[]
all_regrex=[]
forword_regrex="^[0-9].([0-9]{1,3}\.){3}[0-9]{1,3}\ -\ -\ "
day_regrex="(\[)([0-3]{1})([0-9]{1})(\/)(\w{3})(\/)(\d{4})"
conn_regrex="([\s\S]*)"
count_regrex="((GET)|(POST))(\ )(\/)(tserv)(\ )(HTTP)([\s\S]*)"
#获取日期列表days
f=open(log_file,"r")
line = f.readline()
i=0
while line:
pattern = re.compile(day_regrex, re.S)
if pattern.search(line) is None :
day111 = '1'
else:
item=pattern.search(line).group()
regrexs = forword_regrex+"\\" + item + conn_regrex + count_regrex
pattern1 = re.compile(regrexs, re.S)
if pattern1.search(line) is None :
day111 = '1'
else:
item1 = pattern1.search(line).group()
write_to_file(save_result+"/" + str(item).replace("[", "").replace("/", "_").replace("]", "").replace(":","_") + ".txt",str(item1))
line = f.readline()
#记录结果格式化日志:
f.close()
os.remove(log_file)
#匹配内容并写入分体文件
#创建记录文件并将每个文件对应的行数【访问量】写入文件
for file in os.listdir(save_result):
write_to_file(save_result+"/count_save.txt",file+" lines "+count_lines(save_result+"/"+file
|