2016-06-07 17:42:53 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-05-08 21:32:37 +02:00
|
|
|
import argparse
|
|
|
|
import json
|
2016-05-10 22:50:34 +02:00
|
|
|
import logging
|
2016-05-08 21:32:37 +02:00
|
|
|
from os.path import abspath, exists
|
|
|
|
|
2016-08-06 21:36:17 +02:00
|
|
|
from tsstats import config
|
2016-05-25 20:14:59 +02:00
|
|
|
from tsstats.exceptions import InvalidConfiguration
|
2016-05-08 21:32:37 +02:00
|
|
|
from tsstats.log import parse_logs
|
|
|
|
from tsstats.template import render_template
|
|
|
|
|
2016-05-10 22:50:34 +02:00
|
|
|
logger = logging.getLogger('tsstats')
|
2016-05-08 21:32:37 +02:00
|
|
|
|
2016-05-10 22:50:34 +02:00
|
|
|
|
2016-05-11 20:21:09 +02:00
|
|
|
def cli():
|
|
|
|
parser = argparse.ArgumentParser(
|
2016-08-06 21:36:17 +02:00
|
|
|
description='A simple Teamspeak stats-generator, based on server-logs',
|
|
|
|
argument_default=argparse.SUPPRESS
|
2016-05-11 20:21:09 +02:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2016-05-21 22:02:27 +02:00
|
|
|
'-c', '--config',
|
2016-05-21 22:06:32 +02:00
|
|
|
type=str, help='path to config'
|
2016-05-11 20:21:09 +02:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2016-05-21 22:06:32 +02:00
|
|
|
'--idmap', type=str, help='path to id_map'
|
2016-05-11 20:21:09 +02:00
|
|
|
)
|
2016-05-21 21:35:42 +02:00
|
|
|
parser.add_argument(
|
2016-05-21 22:02:27 +02:00
|
|
|
'-l', '--log',
|
|
|
|
type=str, help='path to your logfile(s)'
|
2016-05-21 21:35:42 +02:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2016-05-21 22:02:27 +02:00
|
|
|
'-o', '--output',
|
2016-09-22 15:28:42 +02:00
|
|
|
type=str, help='path to the output-file'
|
2016-05-21 21:35:42 +02:00
|
|
|
)
|
2016-05-11 20:21:09 +02:00
|
|
|
parser.add_argument(
|
2016-05-21 22:02:27 +02:00
|
|
|
'-d', '--debug',
|
|
|
|
help='debug mode', action='store_true'
|
2016-05-11 20:21:09 +02:00
|
|
|
)
|
2016-06-23 21:42:03 +02:00
|
|
|
parser.add_argument(
|
|
|
|
'-nod', '--noonlinedc',
|
2016-06-25 20:37:58 +02:00
|
|
|
help='don\'t add connect until now to onlinetime',
|
|
|
|
action='store_false', dest='onlinedc'
|
2016-06-23 21:42:03 +02:00
|
|
|
)
|
2016-08-10 22:44:48 +02:00
|
|
|
parser.add_argument(
|
|
|
|
'-t', '--template',
|
|
|
|
type=str, help='path to custom template'
|
|
|
|
)
|
2016-09-17 22:41:38 +02:00
|
|
|
parser.add_argument(
|
|
|
|
'-dtf', '--datetimeformat',
|
|
|
|
type=str, help='format of date/time-values (datetime.strftime)'
|
|
|
|
)
|
2016-08-06 21:36:17 +02:00
|
|
|
options = parser.parse_args()
|
|
|
|
if 'config' in options:
|
|
|
|
configuration = config.load(options.config)
|
|
|
|
else:
|
|
|
|
configuration = config.load()
|
|
|
|
for option, value in vars(options).items():
|
2016-08-08 22:25:57 +02:00
|
|
|
configuration.set('General', option, str(value))
|
2016-08-06 21:36:17 +02:00
|
|
|
main(configuration)
|
2016-05-11 20:21:09 +02:00
|
|
|
|
|
|
|
|
2016-08-06 21:36:17 +02:00
|
|
|
def main(configuration):
|
|
|
|
if configuration.getboolean('General', 'debug'):
|
2016-05-21 23:01:45 +02:00
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
2016-08-06 21:36:17 +02:00
|
|
|
idmap = configuration.get('General', 'idmap')
|
2016-05-21 22:20:15 +02:00
|
|
|
if idmap:
|
|
|
|
idmap = abspath(idmap)
|
|
|
|
if not exists(idmap):
|
|
|
|
logger.fatal('identmap not found (%s)', idmap)
|
2016-05-08 21:32:37 +02:00
|
|
|
# read id_map
|
2016-05-21 22:20:15 +02:00
|
|
|
identmap = json.load(open(idmap))
|
2016-05-08 21:32:37 +02:00
|
|
|
else:
|
2016-05-21 22:20:15 +02:00
|
|
|
identmap = None
|
|
|
|
|
2016-08-06 21:36:17 +02:00
|
|
|
log = configuration.get('General', 'log')
|
|
|
|
if not log:
|
2016-05-25 20:14:59 +02:00
|
|
|
raise InvalidConfiguration('log or output missing')
|
2016-05-08 21:32:37 +02:00
|
|
|
|
2016-08-06 21:36:17 +02:00
|
|
|
sid_clients = parse_logs(
|
|
|
|
log, ident_map=identmap,
|
|
|
|
online_dc=configuration.getboolean('General', 'onlinedc')
|
|
|
|
)
|
2016-06-22 20:47:12 +02:00
|
|
|
for sid, clients in sid_clients.items():
|
2016-08-09 20:37:20 +02:00
|
|
|
if sid and len(sid_clients) > 1:
|
2016-06-22 20:47:12 +02:00
|
|
|
ext = '.{}'.format(sid)
|
|
|
|
else:
|
|
|
|
ext = ''
|
2016-08-06 21:36:17 +02:00
|
|
|
render_template(
|
|
|
|
clients,
|
2016-08-10 22:48:12 +02:00
|
|
|
output=abspath(configuration.get('General', 'output') + ext),
|
2016-09-17 22:41:38 +02:00
|
|
|
template_path=configuration.get('General', 'template'),
|
|
|
|
datetime_fmt=configuration.get('General', 'datetimeformat')
|
2016-08-06 21:36:17 +02:00
|
|
|
)
|
2016-05-08 21:32:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-05-11 20:21:09 +02:00
|
|
|
cli()
|