summaryrefslogtreecommitdiffstats
path: root/influx.py
diff options
context:
space:
mode:
Diffstat (limited to 'influx.py')
-rw-r--r--influx.py128
1 files changed, 128 insertions, 0 deletions
diff --git a/influx.py b/influx.py
new file mode 100644
index 0000000..e2488ed
--- /dev/null
+++ b/influx.py
@@ -0,0 +1,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)