WIP
This commit is contained in:
parent
1a63916b4e
commit
6c58e9b352
|
@ -108,4 +108,8 @@ dmypy.json
|
||||||
.pyre/
|
.pyre/
|
||||||
|
|
||||||
# Pycharm
|
# Pycharm
|
||||||
.idea/
|
.idea/
|
||||||
|
.venv/
|
||||||
|
|
||||||
|
# config
|
||||||
|
config.json
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
[Unit]
|
||||||
|
Description=ejabberd2influxdb
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=nobody
|
||||||
|
Group=nobody
|
||||||
|
ExecStart=/opt/ejabberd-metrics/influx.py
|
||||||
|
Restart=always
|
||||||
|
RestartSec=5s
|
||||||
|
Environment=PATH=/usr/bin:/usr/local/bin
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
|
@ -109,7 +109,11 @@ class EjabberdMetrics():
|
||||||
def fetch_muc(self, vhost=None):
|
def fetch_muc(self, vhost=None):
|
||||||
host = "global"
|
host = "global"
|
||||||
if vhost is not None:
|
if vhost is not None:
|
||||||
host = "conference." + vhost
|
version = self._cmd("status", {})
|
||||||
|
if "19.09" in version:
|
||||||
|
host = "conference." + vhost
|
||||||
|
else:
|
||||||
|
host = vhost
|
||||||
result = self._cmd("muc_online_rooms", {"host": host})
|
result = self._cmd("muc_online_rooms", {"host": host})
|
||||||
if "rooms" in result:
|
if "rooms" in result:
|
||||||
return len(result["rooms"])
|
return len(result["rooms"])
|
||||||
|
|
|
@ -0,0 +1,91 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import time
|
||||||
|
import json
|
||||||
|
|
||||||
|
from influxdb import InfluxDBClient
|
||||||
|
|
||||||
|
from ejabberdrpc import EjabberdMetrics
|
||||||
|
|
||||||
|
|
||||||
|
class Influx():
|
||||||
|
def __init__(self, metrics):
|
||||||
|
self._metrics = metrics
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _timestamp():
|
||||||
|
return int(time.time() * 1000)
|
||||||
|
|
||||||
|
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(k, v)
|
||||||
|
|
||||||
|
# append key=value to name
|
||||||
|
|
||||||
|
if " " in str(key):
|
||||||
|
key = key.replace(" ", "-")
|
||||||
|
|
||||||
|
output += ' {}={}i {}'.format(key, value, ts)
|
||||||
|
return output
|
||||||
|
|
||||||
|
def writeMetrics(self):
|
||||||
|
name = "ejabberd"
|
||||||
|
data = list()
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# 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
|
||||||
|
client_write_end_time = time.perf_counter()
|
||||||
|
print("Client Library Write: {time}s".format(time=client_write_end_time - client_write_start_time))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("config.json", "r", encoding="utf-8") as f:
|
||||||
|
login = json.load(f)
|
||||||
|
|
||||||
|
metrics = EjabberdMetrics("http://[::1]:4560", login)
|
||||||
|
influx = Influx(metrics)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
metrics.update()
|
||||||
|
influx.writeMetrics()
|
||||||
|
|
||||||
|
time.sleep(10)
|
Loading…
Reference in New Issue