-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpPyLogstats.py
More file actions
334 lines (281 loc) · 14 KB
/
Copy pathhttpPyLogstats.py
File metadata and controls
334 lines (281 loc) · 14 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import os
import sys
import re
import time
import datetime
import yaml
import argparse
import pandas as pd
host = r'^(?P<host>.*?)'
space = r'\s'
dash = r'\S+'
user = r'(?P<user>\S+)'
reqtime = r'\[(?P<time>.*?)\]'
method = r'\"(?P<method>\S+)'
section = r'(?P<section>/\w+)'
subsection = r'(?P<subsection>/\w+)?'
protocol = r'(?P<protocol>\S+)?\"'
status = r'(?P<status>\d{3})'
size = r'(?P<size>\S+)'
http_re_match = host + space + dash + space + user + space + reqtime + space + method + space + section + subsection + space + protocol + space + status + space + size
log_stats_df = pd.DataFrame(columns=["date_time","host","user_id","method","section","subsection","protocol","response_code","content_size"])
alerts_df = pd.DataFrame(columns=["date_start", "date_end", "type", "value"])
# Available configuration used as global variable
config = {
'logfile': '/var/log/access.log',
'stats_period': 10,
'stats_refresh': 10,
'alerts': {
'high_traffic': {
'enabled': True,
'limit_period': 120,
'limit_value': 10
}
}
}
def load_config(config_file_path):
"""
Load configuration from file
Loaded configuration can only match known configuration structure (see global default config variable)
If file path None or not found, exit program
:param config_file_path: Path to config file
"""
global config
try:
config_file_path = os.path.abspath(config_file_path)
assert config_file_path
with open(file=config_file_path) as yaml_data:
loaded_config = yaml.safe_load(yaml_data)
for k in config:
if k in loaded_config:
config[k] = loaded_config[k]
except AssertionError:
print(f"Config file {config_file_path} not found or unreadable ! Exiting..")
quit(1)
def log_parser(logline, http_re_match):
"""
Parse log line with a regex
If log line matches, insert logline into a global dataframe
If log line does not match, print warning
:param logline: HTTP formated log line (https://www.w3.org/Daemon/User/Config/Logging.html)
:param http_re_match: Regex that validate and split the log line into groups
"""
global log_stats_df
match = re.search(http_re_match, logline)
if match is not None:
# Need to format HTTP log time into timestamp for comparisons
date_time = time.mktime(datetime.datetime.strptime(match.group('time'), '%d/%b/%Y:%H:%M:%S +%f').timetuple())
# Insert every group in case we want to show some more stats one day
log_stat_df = pd.DataFrame({"date_time": [date_time],
"host": [match.group('host')],
"user_id": [match.group('user')],
"method": [match.group('method')],
"section": [match.group('section')],
"subsection": [match.group('subsection')],
"protocol": [match.group('protocol')],
"response_code": [match.group('status')],
"content_size": [match.group('size')]
}, index=[date_time]
)
log_stats_df = pd.concat([log_stats_df,log_stat_df])
else:
print(f"WARNING: Unmatched log line '{logline}'")
def alert_high_traffic(alert_high_traffic_config, log_stats_df, alerts_df):
"""
Manages alerts for high traffic, aka request per sec from a http logs dataframe
Alert configuration is based on 3 parameters :
- If alert is enabled/disabled
- Hit rate limit value
- Hit rate limit period
If hit rate in log dataframe > hit rate limit during period, creates an high traffic to alert dataframe
If high traffic alert active and hit rate in log dataframe <= hit rate limit during period, marks high traffic alert as recovered
Returns an updated alert dataframe
:param alert_high_traffic_config: Dict representing high traffic alert configuration
:param log_stats_df: Dataframe of timestamped HTTP logs
:param alerts_df: Dataframe of timestamped monitored alerts
"""
limit_period = alert_high_traffic_config['limit_period']
limit_value = alert_high_traffic_config['limit_value']
now = time.time()
log_stats_df_subset = log_stats_df.loc[log_stats_df["date_time"] > now - limit_period ]
hit_rate = len(log_stats_df_subset) / limit_period
active_alerts = alerts_df.loc[alerts_df["date_end"].isnull()]
active_high_traffic_alert = active_alerts.loc[active_alerts["type"] == "High traffic"]
if active_high_traffic_alert.empty:
if hit_rate > limit_value:
active_alert = pd.DataFrame({"date_start": now,
"type": "High traffic",
"value_start": f"Hits = {hit_rate} per sec",
"value_end": None,
"date_end": None
}, index=[now])
alerts_df = pd.concat([alerts_df, active_alert])
else:
if hit_rate <= limit_value:
recovered_alert = pd.DataFrame({"date_end": now,
"type": "High traffic",
"value_start": active_high_traffic_alert["value_start"].item(),
"value_end": f"Hits = {hit_rate} per sec",
"date_start": active_high_traffic_alert["date_start"].item(),
}, index=[active_high_traffic_alert.index.item()])
alerts_df.update(recovered_alert)
return alerts_df
def startup():
"""
Starts up HTTPPyLogstats by :
- Loading default configuration
- Loading specific configuration (if passed as an arg)
- Printing main configuration
- Checking main configuration
"""
global config
default_config_file = './httpPyLogstats.yaml'
version = "1.0"
now = time.time()
date_time = datetime.datetime.fromtimestamp(now).strftime('%Y-%m-%d %H:%M:%S')
print('********************************')
print('HTTP LOGS STATISTICS STARTING UP')
print('********************************')
print(f'Version : {version}')
print(f'{date_time}')
print()
if sys.version_info[0] < 3:
raise Exception("Python 3 or a more recent version is required.")
load_config(config_file_path=default_config_file)
parser = argparse.ArgumentParser(description='HTTP Logs Python Statistics')
parser.add_argument("-c", "--config-file", help='Need config file path', dest="--config-file")
args = parser.parse_args().__dict__
if args['--config-file']:
load_config(config_file_path=args['--config-file'])
try:
assert config['logfile']
logfile = open(config['logfile'], "r")
assert logfile.readable()
logfile.close()
except AssertionError:
print(f"Log file not readable or configuration incorrect ! Exiting..")
quit(1)
print('Loaded configuration:')
print(f' Log file analysed : {config["logfile"]}')
try:
assert config["stats_refresh"]
assert config["stats_refresh"] != 0
assert int(config["stats_refresh"])
except AssertionError:
config["stats_refresh"] = 10
try:
assert config["stats_period"]
assert int(config["stats_period"])
except AssertionError:
config["stats_period"] = 10
print(f' Display statistics about the last {config["stats_period"]} seconds every {config["stats_refresh"]} seconds')
print()
print(f'Enabled alerts')
try:
assert config['alerts']['high_traffic']['enabled']
assert int(config["alerts"]["high_traffic"]["limit_value"])
assert int(config["alerts"]["high_traffic"]["limit_period"])
print(' [X] High traffic:')
print(f' Traffic > {config["alerts"]["high_traffic"]["limit_value"]} request per sec over {config["alerts"]["high_traffic"]["limit_period"]} seconds')
except AssertionError:
config['alerts']['high_traffic'] = {
'enabled': False
}
print(' [ ] High traffic')
pass
print()
print(f'Starting analysing {config["logfile"]}')
print(f'--------------------------------------')
def print_header(now):
"""
Prints stats title and timestamp
"""
global config
date_time = datetime.datetime.fromtimestamp(now).strftime('%Y-%m-%d %H:%M:%S')
print('*************************************')
print(f'HTTP LOGS STATISTICS - {date_time}')
def print_log_stats(now, stats_period_seconds, log_stats_df):
"""
Prints log stats from HTTP log dataframe during period
:param now: Timestamp to consider as period upper value
:param stats_period_seconds: Period for printed statistics
:param log_stats_df: HTTP logs dataframe
"""
print(f'### Statistics for the last {stats_period_seconds} seconds ###')
log_stats_df_subset = log_stats_df.loc[log_stats_df["date_time"] > now - stats_period_seconds]
log_stats_df_subset_2XX = log_stats_df_subset[log_stats_df_subset["response_code"].str.match(r'^2\d{2}$')]
log_stats_df_subset_3XX = log_stats_df_subset[log_stats_df_subset["response_code"].str.match(r'^3\d{2}$')]
log_stats_df_subset_4XX = log_stats_df_subset[log_stats_df_subset["response_code"].str.match(r'^4\d{2}$')]
log_stats_df_subset_5XX = log_stats_df_subset[log_stats_df_subset["response_code"].str.match(r'^5\d{2}$')]
log_stats_df_subset_sections = log_stats_df_subset["section"].value_counts().nlargest(3)
print(f' - Number of hits : {len(log_stats_df_subset)}')
print(f' - Number of GET : {len(log_stats_df_subset.loc[log_stats_df_subset["method"] == "GET"])}')
print(f' - Number of POST : {len(log_stats_df_subset.loc[log_stats_df_subset["method"] == "POST"])}')
print(f' - Number of 2XX : {len(log_stats_df_subset_2XX)}')
print(f' - Number of 3XX : {len(log_stats_df_subset_3XX)}')
print(f' - Number of 4XX : {len(log_stats_df_subset_4XX)}')
print(f' - Number of 5XX : {len(log_stats_df_subset_5XX)}')
print(f' - Total traffic : {pd.to_numeric(log_stats_df_subset["content_size"]).sum()}')
print(' - Top 3 sections : ')
i = 0
for section, hits in log_stats_df_subset_sections.iteritems():
i += 1
print(f' {i}. {section}: {hits} hits')
if i >= 3:
break
print(f'###############################################################')
def print_alerts_stats(now, stats_period_seconds, alerts_df):
"""
Prints alert stats from alerts dataframe during period
Alerts are divided into 4 categories : new alerts, active alerts, recovered alerts, archived alerts
:param now: Timestamp to consider as period upper value
:param stats_period_seconds: Period for printed statistics
:param alerts_df: alerts dataframe
"""
if not alerts_df.empty:
active_alerts = alerts_df.loc[alerts_df["date_end"].isnull()]
archived_alerts = alerts_df.loc[alerts_df["date_end"].notnull()]
new_alerts = active_alerts.loc[active_alerts["date_start"] > now - stats_period_seconds]
recovered_alerts = archived_alerts.loc[archived_alerts["date_end"] > now - stats_period_seconds ]
if len(new_alerts) > 0:
print(f'### New alerts during the last {stats_period_seconds} seconds###')
for index, row in new_alerts.iterrows():
date_time_start = datetime.datetime.fromtimestamp(row['date_start']).strftime('%Y-%m-%d %H:%M:%S')
print(f" - {row['type']} generated an alert - {row['value_start']}, triggered at {date_time_start}")
if len(recovered_alerts) > 0:
print(f'### Recovered alerts during the last {stats_period_seconds} seconds###')
for index, row in recovered_alerts.iterrows():
date_time_start = datetime.datetime.fromtimestamp(row['date_start']).strftime('%Y-%m-%d %H:%M:%S')
date_time_end = datetime.datetime.fromtimestamp(row['date_end']).strftime('%Y-%m-%d %H:%M:%S')
print(f" - {row['type']} triggered at {date_time_start} recovered at {date_time_end} - was {row['value_start']}, now {row['value_end']}")
if len(active_alerts) > 0:
print(f'### Active alerts ###')
for index, row in active_alerts.iterrows():
date_time = datetime.datetime.fromtimestamp(row['date_start']).strftime('%Y-%m-%d %H:%M:%S')
print(f" - Since {date_time} : {row['type']}")
if len(archived_alerts) > 0:
print(f'### Past alerts ###')
for index, row in archived_alerts.iterrows():
date_time_start = datetime.datetime.fromtimestamp(row['date_start']).strftime('%Y-%m-%d %H:%M:%S')
date_time_end = datetime.datetime.fromtimestamp(row['date_start']).strftime('%Y-%m-%d %H:%M:%S')
print(f" - {date_time_start} to {date_time_end} : {row['type']}")
else:
print(f'### No alert ###')
if __name__ == '__main__':
startup()
logfile = open(config['logfile'], "r")
before = time.time()
while logfile.readable():
now = time.time()
line = logfile.readline()
if line:
log_parser(line, http_re_match)
else:
if now - before > config['stats_refresh']:
if config['alerts']['high_traffic']['enabled']:
alerts_df = alert_high_traffic(config['alerts']['high_traffic'], log_stats_df=log_stats_df, alerts_df=alerts_df)
print_header(now=now)
print_log_stats(now=now, stats_period_seconds=config['stats_period'], log_stats_df=log_stats_df)
print_alerts_stats(now=now, stats_period_seconds=config['stats_period'], alerts_df=alerts_df)
before = time.time()
quit(0)