forked from jkklee/abs-log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_show.py
74 lines (64 loc) · 3.59 KB
/
log_show.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
#!/bin/env python3
# -*- coding:utf-8 -*-
"""
Usage:
log_show <site_name> [options] request [distribution [<request>]|detail <uri>]
log_show <site_name> [options] ip [distribution <ip>|detail <ip>]
log_show <site_name> [options] error [distribution <error_code>|detail <error_code>]
Options:
-h --help Show this screen.
-f --from <start_time> Start time. Format: %y%m%d[%H[%M]], %H and %M is optional
-t --to <end_time> End time. Format is same as --from
-l --limit <num> Number of lines in output, 0 means no limit. [default: 5]
-s --server <server> Web server hostname
-g --group_by <group_by> Group by every minute, every ten minutes, every hour or every day,
valid values: "minute", "ten_min", "hour", "day". [default: hour]
distribution Show distribution(about hits,bytes,time,etc) of:
all or specific 'request', the specific 'ip', the specific 'error_code' in every period.
Period is specific by --group_by
detail Show details of:
detail 'args' analyse of the specific 'uri'(if it has args);
detail 'uri' analyse of the specific 'ip' or 'error_code'
Notice: it's best to put 'request_uri', 'uri' and 'ip' in quotation marks.
"""
from docopt import docopt
from common.common import mongo_client, match_condition, total_info
from common.show import request_show, ip_show, error_show
arguments = docopt(__doc__)
# print(arguments) #debug
# 判断--group_by合理性
if arguments['--group_by'] not in ('minute', 'ten_min', 'hour', 'day'):
print(" Warning: --group_by must be one of {'minute', 'ten_min', 'hour', 'day'}")
exit(10)
mongo_db = mongo_client[arguments['<site_name>'].replace('.', '')]
# mongodb集合
mongo_col = mongo_db['main']
# 最基本的过滤条件
base_match = match_condition(arguments['--server'], arguments['--from'], arguments['--to'])
# 根据参数执行动作
if arguments['distribution'] and arguments['request']:
request_show.distribution(mongo_col, arguments)
elif arguments['distribution'] and arguments['ip']:
ip_show.distribution(mongo_col, arguments)
elif arguments['distribution'] and arguments['error']:
error_show.distribution(mongo_col, arguments)
elif arguments['detail'] and arguments['request']:
request_show.detail(mongo_col, arguments)
elif arguments['detail'] and arguments['ip']:
ip_show.detail(mongo_col, arguments)
elif arguments['detail'] and arguments['error']:
error_show.detail(mongo_col, arguments)
elif arguments['ip']:
total_dict = total_info(mongo_col, base_match)
ip_show.base_summary('last_cdn_ip', int(arguments['--limit']), mongo_col, base_match, total_dict)
ip_show.base_summary('user_ip_via_cdn', int(arguments['--limit']), mongo_col, base_match, total_dict)
ip_show.base_summary('user_ip_via_proxy', int(arguments['--limit']), mongo_col, base_match, total_dict)
ip_show.base_summary('remote_addr', int(arguments['--limit']), mongo_col, base_match, total_dict)
elif arguments['error']:
total_dict = total_info(mongo_col, base_match)
error_show.base_summary(int(arguments['--limit']), mongo_col, base_match, total_dict)
else:
total_dict = total_info(mongo_col, base_match)
request_show.base_summary('hits', int(arguments['--limit']), mongo_col, base_match, total_dict)
request_show.base_summary('bytes', int(arguments['--limit']), mongo_col, base_match, total_dict)
request_show.base_summary('time', int(arguments['--limit']), mongo_col, base_match, total_dict)