43 lines
1.8 KiB
Python
Executable File
43 lines
1.8 KiB
Python
Executable File
#!/usr/bin/python3
|
|
# Entrouvert 2019
|
|
# Exim log parser
|
|
# See Summary of Fields in Log Lines in https://www.exim.org/exim-html-current/doc/html/spec_html/ch-log_files.html
|
|
import socket
|
|
import re
|
|
import datetime
|
|
import pytz
|
|
from django.utils import timezone
|
|
|
|
from logtracker.agent.agent import tail
|
|
|
|
host = socket.getfqdn()
|
|
paris = pytz.timezone('Europe/Paris')
|
|
patterns = {'ignore': re.compile('([\d-]+) ([\d:]+) .*(Start queue run|End queue run|daemon started|relay not permitted|Spool file is locked|Connection refused|Connection timed out|no immediate delivery|error ignored|Greylisting in action|Remote host closed connection|No route to host|SMTP error|SMTP protocol error|SMTP protocol synchronization error|SMTP command timeout|no host name found|unexpected disconnection|TLS error|log string overflowed|cancelled by timeout).*'),
|
|
'match': re.compile('(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d) ([\w\d\-]+) (<=|=>|->|==|\*\*|Completed|SMTP error|Message is frozen|Frozen|Unfrozen)\s*(.*)$'),
|
|
}
|
|
|
|
|
|
def parse_date(string):
|
|
stamp = datetime.datetime.strptime(string, '%Y-%m-%d %H:%M:%S')
|
|
return timezone.make_aware(stamp, paris)
|
|
|
|
|
|
def parse_line(line):
|
|
match = re.match(patterns['match'], line)
|
|
if match:
|
|
stamp, identifier, action, raw = match.groups()
|
|
stamp = parse_date(stamp)
|
|
data = {'raw': '%s %s' % (action, raw[:511].replace("'", '')), 'identifier': identifier}
|
|
return {'host': host, 'service': 'exim', 'timestamp': stamp, 'priority': 6, 'data': data}
|
|
else:
|
|
match = re.match(patterns['ignore'], line)
|
|
if not match:
|
|
print('Failed to parse line: %s' % line)
|
|
|
|
|
|
def main():
|
|
for line in tail('/var/log/exim4/mainlog'):
|
|
match = parse_line(line)
|
|
if match:
|
|
yield match
|