-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_log.py
54 lines (44 loc) · 1.74 KB
/
parse_log.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
import re
import math
import datetime
def Main():
ip_req = {}
req_ten = {}
resp_time = []
j = 0
with open('access.log', 'r') as f:
for line in f:
pat = re.compile("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
ip = re.findall(pat, line)
patx = re.compile("\[.*.\]")
tim = re.findall(patx, line)[0]
tim = tim.split(" ")
if j == 0:
ptime = datetime.datetime.strptime(tim[0][1:],\
'%d/%b/%Y:%H:%M:%S')
j += 1
timen = datetime.datetime.strptime(tim[0][1:],\
'%d/%b/%Y:%H:%M:%S')
delta = str(ptime) + ' - ' + str(ptime + datetime.\
timedelta(minutes = 10))
if timen <= (ptime + datetime.timedelta(minutes = 10)):
req_ten[delta] = req_ten.get(delta, 0) + 1
else:
req_ten[delta] = req_ten.get(delta, 0)
ptime += datetime.timedelta(minutes = 10)
vals = line.split(" ")
# print (vals[8] + ' ' + vals[9])
resp_time.append(int(vals[8]))
ip_req[ip[0]] = ip_req.get(ip[0], 0) + 1
print('------IP------\t----Requests----')
for ips in sorted(ip_req):
print("%-15s\t\t%d" % (ips, ip_req[ips]))
print('\n-------------------Date------------------'\
'\t---Requests---')
for req in sorted(req_ten):
print('%s\t\t%d' % (req, req_ten[req]))
resp_time.sort()
indx = int(math.ceil(len(resp_time) * .99))
print('\nTP99 = ' + str(resp_time[indx]))
if __name__ == "__main__":
Main()