-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.py
90 lines (69 loc) · 2.69 KB
/
main.py
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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
from pd_ng_log_stat import PDNgLogStat
import argparse
import os
def filter_eff_files(files):
"""过滤有效文件"""
return [path for path in files if os.path.exists(path)]
def filter_eff_file(file):
"""过滤有效文件"""
if os.path.exists(file):
return file
else:
return None
def get_eff_methods_no_addr():
"""获得可以执行的方法 除了需要IP地址文件的方法"""
methods = [
'pv_day', # 统计每日 pv
'pv_hour', # 统计每小时 pv
'url_ref_stat', # 统计外链点击情况
'url_req_stat', # 统计页面点击量
'uv_cdn_ip', # 统计 CDN IP 访问量
'uv_real_ip', # 统计 真实用户 IP 访问量
'browser_stat', # 统计 统计不同浏览器访问次数
]
return methods
def get_eff_methods():
"""获得可以执行的方法"""
methods = get_eff_methods_no_addr()
methods.append('uv_cdn_ip_addr') # 统计 CDN IP 访问量 和 地址
methods.append('uv_real_ip_addr') # 统计 用户真实 IP 访问量 和 地址
return methods
def filter_methods_no_addr(methods):
return list(set(get_eff_methods_no_addr()).intersection(set(methods)))
def filter_methods(methods):
return list(set(get_eff_methods()).intersection(set(methods)))
def main():
parser = argparse.ArgumentParser(description='parse nginx log')
# 文件参数
parser.add_argument('-f', '--files', required = True,
help = 'specified file one or multi file')
# 运行那个方法
parser.add_argument('-m', '--methods', required = True,
help = 'specified method to run')
# ip 对应地址的 路径
parser.add_argument('-i', '--ipfile',
help = 'specified ip address file')
# 解析并检查参数是否合法
args = parser.parse_args()
# 获取有效文件
files = filter_eff_files(args.files.split())
area_ip_path = None
if args.ipfile:
area_ip_path = filter_eff_file(args.ipfile) # 判断ip文件是否可用
# 获取需要运行的方法
methods = args.methods.split()
# 创建统计实例
pd_ng_log_stat = PDNgLogStat()
pd_ng_log_stat.load_data(files)
# 判断是是否有 ip文件 并加载相关执行方法
if area_ip_path:
methods = filter_methods(methods) # 和所有方法过滤
pd_ng_log_stat.load_ip_addr(area_ip_path) # 加载IP文件
else:
methods = filter_methods_no_addr(methods) # 去除需要Ip文件的方法
for method in methods:
print eval('pd_ng_log_stat.{method}()'.format(method = method))
if __name__ == '__main__':
main()