-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lfd-rate.py
executable file
·75 lines (63 loc) · 2.31 KB
/
lfd-rate.py
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
#!/usr/bin/env python3
import os
import re
import datetime
debug = False
log_file_path = '/var/log/lfd.log'
def parse_timestamp(line):
pattern = r'(\w{3}\s+\d{1,2}\s+\d{2}:\d{2}:\d{2})'
match = re.search(pattern, line)
if match:
timestamp = datetime.datetime.strptime(match.group(1), '%b %d %H:%M:%S')
current_year = datetime.datetime.now().year
timestamp = timestamp.replace(year=current_year)
return timestamp
return None
def calculate_lfd_rate(log_file_path, debug=False):
if not os.path.exists(log_file_path):
print("Log file not found.")
return
with open(log_file_path, 'r') as log_file:
lines = log_file.readlines()
lfd_counts = {
'second': {},
'minute': {},
'hour': {},
'day': {}
}
line_count = 0
matched_line_count = 0
for line in lines:
line_count += 1
if re.search(r'\bSSH login\b', line) or re.search(r'\bBlocked in csf\b', line):
matched_line_count += 1
if debug:
print(f"Matched line {matched_line_count}: {line.strip()}")
timestamp = parse_timestamp(line)
if timestamp:
for unit in lfd_counts:
if unit == 'second':
key_format = '%Y-%m-%d %H:%M:%S'
elif unit == 'minute':
key_format = '%Y-%m-%d %H:%M'
elif unit == 'hour':
key_format = '%Y-%m-%d %H'
else: # day
key_format = '%Y-%m-%d'
unit_key = timestamp.strftime(key_format)
if unit_key not in lfd_counts[unit]:
lfd_counts[unit][unit_key] = 1
else:
lfd_counts[unit][unit_key] += 1
print(f"Total lines processed: {line_count}")
print(f"Total matched lines: {matched_line_count}")
for unit, counts in lfd_counts.items():
print(f"LFD actions per {unit}:")
if counts:
for unit_key, count in sorted(counts.items()):
print(f" {unit_key}: {count} lfd actions")
else:
print(f" No actions found for {unit}")
print()
if __name__ == '__main__':
calculate_lfd_rate(log_file_path, debug=debug)