-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
180 lines (134 loc) · 6.9 KB
/
main.py
File metadata and controls
180 lines (134 loc) · 6.9 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from datetime import datetime
import sys
# list storing the data extracted from each log
LOGS = []
print("---------------------LOG ANALYZER-------------------------\n")
print('Enter the relative path of your log file:', end=" ")
file_path = input()
with open(file_path) as logs:
for log in logs:
log_data = log.split()
ip = log_data[0]
date_time = log_data[3].strip('[')
dt = datetime.strptime(date_time, "%d/%b/%Y:%H:%M:%S")
method = log_data[5].strip('"')
endpoint = log_data[6]
req_type = log_data[7].strip('"')
status_code = int(log_data[8])
response_size = int(log_data[9])
response_time = int(log_data[10][:-2])
LOGS.append({
'ip': ip,
'date_time': dt,
'method': method,
'endpoint': endpoint,
'req_type': req_type,
'status_code': status_code,
'response_size': response_size,
'response_time': response_time
})
if len(LOGS) <= 0:
raise RuntimeError("ERROR WHILE READING LOG FILE")
while True:
print('What kind of information you want to get from the logs?')
print("""
1. Specifc Errors (e.g. 500 errors between 12 Dec and 13 Dec)
2. Which IP caused the most failed logins (401)?
3. Average response time across all requests
4. Which IPs requested more than [number_of_reqs] times over a short duration [duration] seconds?
5. Fastest and Slowest Endpoints
""")
print("Which one do you want to select?", end=" ")
user_input = int(input())
if user_input == 1:
print("\nEnter error status code:", end=" ")
status_code = int(input())
print("Enter start date:", end=" ")
start_date = input()
start_date_time = datetime.strptime(start_date, "%Y %d %b")
print("Enter end date:", end=" ")
end_date = input()
end_date_time = datetime.strptime(end_date, "%Y %d %b")
error_count = 0
for log in LOGS:
if ( log['status_code'] == status_code and
start_date_time <= log['date_time'] <= end_date_time
):
error_count += 1
print('------------------------------------------------------------------------------------\n')
print(f"===============> {error_count} {status_code} errors happened between {start_date} and {end_date} <===============\n")
print('------------------------------------------------------------------------------------\n')
elif user_input == 2:
ip_to_failed_login_freq = {}
for log in LOGS:
if log['status_code'] == 401:
ip = log['ip']
ip_to_failed_login_freq[ip] = ip_to_failed_login_freq.get(ip, 0) + 1
max_failed_login_ip = max(ip_to_failed_login_freq, key=ip_to_failed_login_freq.get)
failed_logins = max(ip_to_failed_login_freq.values())
print('------------------------------------------------------------------------------------\n')
print(f"===============> IP - `{max_failed_login_ip}` caused the most ({failed_logins}) failed logins (401) <===============\n")
print('------------------------------------------------------------------------------------\n')
elif user_input == 3:
sum_of_resp_times = sum(log['response_time'] for log in LOGS)
total = len(LOGS)
avg_resp_time = round(sum_of_resp_times / total, 3)
print('------------------------------------------------------------------------------------\n')
print(f"===============> Average response time across all requests is: {avg_resp_time} <===============\n")
print('------------------------------------------------------------------------------------\n')
elif user_input == 4:
print("\nEnter number of frequent requests: ")
number_of_reqs = int(input())
print("Enter duration in seconds for the requests: ")
duration = int(input())
ip_to_times = {}
for log in LOGS:
lst = ip_to_times.get(log['ip'], [])
lst.append(log['date_time'])
ip_to_times[log['ip']] = lst
result_ips = []
for ip in ip_to_times:
times = ip_to_times[ip]
if len(times) < number_of_reqs:
continue
times.sort()
left = 0
right = 1
max_total_cons_reqs = 1
# sliding window implementation to determine the reqs in a time window
while right < len(times):
if (times[right] - times[left]).total_seconds() <= duration:
right += 1
else:
max_total_cons_reqs = max(max_total_cons_reqs, right - left)
left += 1
max_total_cons_reqs = max(max_total_cons_reqs, right - left)
if max_total_cons_reqs >= number_of_reqs:
result_ips.append(ip)
print("-----------------------------------------------------------------------\n")
print(f"===============> IPs that requested {number_of_reqs} requests in less than {duration} seconds: \n")
for ip in result_ips:
print(f"--------- {ip}")
print(f"\n<===============")
print("-----------------------------------------------------------------------\n")
elif user_input == 5:
fastest_response_time = sys.maxsize
slowest_response_time = -1
fastest_endpoint_ip = None
fastest_endpoint = None
slowest_endpoint_ip = None
slowest_endpoint = None
for log in LOGS:
if log['response_time'] < fastest_response_time:
fastest_response_time = log['response_time']
fastest_endpoint_ip = log['ip']
fastest_endpoint = log['endpoint']
if log['response_time'] > slowest_response_time:
slowest_response_time = log['response_time']
slowest_endpoint_ip = log['ip']
slowest_endpoint = log['endpoint']
print('-------------------------------------------------------------\n')
print(f"===============> Fastest Endpoint - {fastest_endpoint} from IP - {fastest_endpoint_ip} with response_time: {fastest_response_time} <===============")
print()
print(f"===============> Slowest Endpoint - {slowest_endpoint} from IP - {slowest_endpoint_ip} with response_time: {slowest_response_time} <===============")
print('-------------------------------------------------------------\n')