add tsstats.log.re_log_entry for log-line-matching

* don't evaluate invalid log-lines leading to unexpected results
* don't abort parsing just because of an invalid character at the beginning/end
* add debug-output if line doesn't match
This commit is contained in:
Thor77 2016-05-24 22:30:16 +02:00
parent 42d8c74f72
commit da5683ede2
3 changed files with 20 additions and 13 deletions

View File

@ -54,9 +54,9 @@ author = 'Thor77'
# built documents. # built documents.
# #
# The short X.Y version. # The short X.Y version.
version = '0.4' version = '0.5'
# The full version, including alpha/beta/rc tags. # The full version, including alpha/beta/rc tags.
release = '0.4.1' release = '0.5.0'
# The language for content autogenerated by Sphinx. Refer to documentation # The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages. # for a list of supported languages.

View File

@ -2,7 +2,7 @@ from setuptools import setup
setup( setup(
name='tsstats', name='tsstats',
version='0.4.1', version='0.5.0',
author='Thor77', author='Thor77',
author_email='thor77@thor77.org', author_email='thor77@thor77.org',
description='A simple Teamspeak stats-generator', description='A simple Teamspeak stats-generator',

View File

@ -5,6 +5,9 @@ from glob import glob
from tsstats.client import Client, Clients from tsstats.client import Client, Clients
re_log_entry = re.compile('(?P<timestamp>\d{4}-\d\d-\d\d\ \d\d:\d\d:\d\d.\d+)'
'\|(?P<level>\w+)\ +\|(?P<component>\w+)'
'\|\ +(?P<sid>\d+)\|\ (?P<message>.*)')
re_dis_connect = re.compile(r"'(.*)'\(id:(\d*)\)") re_dis_connect = re.compile(r"'(.*)'\(id:(\d*)\)")
re_disconnect_invoker = re.compile( re_disconnect_invoker = re.compile(
r'invokername=(.*)\ invokeruid=(.*)\ reasonmsg' r'invokername=(.*)\ invokeruid=(.*)\ reasonmsg'
@ -28,28 +31,32 @@ def parse_log(log_path, ident_map=None, clients=None):
# process lines # process lines
logger.debug('Started parsing of %s', log_file.name) logger.debug('Started parsing of %s', log_file.name)
for line in log_file: for line in log_file:
parts = line.split('|') match = re_log_entry.match(line)
if not match:
logger.debug('No match: "%s"', line)
continue
match = match.groupdict()
log_format = '%Y-%m-%d %H:%M:%S.%f' log_format = '%Y-%m-%d %H:%M:%S.%f'
stripped_time = datetime.strptime(parts[0], log_format) stripped_time = datetime.strptime(match['timestamp'], log_format)
logdatetime = int((stripped_time - datetime(1970, 1, 1)) logdatetime = int((stripped_time - datetime(1970, 1, 1))
.total_seconds()) .total_seconds())
data = '|'.join(parts[4:]).strip() message = match['message']
if data.startswith('client'): if message.startswith('client'):
nick, clid = re_dis_connect.findall(data)[0] nick, clid = re_dis_connect.findall(message)[0]
client = clients.setdefault(clid, Client(clid, nick)) client = clients.setdefault(clid, Client(clid, nick))
client.nick = nick # set nick to display changes client.nick = nick # set nick to display changes
if data.startswith('client connected'): if message.startswith('client connected'):
client.connect(logdatetime) client.connect(logdatetime)
elif data.startswith('client disconnected'): elif message.startswith('client disconnected'):
client.disconnect(logdatetime) client.disconnect(logdatetime)
if 'invokeruid' in data: if 'invokeruid' in message:
re_disconnect_data = re_disconnect_invoker.findall( re_disconnect_data = re_disconnect_invoker.findall(
data) message)
invokernick, invokeruid = re_disconnect_data[0] invokernick, invokeruid = re_disconnect_data[0]
invoker = clients.setdefault(invokeruid, invoker = clients.setdefault(invokeruid,
Client(invokeruid)) Client(invokeruid))
invoker.nick = invokernick invoker.nick = invokernick
if 'bantime' in data: if 'bantime' in message:
invoker.ban(client) invoker.ban(client)
else: else:
invoker.kick(client) invoker.kick(client)