-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog_analyzer.py
More file actions
67 lines (53 loc) · 2.24 KB
/
log_analyzer.py
File metadata and controls
67 lines (53 loc) · 2.24 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
import re
import sys
from collections import Counter
# Configuration for log file (adjust path accordingly)
LOG_FILE = '/path/to/web/server/logfile.log'
# Regular expression patterns to extract data from the log lines
IP_PATTERN = r'(\d+\.\d+\.\d+\.\d+)' # Extracts IP addresses
STATUS_CODE_PATTERN = r'"\s(\d{3})\s' # Extracts HTTP status codes (e.g., 404, 200)
REQUEST_PATTERN = r'"(GET|POST|PUT|DELETE)\s(.*?)\sHTTP' # Extracts requested URLs
def analyze_log_file(log_file_path):
try:
with open(log_file_path, 'r') as file:
lines = file.readlines()
ip_addresses = []
status_codes = []
requests = []
for line in lines:
# Extract IP addresses
ip_match = re.search(IP_PATTERN, line)
if ip_match:
ip_addresses.append(ip_match.group(1))
# Extract HTTP status codes
status_code_match = re.search(STATUS_CODE_PATTERN, line)
if status_code_match:
status_codes.append(status_code_match.group(1))
# Extract requested URLs
request_match = re.search(REQUEST_PATTERN, line)
if request_match:
requests.append(request_match.group(2))
# Analyzing log data
ip_count = Counter(ip_addresses)
status_count = Counter(status_codes)
request_count = Counter(requests)
# Report results
print("\nMost Frequent IP Addresses:")
for ip, count in ip_count.most_common(5):
print(f"{ip}: {count}")
print("\nMost Common Status Codes:")
for status, count in status_count.most_common():
print(f"Status {status}: {count}")
print("\nMost Requested Pages:")
for request, count in request_count.most_common(5):
print(f"Page: {request}, Requests: {count}")
except FileNotFoundError:
print(f"Error: Log file {log_file_path} not found.")
except Exception as e:
print(f"An error occurred while analyzing the log file: {e}")
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: python log_analyzer.py <path_to_log_file>")
sys.exit(1)
log_file_path = sys.argv[1]
analyze_log_file(log_file_path)