forked from yangpeng14/DevOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginx_flow_analysis3.py
131 lines (120 loc) · 4.87 KB
/
nginx_flow_analysis3.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python3
#-*-coding=utf-8-*-
import time
import os
import sys
import argparse
class displayFormat():
def format_size(self, size):
# 格式化流量单位
KB = 1024 # KB -> B B是字节
MB = 1048576 # MB -> B
GB = 1073741824 # GB -> B
TB = 1099511627776 # TB -> B
if size >= TB:
size = str("%.2f" % (float(size / TB)) ) + 'T'
elif size < KB:
size = str(size) + 'B'
elif size >= GB and size < TB:
size = str("%.2f" % (float(size / GB))) + 'G'
elif size >= MB and size < GB:
size = str("%.2f" % (float(size / MB))) + 'M'
else:
size = str("%.2f" % (float(size / KB))) + 'K'
return size
def execut_time(self):
# 输出脚本执行的时间
print('\n')
print("Script Execution Time: %.3f second" % time.clock())
class input_logfile_sort():
# 内存优化
__slots__ = ['read_logascii_dict', 'key']
def __init__(self):
self.read_logascii_dict = {}
self.key = 1
def logascii_sortetd(self, logfile):
with open(logfile, 'r') as f:
for line in f.readlines():
try:
list_line = line.split()
timeArray = time.strptime(list_line[3].strip('['), "%d/%b/%Y:%H:%M:%S")
timeStamp_start = int(time.mktime(timeArray))
list_line1 = [timeStamp_start, list_line[9]]
#生成字典
self.read_logascii_dict[self.key] = list_line1
self.key += 1
except ValueError:
continue
except IndexError:
continue
sorted_list_ascii = sorted(self.read_logascii_dict.items(), key=lambda k: (k[1][0]))
return sorted_list_ascii
# out [(4, [1420686592, '1024321222']), (3, [1449544192, '10243211111'])]
class log_partition():
display_format = displayFormat()
def __init__(self):
self.size1 = 0
self.j = 0
def time_format(self, time_stamps_start, time_stamps_end):
time_start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time_stamps_start))
time_end = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time_stamps_end))
print(time_start + ' -- ' + time_end + ' ' * 6 + '<=========>' + ' ' * 6 + self.display_format.format_size(self.size1))
def log_pr(self, stored_log, times):
time_stamps_start = stored_log[0][1][0]
time_stamps_end = time_stamps_start + times
lines = len(stored_log)
for line in stored_log:
self.j += 1
if int(line[1][0]) <= time_stamps_end:
try:
self.size1 = self.size1 + int(line[1][1])
if self.j == lines:
self.time_format(time_stamps_start, time_stamps_end)
except ValueError:
continue
else:
try:
self.time_format(time_stamps_start, time_stamps_end)
self.size1 = 0
self.size1 = self.size1 + int(line[1][1])
time_stamps_start = int(line[1][0])
time_stamps_end = time_stamps_start + times
if self.j == lines:
self.time_format(time_stamps_start, time_stamps_end)
except ValueError:
continue
class Main():
#主调函数
def main(self):
parser = argparse.ArgumentParser(
description="Nginx flow analysis, Supported file types ascii text.")
parser.add_argument('-f', '--file',
dest='file',
nargs='?',
help="log file input.")
parser.add_argument('-m', '--minute',
dest='minute',
default=5,
nargs='?',
type=int,
help="Nginx separation time,Default 5 min.")
args = parser.parse_args()
Input_sorted_log = input_logfile_sort()
display_format1 = displayFormat()
times = args.minute * 60
for type in os.popen("""file {}""".format(args.file)):
file_type = type.strip().split()[1]
if file_type.lower() == 'ascii':
logascii_analysis = log_partition()
logascii_analysis.log_pr(Input_sorted_log.logascii_sortetd(args.file), times)
print('\033[1;32;40m')
display_format1.execut_time()
print('\033[0m')
else:
print('\033[1;32;40m')
print("Supported file types ascii text.")
print("Example: python3 {} -f nginxlogfile -m time".format(sys.argv[0]))
print('\033[0m')
if __name__ == '__main__':
main_obj = Main()
main_obj.main()