2016-06-07 11:42:53 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-05-08 15:32:37 -04:00
|
|
|
import logging
|
|
|
|
import re
|
2016-11-02 16:24:07 -04:00
|
|
|
from codecs import open
|
2016-06-19 16:10:35 -04:00
|
|
|
from collections import namedtuple
|
2016-05-08 15:32:37 -04:00
|
|
|
from datetime import datetime
|
|
|
|
from glob import glob
|
2016-06-19 16:10:35 -04:00
|
|
|
from os.path import basename
|
2016-05-08 15:32:37 -04:00
|
|
|
|
2016-05-18 16:42:42 -04:00
|
|
|
from tsstats.client import Client, Clients
|
2016-05-08 15:32:37 -04:00
|
|
|
|
2016-06-19 16:10:35 -04:00
|
|
|
re_log_filename = re.compile(r'ts3server_(?P<date>\d{4}-\d\d-\d\d)'
|
|
|
|
'__(?P<time>\d\d_\d\d_\d\d.\d+)_(?P<sid>\d).log')
|
2016-05-24 16:30:16 -04:00
|
|
|
re_log_entry = re.compile('(?P<timestamp>\d{4}-\d\d-\d\d\ \d\d:\d\d:\d\d.\d+)'
|
2016-06-07 10:51:55 -04:00
|
|
|
'\|\ *(?P<level>\w+)\ *\|\ *(?P<component>\w+)\ *'
|
|
|
|
'\|\ *(?P<sid>\d+)\ *\|\ *(?P<message>.*)')
|
2016-10-26 18:02:56 -04:00
|
|
|
re_dis_connect = re.compile(
|
2016-10-26 18:08:28 -04:00
|
|
|
r"client (?P<action>(dis)?connected) '(?P<nick>.*)'\(id:(?P<clid>\d+)\)")
|
2016-05-08 15:32:37 -04:00
|
|
|
re_disconnect_invoker = re.compile(
|
|
|
|
r'invokername=(.*)\ invokeruid=(.*)\ reasonmsg'
|
|
|
|
)
|
|
|
|
|
2016-05-25 14:08:48 -04:00
|
|
|
log_timestamp_format = '%Y-%m-%d %H:%M:%S.%f'
|
|
|
|
|
2016-06-19 16:10:35 -04:00
|
|
|
TimedLog = namedtuple('TimedLog', ['path', 'timestamp'])
|
|
|
|
|
2016-05-08 15:32:37 -04:00
|
|
|
|
2016-05-10 16:50:34 -04:00
|
|
|
logger = logging.getLogger('tsstats')
|
|
|
|
|
|
|
|
|
2016-11-03 17:40:57 -04:00
|
|
|
def parse_logs(log_glob, ident_map=None, online_dc=True, *args, **kwargs):
|
2016-06-19 16:26:19 -04:00
|
|
|
'''
|
|
|
|
parse logs from `log_glob`
|
2016-06-21 12:29:59 -04:00
|
|
|
|
|
|
|
:param log_glob: path to server-logs (supports globbing)
|
|
|
|
:param ident_map: identmap used for Client-initializations
|
|
|
|
|
|
|
|
:type log_glob: str
|
|
|
|
:type ident_map: dict
|
|
|
|
|
|
|
|
:return: clients bundled by virtual-server
|
|
|
|
:rtype: dict
|
2016-06-19 16:26:19 -04:00
|
|
|
'''
|
|
|
|
vserver_clients = {}
|
2016-06-20 16:02:55 -04:00
|
|
|
for virtualserver_id, logs in\
|
2016-11-06 13:18:21 -05:00
|
|
|
_bundle_logs(glob(log_glob)).items():
|
2016-06-21 12:20:57 -04:00
|
|
|
clients = Clients(ident_map)
|
2016-11-03 17:40:57 -04:00
|
|
|
# keep last log out of the iteration for now
|
|
|
|
for log in logs[:-1]:
|
|
|
|
# don't reconnect connected clients for all logs except last one
|
|
|
|
# because that would lead to insane onlinetimes
|
|
|
|
_parse_details(log.path, clients=clients, online_dc=False,
|
|
|
|
*args, **kwargs)
|
|
|
|
# now parse details of last log with correct online_dc set
|
|
|
|
_parse_details(logs[-1].path, clients=clients, online_dc=online_dc,
|
|
|
|
*args, **kwargs)
|
2016-06-19 16:26:19 -04:00
|
|
|
if len(clients) >= 1:
|
|
|
|
vserver_clients[virtualserver_id] = clients
|
|
|
|
return vserver_clients
|
|
|
|
|
|
|
|
|
2016-06-20 15:31:59 -04:00
|
|
|
def _bundle_logs(logs):
|
2016-05-30 14:23:03 -04:00
|
|
|
'''
|
2016-06-20 15:31:59 -04:00
|
|
|
bundle `logs` by virtualserver-id
|
|
|
|
and sort by timestamp from filename (if exists)
|
2016-06-19 15:37:00 -04:00
|
|
|
|
2016-06-20 15:31:59 -04:00
|
|
|
:param logs: list of paths to logfiles
|
2016-05-30 14:23:03 -04:00
|
|
|
|
2016-06-20 15:31:59 -04:00
|
|
|
:type logs: list
|
2016-05-30 14:23:03 -04:00
|
|
|
|
2016-06-20 15:31:59 -04:00
|
|
|
:return: `logs` bundled by virtualserver-id and sorted by timestamp
|
2016-06-19 16:21:02 -04:00
|
|
|
:rtype: dict{str: [TimedLog]}
|
2016-05-30 14:23:03 -04:00
|
|
|
'''
|
2016-06-19 16:10:35 -04:00
|
|
|
vserver_logfiles = {} # sid: [/path/to/log1, ..., /path/to/logn]
|
2016-06-20 15:31:59 -04:00
|
|
|
for log in logs:
|
2016-06-19 16:10:35 -04:00
|
|
|
# try to get date and sid from filename
|
2016-06-20 15:31:59 -04:00
|
|
|
match = re_log_filename.match(basename(log))
|
2016-06-19 16:10:35 -04:00
|
|
|
if match:
|
|
|
|
match = match.groupdict()
|
|
|
|
timestamp = datetime.strptime('{0} {1}'.format(
|
2016-06-20 15:59:46 -04:00
|
|
|
match['date'], match['time'].replace('_', ':')),
|
|
|
|
log_timestamp_format)
|
2016-06-20 15:31:59 -04:00
|
|
|
tl = TimedLog(log, timestamp)
|
2016-06-19 16:10:35 -04:00
|
|
|
sid = match['sid']
|
|
|
|
if sid in vserver_logfiles:
|
|
|
|
# if already exists, keep list sorted by timestamp
|
2016-06-20 15:59:46 -04:00
|
|
|
vserver_logfiles[sid].append(tl)
|
2016-06-19 16:10:35 -04:00
|
|
|
vserver_logfiles[sid] =\
|
|
|
|
sorted(vserver_logfiles[sid],
|
|
|
|
key=lambda tl: tl.timestamp)
|
|
|
|
else:
|
|
|
|
# if not exists, just create a list
|
|
|
|
vserver_logfiles[match['sid']] = [tl]
|
|
|
|
else:
|
|
|
|
# fallback to plain sorting
|
|
|
|
vserver_logfiles.setdefault('', [])\
|
2016-06-20 15:31:59 -04:00
|
|
|
.append(TimedLog(log, None))
|
2016-06-20 15:38:57 -04:00
|
|
|
vserver_logfiles[''] =\
|
|
|
|
sorted(vserver_logfiles[''],
|
|
|
|
key=lambda tl: tl.path)
|
2016-06-19 16:10:35 -04:00
|
|
|
return vserver_logfiles
|
2016-05-23 15:50:10 -04:00
|
|
|
|
2016-05-08 15:32:37 -04:00
|
|
|
|
2016-06-19 15:44:23 -04:00
|
|
|
def _parse_details(log_path, ident_map=None, clients=None, online_dc=True):
|
2016-05-30 14:23:03 -04:00
|
|
|
'''
|
2016-06-19 15:37:00 -04:00
|
|
|
extract details from log-files
|
|
|
|
|
|
|
|
detailed parsing is done here: onlinetime, kicks, pkicks, bans, pbans
|
2016-05-30 14:23:03 -04:00
|
|
|
|
|
|
|
:param log_path: path to log-file
|
2016-06-08 16:11:37 -04:00
|
|
|
:param ident_map: :doc:`identmap`
|
2016-05-30 14:23:03 -04:00
|
|
|
:param clients: clients-object to add parsing-results to
|
2016-06-12 10:54:49 -04:00
|
|
|
:param online_cd: disconnect online clients after parsing
|
2016-05-30 14:23:03 -04:00
|
|
|
|
|
|
|
:type log_path: str
|
|
|
|
:type ident_map: dict
|
|
|
|
:type clients: tsstats.client.Clients
|
2016-06-12 10:54:49 -04:00
|
|
|
:type online_cd: bool
|
2016-05-30 14:23:03 -04:00
|
|
|
|
|
|
|
:return: parsed clients
|
|
|
|
:rtype: tsstats.client.Clients
|
|
|
|
'''
|
2016-06-22 14:40:30 -04:00
|
|
|
if clients is None:
|
2016-05-24 15:48:11 -04:00
|
|
|
clients = Clients(ident_map)
|
2016-11-02 16:24:07 -04:00
|
|
|
log_file = open(log_path, encoding='utf-8')
|
2016-05-23 15:50:10 -04:00
|
|
|
# process lines
|
|
|
|
logger.debug('Started parsing of %s', log_file.name)
|
|
|
|
for line in log_file:
|
2016-05-24 16:30:16 -04:00
|
|
|
match = re_log_entry.match(line)
|
|
|
|
if not match:
|
|
|
|
logger.debug('No match: "%s"', line)
|
|
|
|
continue
|
|
|
|
match = match.groupdict()
|
2016-06-12 11:52:44 -04:00
|
|
|
logdatetime = datetime.strptime(match['timestamp'],
|
|
|
|
log_timestamp_format)
|
2016-05-24 16:30:16 -04:00
|
|
|
message = match['message']
|
|
|
|
if message.startswith('client'):
|
2016-10-26 18:02:56 -04:00
|
|
|
match = re_dis_connect.match(message)
|
|
|
|
if not match:
|
|
|
|
logger.debug('Not supported client action: "%s"', message)
|
|
|
|
continue
|
|
|
|
nick, clid = match.group('nick'), match.group('clid')
|
2016-05-23 15:50:10 -04:00
|
|
|
client = clients.setdefault(clid, Client(clid, nick))
|
|
|
|
client.nick = nick # set nick to display changes
|
2016-10-26 18:08:28 -04:00
|
|
|
action = match.group('action')
|
|
|
|
if action == 'connected':
|
2016-05-23 15:50:10 -04:00
|
|
|
client.connect(logdatetime)
|
2016-10-26 18:08:28 -04:00
|
|
|
elif action == 'disconnected':
|
2016-05-23 15:50:10 -04:00
|
|
|
client.disconnect(logdatetime)
|
2016-05-24 16:30:16 -04:00
|
|
|
if 'invokeruid' in message:
|
2016-05-23 15:50:10 -04:00
|
|
|
re_disconnect_data = re_disconnect_invoker.findall(
|
2016-05-24 16:30:16 -04:00
|
|
|
message)
|
2016-05-23 15:50:10 -04:00
|
|
|
invokernick, invokeruid = re_disconnect_data[0]
|
|
|
|
invoker = clients.setdefault(invokeruid,
|
|
|
|
Client(invokeruid))
|
|
|
|
invoker.nick = invokernick
|
2016-05-24 16:30:16 -04:00
|
|
|
if 'bantime' in message:
|
2016-05-23 15:50:10 -04:00
|
|
|
invoker.ban(client)
|
|
|
|
else:
|
|
|
|
invoker.kick(client)
|
2016-11-06 13:23:00 -05:00
|
|
|
elif message == 'stopped':
|
|
|
|
# make sure all clients are disconnected at server stop
|
|
|
|
[
|
|
|
|
client.disconnect(logdatetime)
|
|
|
|
for client in clients
|
|
|
|
if client.connected
|
|
|
|
]
|
2016-06-12 10:54:49 -04:00
|
|
|
if online_dc:
|
2016-11-04 15:03:02 -04:00
|
|
|
def _reconnect(client):
|
|
|
|
client.disconnect(datetime.utcnow())
|
|
|
|
client.connected += 1
|
|
|
|
[_reconnect(client) for client in clients if client.connected]
|
2016-05-23 15:50:10 -04:00
|
|
|
logger.debug('Finished parsing of %s', log_file.name)
|
2016-05-08 15:32:37 -04:00
|
|
|
return clients
|