Read in and extract fields from logs

This commit is contained in:
Brian Read 2024-05-29 10:15:23 +01:00
parent eefac0a502
commit 02deabb6af

View File

@ -8,7 +8,11 @@
# and html output added # and html output added
# #
import datetime import datetime
import datetime import sys
from chameleon import PageTemplateFile,PageTemplate
import pkg_resources
Mailstats_version = '1.2'
def truncate_microseconds(timestamp): def truncate_microseconds(timestamp):
# Split timestamp into main part and microseconds # Split timestamp into main part and microseconds
@ -40,12 +44,13 @@ def read_and_filter_yesterday_log(file_path):
log_entries = [] log_entries = []
with open(file_path, 'r') as file: with open(file_path, 'r') as file:
for line in file: for line in file:
parts = line.strip().split() if '`' in line:
if parts: parts = line.split(' ')
# Combine parts to form the complete timestamp if parts:
timestamp = ' '.join(parts[:2]) # Combine parts to form the complete timestamp
data = ' '.join(parts[2:]) # The rest of the line after date and time timestamp = ' '.join(parts[:2])
log_entries.append((timestamp, data)) data = ' '.join(parts[2:]) # The rest of the line after date and time
log_entries.append((timestamp, data))
# Filter the entries to keep only those from yesterday # Filter the entries to keep only those from yesterday
filtered_entries = filter_yesterdays_entries(log_entries) filtered_entries = filter_yesterdays_entries(log_entries)
@ -61,38 +66,68 @@ def read_and_filter_yesterday_log(file_path):
def parse_data(data): def parse_data(data):
# Split data string into parts and map to named fields. # Split data string into parts and map to named fields.
# Adjust the field names and parsing logic according to your data format. # Adjust the field names and parsing logic according to your data format.
# Split at the backtick - before it fields split at space, after, fields split at tab
parts = data.split('`') parts = data.split('`')
fields = parts[1].strip().split('\t') if len(parts) > 1 else [] #print(parts[0],parts[1])
# Example mapping: fields1 = parts[0].strip().split() if len(parts) > 0 else []
return { fields2 = parts[1].split('\t') if len(parts) > 1 else []
'id': fields[0], # then merge them
'action': fields[1] if len(fields) > 0 else None, fields = fields1 + fields2
'logterse': fields[2] if len(fields) > 1 else None, # if fields[8] != 'queued':
'reversequote': fields[3] if len(fields) > 2 else None, # i = 0
'ip': fields[4] if len(fields) > 3 else None, # print(f"len:{len(fields)}")
'sendurl': fields[5] if len(fields) > 4 else None, # for part in fields:
'sendurl1': fields[6] if len(fields) > 5 else None, # print(f"{i}: {part}")
'error-plugin': fields[6] if len(fields) > 5 else None, # i = i +1
'from-email': fields[7] if len(fields) > 6 else None, # quit()
'error-reason': fields[7] if len(fields) > 6 else None, # and mapping:
'to-email': fields[8] if len(fields) > 7 else None, try:
'action1': fields[9] if len(fields) > 8 else None, return_dict = {
'sendurl2': fields[10] if len(fields) > 9 else None, 'id': fields[0] if len(fields) > 0 else None,
'spam-yes-no': fields[11] if len(fields) > 10 else None, 'action': fields[1] if len(fields) > 1 else None,
'spam-score': fields[12] if len(fields) > 11 else None, 'logterse': fields[2] if len(fields) > 2 else None,
'spam-score-reqd': fields[13] if len(fields) > 12 else None, 'ip': fields[3] if len(fields) > 3 else None,
'autolearn': fields[14] if len(fields) > 13 else None, 'sendurl': fields[4] if len(fields) > 4 else None,
'logterse': fields[15] if len(fields) > 14 else None, 'sendurl1': fields[5] if len(fields) > 5 else None,
'logterse': fields[16] if len(fields) > 15 else None 'from-email': fields[6] if len(fields) > 6 else None,
# Add more fields as necessary 'error-reason': fields[6] if len(fields) > 6 else None,
} 'to-email': fields[7] if len(fields) > 7 else None,
'error-plugin': fields[8] if len(fields) > 8 else None,
'action1': fields[8] if len(fields) > 8 else None,
# Example usage 'error-number' : fields[9] if len(fields) > 9 else None,
sorted_log_dict = read_and_filter_yesterday_log('/home/brianr/SME11Build/GITFiles/smecontribs/smeserver-mailstats/current.log') 'sender': fields[10] if len(fields) > 10 else None,
#print(sorted_log_dict) 'error-msg' :fields[10] if len(fields) > 10 else None,
'spam-status': fields[11] if len(fields) > 11 else None,
for timestamp, data in sorted_log_dict.items(): 'error-result': fields[11] if len(fields) > 11 else None,
print(f"{timestamp} IP = {data['ip']}") # Add more fields as necessary
}
except:
#print(f"error:len:{len(fields)}")
return_dict = {}
return return_dict
if __name__ == "__main__":
try:
chameleon_version = pkg_resources.get_distribution("Chameleon").version
except pkg_resources.DistributionNotFound:
chameleon_version = "Version information not available"
python_version = sys.version
python_version = python_version[:8]
current_datetime = datetime.datetime.now()
formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M")
hello_string = "Mailstats version:"+Mailstats_version+" Chameleon version:"+chameleon_version+" On Python:"+python_version+" at "+formatted_datetime
print(hello_string)
sorted_log_dict = read_and_filter_yesterday_log('/home/brianr/SME11Build/GITFiles/smecontribs/smeserver-mailstats/current.log')
#print(sorted_log_dict)
i = 1
for timestamp, data in sorted_log_dict.items():
if data['action'] == '(deny)':
error = data['error-plugin']
msg = data['error-msg']
else:
error = ""
msg = ""
print(f"{i}: {timestamp} IP = {data['ip']} Result:{data['action']} {error} {msg}" )
i = i + 1