2016-05-08 15:32:37 -04:00
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
from datetime import datetime
|
|
|
|
from glob import glob
|
|
|
|
|
2016-05-18 16:42:42 -04:00
|
|
|
from tsstats.client import Client, Clients
|
2016-05-08 15:32:37 -04:00
|
|
|
|
|
|
|
re_dis_connect = re.compile(r"'(.*)'\(id:(\d*)\)")
|
|
|
|
re_disconnect_invoker = re.compile(
|
|
|
|
r'invokername=(.*)\ invokeruid=(.*)\ reasonmsg'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-05-10 16:50:34 -04:00
|
|
|
logger = logging.getLogger('tsstats')
|
|
|
|
|
|
|
|
|
2016-05-19 15:46:22 -04:00
|
|
|
def parse_logs(log_path, ident_map=None):
|
2016-05-08 15:32:37 -04:00
|
|
|
clients = Clients(ident_map)
|
|
|
|
|
|
|
|
# find all log-files and open them TODO: move this into main
|
|
|
|
file_paths = sorted([file_path for file_path in glob(log_path)])
|
|
|
|
|
|
|
|
for file_path in file_paths:
|
|
|
|
log_file = open(file_path)
|
|
|
|
# process lines
|
2016-05-11 14:45:42 -04:00
|
|
|
logger.debug('Started parsing of %s', log_file.name)
|
2016-05-08 15:32:37 -04:00
|
|
|
for line in log_file:
|
|
|
|
parts = line.split('|')
|
|
|
|
log_format = '%Y-%m-%d %H:%M:%S.%f'
|
|
|
|
stripped_time = datetime.strptime(parts[0], log_format)
|
2016-05-08 15:54:58 -04:00
|
|
|
logdatetime = int((stripped_time - datetime(1970, 1, 1))
|
|
|
|
.total_seconds())
|
2016-05-08 15:32:37 -04:00
|
|
|
data = '|'.join(parts[4:]).strip()
|
|
|
|
if data.startswith('client'):
|
|
|
|
nick, clid = re_dis_connect.findall(data)[0]
|
2016-05-19 15:53:05 -04:00
|
|
|
client = clients.setdefault(clid, Client(clid, nick))
|
2016-05-19 15:59:34 -04:00
|
|
|
client.nick = nick # set nick to display changes
|
2016-05-08 15:32:37 -04:00
|
|
|
if data.startswith('client connected'):
|
|
|
|
client.connect(logdatetime)
|
|
|
|
elif data.startswith('client disconnected'):
|
|
|
|
client.disconnect(logdatetime)
|
|
|
|
if 'invokeruid' in data:
|
|
|
|
re_disconnect_data = re_disconnect_invoker.findall(
|
|
|
|
data)
|
|
|
|
invokernick, invokeruid = re_disconnect_data[0]
|
2016-05-18 16:42:42 -04:00
|
|
|
invoker = clients.setdefault(invokeruid,
|
|
|
|
Client(invokeruid))
|
2016-05-08 15:32:37 -04:00
|
|
|
invoker.nick = invokernick
|
|
|
|
if 'bantime' in data:
|
|
|
|
invoker.ban(client)
|
|
|
|
else:
|
|
|
|
invoker.kick(client)
|
2016-05-11 14:45:42 -04:00
|
|
|
logger.debug('Finished parsing of %s', log_file.name)
|
2016-05-08 15:32:37 -04:00
|
|
|
return clients
|