-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_analyzer.py
More file actions
44 lines (32 loc) · 1.19 KB
/
log_analyzer.py
File metadata and controls
44 lines (32 loc) · 1.19 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
import sys
def analyze_log(file_path):
# Dictionary to store counts
status_counts = {}
try:
with open(file_path, 'r') as file:
for line in file:
parts = line.split()
if len(parts) < 9:
continue
status_code = parts[7]
# Dictionary তে count করি
if status_code in status_counts:
status_counts[status_code] += 1
else:
status_counts[status_code] = 1
return status_counts
except FileNotFoundError:
print("File not found!")
return {}
def print_report(counts):
print("--- Server Health Report ---")
for code, count in counts.items():
print(f"Status {code}: {count} occurrences")
# Alert logic
if counts.get("500", 0) > 2:
print("!!! CRITICAL ALERT: High rate of server failures detected !!!")
if __name__ == "__main__":
# Command line argument support
log_file = sys.argv[1] if len(sys.argv) > 1 else "server.log"
data = analyze_log(log_file)
print_report(data)