final touches

+ add method to escape whitespaces in keys and values
* reverted node first mentality

- vhosts are listed first and after that the respective nodes are listed
This commit is contained in:
nico 2019-10-22 14:25:52 +02:00
parent 6c58e9b352
commit 38dba3845f
Signed by: mightyBroccoli
GPG Key ID: EA7C31AAB1BDC1A2
1 changed files with 34 additions and 27 deletions

View File

@ -1,8 +1,8 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
import json
import time
from influxdb import InfluxDBClient
@ -17,6 +17,16 @@ class Influx():
def _timestamp():
return int(time.time() * 1000)
@staticmethod
def _rmspace(key: str = None, value=None):
try:
key = key.replace(" ", "\ ")
value = value.replace(" ","\ ")
except (TypeError, AttributeError):
pass
return key, value
def _parse(self, name, key, value, ts, tags=None):
output = name
@ -25,14 +35,10 @@ class Influx():
# create tag_key=tag_value pairs for all elements and append them to name
for k, v in tags.items():
output += ",{}={}".format(k, v)
output += ",{}={}".format(*self._rmspace(k, v))
# append key=value to name
if " " in str(key):
key = key.replace(" ", "-")
output += ' {}={}i {}'.format(key, value, ts)
output += ' {}={}i {}'.format(*self._rmspace(key, value), ts)
return output
def writeMetrics(self):
@ -41,35 +47,36 @@ class Influx():
# global values
cur_ts = self._timestamp()
data.append("{m} registered={v}i {ts}".format(m=name, v=self._metrics.get_registered(), ts=cur_ts))
data.append("{m} muc={v}i {ts}".format(m=name, v=self._metrics.get_muc(), ts=cur_ts))
data.append("{m} s2s_in={v}i {ts}".format(m=name, v=self._metrics.get_s2s_in(), ts=cur_ts))
data.append("{m} s2s_out={v}i {ts}".format(m=name, v=self._metrics.get_s2s_out(), ts=cur_ts))
# node values
for node in self._metrics.get_nodes():
cur_ts = self._timestamp()
for k, v in self._metrics.get_online_by_status(node=node).items():
data.append(self._parse("ejabberd_online_status", k, v, cur_ts, {"node": node}))
for k, v in self._metrics.get_online_by_client(node=node).items():
data.append(self._parse("ejabberd_online_client", k, v, cur_ts, {"node": node}))
for k, v in self._metrics.get_online_by_ipversion(node=node).items():
data.append(self._parse("ejabberd_online_ipversion", k, v, cur_ts, {"node": node}))
for k, v in self._metrics.get_online_by_connection(node=node).items():
data.append(self._parse("ejabberd_online_connection", k, v, cur_ts, {"node": node}))
for k, v in self._metrics.get_online_by_vhost(node=node).items():
data.append(self._parse("ejabberd_online_node", k, v, cur_ts, {"node": node}))
# vhost values
# todo
for vhost in self._metrics.get_vhosts():
cur_ts = self._timestamp()
data.append("{m},vhost={vh} registered={v}i {ts}".format(m=name, vh= vhost, v=self._metrics.get_registered(vhost),ts=cur_ts))
data.append("{m},vhost={vh} muc={v}i {ts}".format(m=name, vh= vhost, v=self._metrics.get_muc(vhost), ts=cur_ts))
# vhost statistics on their repsective node
for node in self._metrics.get_nodes():
cur_ts = self._timestamp()
for k, v in self._metrics.get_online_by_status(node=node, vhost=vhost).items():
data.append(self._parse("ejabberd_online_status", k, v, cur_ts, {"node": node, "vhost": vhost}))
for k, v in self._metrics.get_online_by_client(node=node, vhost=vhost).items():
data.append(self._parse("ejabberd_online_client", k, v, cur_ts, {"node": node, "vhost": vhost}))
for k, v in self._metrics.get_online_by_ipversion(node=node, vhost=vhost).items():
data.append(self._parse("ejabberd_online_ipversion", k, v, cur_ts, {"node": node, "vhost": vhost}))
for k, v in self._metrics.get_online_by_connection(node=node, vhost=vhost).items():
data.append(self._parse("ejabberd_online_connection", k, v, cur_ts, {"node": node, "vhost": vhost}))
for cl, ipv in self._metrics.get_online_client_by_ipversion(node=node, vhost=vhost).items():
for k, v in ipv.items():
data.append(self._parse("ejabberd_online_client_ipversion", k, v, cur_ts, {"vhost": vhost, "node": node, "ipversion": k, "client": cl}))
# output
client = InfluxDBClient(host='localhost', port=8086)
# test code
client.create_database('writetest')
client_write_start_time = time.perf_counter()
client.write_points(data, database='writetest', time_precision='ms', batch_size=10000, protocol='line')
# test code
@ -88,4 +95,4 @@ if __name__ == "__main__":
metrics.update()
influx.writeMetrics()
time.sleep(10)
time.sleep(10)