ID-Mapping and sections-configuration

This commit is contained in:
Thor77 2015-05-12 21:38:34 +02:00
parent dc91b8fc59
commit e86d2154bc
3 changed files with 68 additions and 19 deletions

View File

@ -8,21 +8,39 @@ Run `tsstats.py` and point your web-server to the generated .html-file, now you
###Configname ###Configname
`config.ini` `config.ini`
###Sections #### Keys
`[General]` `[General]`
### Keys
- title `HTMl-Title`
- logfile `Path to TS3Server-logfile` - logfile `Path to TS3Server-logfile`
- outputfile `Path to the location, where the generator will put the generated .html-file` - outputfile `Path to the location, where the generator will put the generated .html-file`
`[HTML]`
- title `HTML-Title`
- onlinetime `Show the onlinetime-section (default=True)`
- kicks `Show the kicks-section (default=True)`
- pkicks `Show the passive-kicks-section (default=True)`
- bans `Show the bans-section (default=True)`
## Example ## Example
``` ```
[General] [General]
title = TeamspeakStats
logfile = /usr/local/bin/teamspeak-server/logs/ts3server_2015-03-02__14_01_43.110983_1.log logfile = /usr/local/bin/teamspeak-server/logs/ts3server_2015-03-02__14_01_43.110983_1.log
outputfile = /var/www/html/stats.html outputfile = /var/www/html/stats.html
[HTML]
title = TeamspeakStats
bans = False
``` ```
# ID-Mapping
`id_map.json`
You can map multiple ID's to one (for example, when an user creates a new identity)
## Example
```json
{
"1": "2",
"3": "2"
}
```
The online-time of `1` and `3` will be added to the online-time of `2`
# TODO # TODO
- Localization - Localization
- Map multiple ID's to one user (`json`) - find a better way to count kicks and bans

View File

@ -11,6 +11,7 @@
</head> </head>
<body> <body>
{# Onlinetime #} {# Onlinetime #}
{% if show_onlinetime is sameas true %}
<h1>Onlinetime</h1> <h1>Onlinetime</h1>
<ul class="list-group"> <ul class="list-group">
{% for clid, nick, onlinetime_, connected in onlinetime %} {% for clid, nick, onlinetime_, connected in onlinetime %}
@ -19,7 +20,10 @@
<li class="list-group-item">No data</li> <li class="list-group-item">No data</li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %}
{# Kicks #} {# Kicks #}
{% if show_kicks is sameas true %}
<h1>Kicks</h1> <h1>Kicks</h1>
<ul class="list-group"> <ul class="list-group">
{% for _, nick, kicks_ in kicks %} {% for _, nick, kicks_ in kicks %}
@ -28,7 +32,10 @@
<li class="list-group-item">No data</li> <li class="list-group-item">No data</li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %}
{# passive Kicks #} {# passive Kicks #}
{% if show_pkicks is sameas true %}
<h1>passive Kicks</h1> <h1>passive Kicks</h1>
<ul class="list-group"> <ul class="list-group">
{% for _, nick, pkicks_ in pkicks %} {% for _, nick, pkicks_ in pkicks %}
@ -37,7 +44,10 @@
<li class="list-group-item">No data</li> <li class="list-group-item">No data</li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %}
{# Bans #} {# Bans #}
{% if show_bans is sameas true %}
<h1>Bans</h1> <h1>Bans</h1>
<ul class="list-group"> <ul class="list-group">
{% for _, nick, bans_ in bans %} {% for _, nick, bans_ in bans %}
@ -46,6 +56,7 @@
<li class="list-group-item">No data</li> <li class="list-group-item">No data</li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %}
<p>generated in {{ seconds }} at {{ date }}</p> <p>generated in {{ seconds }} at {{ date }}</p>
</body> </body>
</html> </html>

View File

@ -1,5 +1,6 @@
import re import re
import sys import sys
import json
import configparser import configparser
from time import mktime from time import mktime
from datetime import datetime, timedelta from datetime import datetime, timedelta
@ -18,13 +19,22 @@ path += '/'
# parse config # parse config
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(path + 'config.ini') config.read(path + 'config.ini')
if 'General' not in config or not ('title' in config['General'] and 'logfile' in config['General'] and 'outputfile' in config['General']): # check keys
print('Invalid configuration!') general = config['General']
html = config['HTML']
if ('logfile' not in general or 'outputfile' not in general) or ('title' not in html):
print('Invalid config!')
import sys import sys
sys.exit() sys.exit()
title = config['General']['title'] log_path = general['logfile']
log_path = config['General']['logfile'] output_path = general['outputfile']
output_path = config['General']['outputfile'] title = html['title']
show_onlinetime = html.get('onlinetime', True)
show_kicks = html.get('kicks', True)
show_pkicks = html.get('pkicks', True)
show_bans = html.get('bans', True)
# read id_map
id_map = json.load(open(path + 'id_map.json'))
generation_start = datetime.now() generation_start = datetime.now()
clients = {} # clid: {'nick': ..., 'onlinetime': ..., 'kicks': ..., 'pkicks': ..., 'bans': ..., 'last_connect': ..., 'connected': ...} clients = {} # clid: {'nick': ..., 'onlinetime': ..., 'kicks': ..., 'pkicks': ..., 'bans': ..., 'last_connect': ..., 'connected': ...}
@ -97,22 +107,27 @@ with open(log_path, 'r') as f:
if data.startswith('client'): if data.startswith('client'):
r = cldata.findall(data)[0] r = cldata.findall(data)[0]
nick = r[0] nick = r[0]
id = r[1] clid = r[1]
#print(clid, nick, sep=' | ')
if clid in id_map:
clid = id_map[clid]
if data.startswith('client connected'): if data.startswith('client connected'):
add_connect(id, nick, logdatetime) add_connect(clid, nick, logdatetime)
elif data.startswith('client disconnected'): elif data.startswith('client disconnected'):
add_disconnect(id, nick, logdatetime) add_disconnect(clid, nick, logdatetime)
if 'invokerid' in data: if 'invokerid' in data:
add_pkick(id, nick) add_pkick(clid, nick)
r = cldata_invoker.findall(data)[0] r = cldata_invoker.findall(data)[0]
nick = r[1] nick = r[1]
id = r[0] clid = r[0]
add_kick(id, nick) if clid in id_map:
clid = id_map[clid]
add_kick(clid, nick)
elif data.startswith('ban added') and 'cluid' in data: elif data.startswith('ban added') and 'cluid' in data:
r = cldata_ban.findall(data)[0] r = cldata_ban.findall(data)[0]
nick = r[0] nick = r[0]
id = r[1] clid = r[1]
add_ban(id, nick) add_ban(clid, nick)
for clid in clients: for clid in clients:
if 'connected' not in clients[clid]: if 'connected' not in clients[clid]:
@ -154,7 +169,12 @@ def render_template():
onlinetime_desc[idx] = (clid, nick, onlinetime_str, clients[clid]['connected']) onlinetime_desc[idx] = (clid, nick, onlinetime_str, clients[clid]['connected'])
with open(output_path, 'w+') as f: with open(output_path, 'w+') as f:
f.write(template.render(title=title, onlinetime=onlinetime_desc, kicks=desc('kicks'), pkicks=desc('pkicks'), bans=desc('bans'), seconds='{}.{}'.format(generation_delta.seconds, generation_delta.microseconds), date=generation_end.strftime('%d.%m.%Y um %H:%M'))) f.write(template.render(title=title, onlinetime=onlinetime_desc, kicks=desc('kicks'), pkicks=desc('pkicks'), bans=desc('bans'), seconds='{}.{}'.format(generation_delta.seconds, generation_delta.microseconds),
date=generation_end.strftime('%d.%m.%Y um %H:%M'),
show_onlinetime=show_onlinetime,
show_kicks=show_kicks,
show_pkicks=show_pkicks,
show_bans=show_bans))
if len(clients) < 1: if len(clients) < 1:
print('Not enough data!') print('Not enough data!')