-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog_parser.py
More file actions
169 lines (148 loc) · 5.95 KB
/
Copy pathlog_parser.py
File metadata and controls
169 lines (148 loc) · 5.95 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
import re
import gzip
from datetime import datetime
import pandas as pd
import streamlit as st
@st.cache_data
def parse_nginx_log(log_path):
ip_regex = re.compile(r"^\d{1,3}(\.\d{1,3}){3}$|^[a-fA-F0-9:]+$") # IPv4 or IPv6
try:
with open(log_path, 'r') as f:
lines = f.readlines()
parsed_data = []
for line in lines:
try:
quoted = re.findall(r'"([^"]*)"', line)
bracketed = re.findall(r'\[([^\]]+)\]', line)
if len(quoted) < 4 or len(bracketed) < 1:
continue
time_str = bracketed[0]
request = quoted[0]
referrer = quoted[1]
user_agent = quoted[2]
proxy_chain = quoted[3]
status_match = re.search(r'"\s*(\d{3})\s+(\d+)\s', line)
if status_match:
status = int(status_match.group(1))
size = int(status_match.group(2))
else:
status = None
size = None
req_time_match = re.search(r'" ([\d\.]+) "', line)
req_time = req_time_match.group(1) if req_time_match else None
method, path, protocol = ('', '', '')
req_parts = request.split()
if len(req_parts) == 3:
method, path, protocol = req_parts
try:
time = datetime.strptime(time_str, '%d/%b/%Y:%H:%M:%S %z')
except Exception:
time = None
real_ip = proxy_chain.split(',')[0].strip() if proxy_chain else '-'
ip = real_ip if ip_regex.match(real_ip) else '-'
parsed_data.append({
'ip': ip,
'time': time,
'method': method,
'path': path,
'protocol': protocol,
'status': status,
'size': size,
'referrer': referrer,
'user_agent': user_agent,
'req_time': req_time,
'proxy_chain': proxy_chain
})
except Exception as e:
continue
df = pd.DataFrame(parsed_data)
if not df.empty:
df['status'] = pd.to_numeric(df['status'], errors='coerce')
return df
except Exception as e:
st.warning(f"Couldn't parse {log_path}: {str(e)}")
return pd.DataFrame()
@st.cache_data
def parse_compressed_nginx_log(gz_path):
"""Parse a gzipped nginx log file"""
ip_regex = re.compile(r"^\d{1,3}(\.\d{1,3}){3}$|^[a-fA-F0-9:]+$") # IPv4 or IPv6
try:
with gzip.open(gz_path, 'rt', encoding='utf-8', errors='ignore') as f:
lines = f.readlines()
parsed_data = []
for line in lines:
try:
quoted = re.findall(r'"([^"]*)"', line)
bracketed = re.findall(r'\[([^\]]+)\]', line)
if len(quoted) < 4 or len(bracketed) < 1:
continue
time_str = bracketed[0]
request = quoted[0]
referrer = quoted[1]
user_agent = quoted[2]
proxy_chain = quoted[3]
status_match = re.search(r'"\s*(\d{3})\s+(\d+)\s', line)
if status_match:
status = int(status_match.group(1))
size = int(status_match.group(2))
else:
status = None
size = None
req_time_match = re.search(r'" ([\d\.]+) "', line)
req_time = req_time_match.group(1) if req_time_match else None
method, path, protocol = ('', '', '')
req_parts = request.split()
if len(req_parts) == 3:
method, path, protocol = req_parts
try:
time = datetime.strptime(time_str, '%d/%b/%Y:%H:%M:%S %z')
except Exception:
time = None
real_ip = proxy_chain.split(',')[0].strip() if proxy_chain else '-'
ip = real_ip if ip_regex.match(real_ip) else '-'
parsed_data.append({
'ip': ip,
'time': time,
'method': method,
'path': path,
'protocol': protocol,
'status': status,
'size': size,
'referrer': referrer,
'user_agent': user_agent,
'req_time': req_time,
'proxy_chain': proxy_chain
})
except Exception as e:
continue
df = pd.DataFrame(parsed_data)
if not df.empty:
df['status'] = pd.to_numeric(df['status'], errors='coerce')
return df
except Exception as e:
st.warning(f"Couldn't parse {gz_path}: {str(e)}")
return pd.DataFrame()
@st.cache_data
def parse_php_error_log(log_path):
pattern = re.compile(r'\[(.*?)\]\s+PHP\s+([A-Za-z ]+):\s*(.*)')
parsed = []
with open(log_path, 'r', encoding='utf-8', errors='replace') as f:
for line in f:
m = pattern.match(line)
if m:
time_str, error_type, message = m.groups()
error_type = error_type.strip().lower()
if error_type == "fatal error":
error_type_label = "Fatal Error (Critical)"
elif error_type == "warning":
error_type_label = "Warning"
elif error_type == "notice":
error_type_label = "Info"
else:
error_type_label = error_type.capitalize()
parsed.append({
'time': time_str,
'type': error_type_label,
'message': message
})
return pd.DataFrame(parsed)