1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import string
from influxdb import InfluxDBClient
from config import Config
from metrics import TeamSpeakMetrics
keys = [
"connection_bytes_received_control",
"connection_bytes_received_keepalive",
"connection_bytes_received_speech",
"connection_bytes_received_total",
"connection_bytes_sent_control",
"connection_bytes_sent_keepalive",
"connection_bytes_sent_speech",
"connection_bytes_sent_total",
"connection_filetransfer_bytes_received_total",
"connection_filetransfer_bytes_sent_total",
"connection_packets_received_control",
"connection_packets_received_keepalive",
"connection_packets_received_speech",
"connection_packets_received_total",
"connection_packets_sent_control",
"connection_packets_sent_keepalive",
"connection_packets_sent_speech",
"connection_packets_sent_total",
"virtualserver_autostart",
"virtualserver_channelsonline",
"virtualserver_clientsonline",
"virtualserver_maxclients",
"virtualserver_queryclientsonline",
"virtualserver_reserved_slots",
"virtualserver_status",
"virtualserver_total_packetloss_control",
"virtualserver_total_packetloss_keepalive",
"virtualserver_total_packetloss_speech",
"virtualserver_total_packetloss_total",
"virtualserver_total_ping",
"virtualserver_uptime",
]
class Influx:
def __init__(self, data, cld):
self._metrics = data
self.client = cld
@staticmethod
def _timestamp():
return int(time.time() * 1000)
@staticmethod
def _rmspace(key: str = None, value: (str, int) = None):
try:
key = key.replace(" ", "\ ") # noqa: W605
value = value.replace(" ", "\ ") # noqa: W605
except (TypeError, AttributeError):
pass
return key, value
def _parse(self, name, key, value, ts, tags=None):
output = name
# check if tags is a dict
if isinstance(tags, dict):
# create tag_key=tag_value pairs for all elements and append them to name
for k, v in tags.items():
output += ",{}={}".format(*self._rmspace(k, v))
# append key=value to name
if value[0] in string.ascii_letters:
output += ' {}="{}" {}'.format(*self._rmspace(key, value), ts)
elif value.isdigit():
output += " {}={}i {}".format(*self._rmspace(key, value), ts)
else:
output += " {}={} {}".format(*self._rmspace(key, value), ts)
return output
def write_metrics(self):
data = list()
cur_ts = self._timestamp()
# serverinfo
for id in ids:
query = self._metrics.serverinfo(id)
name, sid = query["virtualserver_name"], query["virtualserver_id"]
for key, value in query.items():
if key in keys:
data.append(self._parse("teamspeak", key, value, cur_ts, {"name": name, "virtual_server": sid}))
self.client.write_points(data, time_precision="ms", batch_size=10000, protocol="line")
if __name__ == "__main__":
# load config
config = Config()
# credentials and parameters
url = config.get("url", default="https://localhost:10443")
apikey = config.get("apikey", default="")
ids = config.get("ids", default=1)
# config influxdb
influx_host = config.get("influxdb_host", default="localhost")
influx_port = config.get("influxdb_port", default=8086)
influx_dbname = config.get("influxdb_db", default="teamspeak")
# init handler
metrics = TeamSpeakMetrics(url=url, apikey=apikey)
client = InfluxDBClient(host=influx_host, port=influx_port, database=influx_dbname, retries=5)
# create database only once
client.create_database(influx_dbname)
# init influx class
influx = Influx(metrics, client)
while True:
influx.write_metrics()
time.sleep(10)
|