2016-06-07 11:42:53 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-05-08 15:32:37 -04:00
|
|
|
import argparse
|
|
|
|
import json
|
2016-05-10 16:50:34 -04:00
|
|
|
import logging
|
2016-05-08 15:32:37 -04:00
|
|
|
from os.path import abspath, exists
|
|
|
|
|
|
|
|
from tsstats.config import parse_config
|
2016-05-25 14:14:59 -04:00
|
|
|
from tsstats.exceptions import InvalidConfiguration
|
2016-05-08 15:32:37 -04:00
|
|
|
from tsstats.log import parse_logs
|
|
|
|
from tsstats.template import render_template
|
|
|
|
|
2016-05-10 16:50:34 -04:00
|
|
|
logger = logging.getLogger('tsstats')
|
2016-05-08 15:32:37 -04:00
|
|
|
|
2016-05-10 16:50:34 -04:00
|
|
|
|
2016-05-11 14:21:09 -04:00
|
|
|
def cli():
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='A simple Teamspeak stats-generator - based on server-logs'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2016-05-21 16:02:27 -04:00
|
|
|
'-c', '--config',
|
2016-05-21 16:06:32 -04:00
|
|
|
type=str, help='path to config'
|
2016-05-11 14:21:09 -04:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2016-05-21 16:06:32 -04:00
|
|
|
'--idmap', type=str, help='path to id_map'
|
2016-05-11 14:21:09 -04:00
|
|
|
)
|
2016-05-21 15:35:42 -04:00
|
|
|
parser.add_argument(
|
2016-05-21 16:02:27 -04:00
|
|
|
'-l', '--log',
|
|
|
|
type=str, help='path to your logfile(s)'
|
2016-05-21 15:35:42 -04:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
2016-05-21 16:02:27 -04:00
|
|
|
'-o', '--output',
|
|
|
|
type=str, help='path to the output-file', default='stats.html'
|
2016-05-21 15:35:42 -04:00
|
|
|
)
|
2016-05-11 14:21:09 -04:00
|
|
|
parser.add_argument(
|
2016-05-21 16:02:27 -04:00
|
|
|
'-d', '--debug',
|
|
|
|
help='debug mode', action='store_true'
|
2016-05-11 14:21:09 -04:00
|
|
|
)
|
2016-06-23 15:42:03 -04:00
|
|
|
parser.add_argument(
|
|
|
|
'-nod', '--noonlinedc',
|
|
|
|
help='don\'t add connect until now to onlinetime', action='store_false'
|
|
|
|
)
|
2016-05-11 14:21:09 -04:00
|
|
|
args = parser.parse_args()
|
2016-05-21 16:20:15 -04:00
|
|
|
main(**vars(args))
|
2016-05-11 14:21:09 -04:00
|
|
|
|
|
|
|
|
2016-06-23 15:42:03 -04:00
|
|
|
def main(config=None, idmap=None, log=None,
|
|
|
|
output=None, debug=False, noonlinedc=True):
|
2016-05-21 17:01:45 -04:00
|
|
|
if debug:
|
|
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
|
2016-05-21 16:20:15 -04:00
|
|
|
if config:
|
|
|
|
config = abspath(config)
|
|
|
|
if not exists(config):
|
|
|
|
logger.fatal('config not found (%s)', config)
|
2016-06-25 14:34:17 -04:00
|
|
|
idmap, log, output, debug, noonlinedc = parse_config(config)
|
2016-05-21 17:01:45 -04:00
|
|
|
if debug:
|
|
|
|
logger.setLevel(logging.DEBUG)
|
2016-05-08 15:32:37 -04:00
|
|
|
|
2016-05-21 16:20:15 -04:00
|
|
|
if idmap:
|
|
|
|
idmap = abspath(idmap)
|
|
|
|
if not exists(idmap):
|
|
|
|
logger.fatal('identmap not found (%s)', idmap)
|
2016-05-08 15:32:37 -04:00
|
|
|
# read id_map
|
2016-05-21 16:20:15 -04:00
|
|
|
identmap = json.load(open(idmap))
|
2016-05-08 15:32:37 -04:00
|
|
|
else:
|
2016-05-21 16:20:15 -04:00
|
|
|
identmap = None
|
|
|
|
|
|
|
|
if not log or not output:
|
2016-05-25 14:14:59 -04:00
|
|
|
raise InvalidConfiguration('log or output missing')
|
2016-05-08 15:32:37 -04:00
|
|
|
|
2016-06-24 15:41:27 -04:00
|
|
|
sid_clients = parse_logs(log, ident_map=identmap, online_dc=noonlinedc)
|
2016-06-22 14:47:12 -04:00
|
|
|
for sid, clients in sid_clients.items():
|
|
|
|
if sid:
|
|
|
|
ext = '.{}'.format(sid)
|
|
|
|
else:
|
|
|
|
ext = ''
|
|
|
|
render_template(clients, output=abspath(output + ext))
|
2016-05-08 15:32:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2016-05-11 14:21:09 -04:00
|
|
|
cli()
|