This commit is contained in:
nico 2019-10-19 05:26:34 +02:00
parent 1a63916b4e
commit 6c58e9b352
Signed by: mightyBroccoli
GPG Key ID: EA7C31AAB1BDC1A2
4 changed files with 115 additions and 2 deletions

6
.gitignore vendored
View File

@ -108,4 +108,8 @@ dmypy.json
.pyre/
# Pycharm
.idea/
.idea/
.venv/
# config
config.json

View File

@ -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

View File

@ -109,7 +109,11 @@ class EjabberdMetrics():
def fetch_muc(self, vhost=None):
host = "global"
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})
if "rooms" in result:
return len(result["rooms"])

91
influx.py Normal file
View File

@ -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)