-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_analysis_script.py
More file actions
60 lines (43 loc) · 1.71 KB
/
Copy pathlog_analysis_script.py
File metadata and controls
60 lines (43 loc) · 1.71 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
import pandas as pd
import re
from datetime import datetime
def load_logs(file_path):
"""Reads log entries from a file."""
with open(file_path, 'r') as file:
logs = file.readlines()
return logs
def filter_logs(logs):
"""Filters logs for specific patterns, such as failed logins or critical alerts."""
filtered_logs = []
# Define patterns to search for in logs
failed_login_pattern = re.compile(r"failed login", re.IGNORECASE)
critical_alert_pattern = re.compile(r"CRITICAL|ALERT", re.IGNORECASE)
for log in logs:
# Check if log matches any pattern
if failed_login_pattern.search(log) or critical_alert_pattern.search(log):
filtered_logs.append(log)
return filtered_logs
def prioritize_alerts(filtered_logs):
"""Prioritizes critical logs for immediate attention."""
prioritized_logs = []
# Define pattern for critical or high-priority alerts
critical_pattern = re.compile(r"CRITICAL|malware|unauthorized access", re.IGNORECASE)
for log in filtered_logs:
# Check if log matches critical pattern
if critical_pattern.search(log):
prioritized_logs.append(log)
return prioritized_logs
if __name__ == "__main__":
# Load logs from the sample file
logs = load_logs('sample_logs/example_log.txt')
# Filter logs for important events
filtered_logs = filter_logs(logs)
# Prioritize high-severity alerts
prioritized_logs = prioritize_alerts(filtered_logs)
# Display the results
print("Filtered Logs:")
for log in filtered_logs:
print(log.strip())
print("\nPrioritized Alerts:")
for log in prioritized_logs:
print(log.strip())