2019-10-24 20:31:49 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2020-06-10 00:40:22 +02:00
|
|
|
import logging
|
2019-12-28 22:37:07 +01:00
|
|
|
import time
|
2019-10-24 20:31:49 +02:00
|
|
|
|
|
|
|
from influxdb import InfluxDBClient
|
|
|
|
|
2020-02-16 20:19:03 +01:00
|
|
|
from config import Config
|
2020-04-28 14:11:49 +02:00
|
|
|
from metrics import EjabberdMetrics
|
2019-10-24 20:31:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Influx:
|
2019-12-28 22:37:07 +01:00
|
|
|
def __init__(self, data, cld):
|
|
|
|
self._metrics = data
|
|
|
|
self.client = cld
|
2019-10-24 20:31:49 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _timestamp():
|
|
|
|
return int(time.time() * 1000)
|
|
|
|
|
|
|
|
@staticmethod
|
2019-12-28 22:37:07 +01:00
|
|
|
def _rmspace(key: str = None, value: (str, int) = None):
|
2019-10-24 20:31:49 +02:00
|
|
|
try:
|
2020-06-29 18:59:16 +02:00
|
|
|
key = key.replace(" ", "\ ") # noqa: W605
|
|
|
|
value = value.replace(" ", "\ ") # noqa: W605
|
2019-10-24 20:31:49 +02:00
|
|
|
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():
|
2020-06-29 11:03:49 +02:00
|
|
|
output += ",{}={}".format(*self._rmspace(k, v))
|
2019-10-24 20:31:49 +02:00
|
|
|
|
|
|
|
# append key=value to name
|
2020-06-29 11:03:49 +02:00
|
|
|
output += " {}={}i {}".format(*self._rmspace(key, value), ts)
|
2019-10-24 20:31:49 +02:00
|
|
|
return output
|
|
|
|
|
2019-12-28 22:37:07 +01:00
|
|
|
def write_metrics(self):
|
2019-10-24 20:31:49 +02:00
|
|
|
data = list()
|
|
|
|
|
|
|
|
# global values
|
|
|
|
cur_ts = self._timestamp()
|
2020-06-29 11:03:49 +02:00
|
|
|
data.append(f"ejabberd s2s_in={self._metrics.get_s2s_in()}i {cur_ts}")
|
|
|
|
data.append(f"ejabberd s2s_out={self._metrics.get_s2s_out()}i {cur_ts}")
|
|
|
|
data.append(f"ejabberd uptime={self._metrics.get_uptime()}i {cur_ts}")
|
|
|
|
data.append(f"ejabberd processes={self._metrics.get_processes()}i {cur_ts}")
|
2019-10-24 20:31:49 +02:00
|
|
|
|
|
|
|
# vhost values
|
|
|
|
for vhost in self._metrics.get_vhosts():
|
|
|
|
cur_ts = self._timestamp()
|
2020-06-29 11:03:49 +02:00
|
|
|
data.append(f"ejabberd,vhost={vhost} registered={self._metrics.get_registered(vhost)}i {cur_ts}")
|
|
|
|
data.append(f"ejabberd,vhost={vhost} muc={self._metrics.get_muc(vhost)}i {cur_ts}")
|
2019-10-24 20:31:49 +02:00
|
|
|
|
2019-12-07 14:48:25 +01:00
|
|
|
# vhost statistics on their respective node
|
2019-10-24 20:31:49 +02:00
|
|
|
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():
|
2020-06-29 11:03:49 +02:00
|
|
|
data.append(self._parse("ejabberd_online_status", k, v, cur_ts, {"node": node, "vhost": vhost}))
|
2019-10-24 20:31:49 +02:00
|
|
|
for k, v in self._metrics.get_online_by_client(node=node, vhost=vhost).items():
|
2020-06-29 11:03:49 +02:00
|
|
|
data.append(self._parse("ejabberd_online_client", k, v, cur_ts, {"node": node, "vhost": vhost}))
|
2019-10-24 20:31:49 +02:00
|
|
|
for k, v in self._metrics.get_online_by_ipversion(node=node, vhost=vhost).items():
|
2020-06-29 11:03:49 +02:00
|
|
|
data.append(self._parse("ejabberd_online_ipversion", k, v, cur_ts, {"node": node, "vhost": vhost}))
|
2019-10-24 20:31:49 +02:00
|
|
|
for k, v in self._metrics.get_online_by_connection(node=node, vhost=vhost).items():
|
2020-06-29 11:03:49 +02:00
|
|
|
data.append(self._parse("ejabberd_online_connection", k, v, cur_ts, {"node": node, "vhost": vhost}))
|
2019-10-24 20:31:49 +02:00
|
|
|
for cl, ipv in self._metrics.get_online_client_by_ipversion(node=node, vhost=vhost).items():
|
|
|
|
for k, v in ipv.items():
|
2020-06-29 11:03:49 +02:00
|
|
|
data.append(
|
|
|
|
self._parse(
|
|
|
|
"ejabberd_online_client_ipversion",
|
|
|
|
k,
|
|
|
|
v,
|
|
|
|
cur_ts,
|
|
|
|
{"vhost": vhost, "node": node, "ipversion": k, "client": cl},
|
|
|
|
)
|
|
|
|
)
|
2019-10-24 20:31:49 +02:00
|
|
|
|
|
|
|
# write output to database
|
2020-06-29 11:03:49 +02:00
|
|
|
self.client.write_points(data, time_precision="ms", batch_size=10000, protocol="line")
|
2019-12-07 14:48:25 +01:00
|
|
|
|
2019-10-24 20:31:49 +02:00
|
|
|
|
2020-06-29 11:03:49 +02:00
|
|
|
if __name__ == "__main__":
|
2019-12-07 14:48:25 +01:00
|
|
|
# load config
|
2020-02-16 20:19:03 +01:00
|
|
|
config = Config()
|
2019-10-24 20:31:49 +02:00
|
|
|
|
2020-06-29 11:03:49 +02:00
|
|
|
if config.get("debug", default=False):
|
2020-06-09 20:22:30 +02:00
|
|
|
logging.getLogger().setLevel(logging.DEBUG)
|
|
|
|
|
2020-02-16 20:19:03 +01:00
|
|
|
# credentials and parameters
|
2020-06-29 11:03:49 +02:00
|
|
|
url = config.get("url", default="http://localhost:5280/api")
|
|
|
|
login = config.get("login", default=None)
|
|
|
|
api = config.get("api", default="rest")
|
2019-12-28 22:37:07 +01:00
|
|
|
|
2019-12-07 16:01:03 +01:00
|
|
|
# config influxdb
|
2020-06-29 11:03:49 +02:00
|
|
|
influx_host = config.get("influxdb_host", default="localhost")
|
|
|
|
influx_port = config.get("influxdb_port", default=8086)
|
|
|
|
influx_dbname = config.get("influxdb_db", default="ejabberd")
|
2019-12-07 16:01:03 +01:00
|
|
|
|
2019-12-28 22:37:07 +01:00
|
|
|
# init handler
|
2019-12-07 16:01:03 +01:00
|
|
|
metrics = EjabberdMetrics(url, login, api)
|
2019-12-28 22:37:07 +01:00
|
|
|
client = InfluxDBClient(host=influx_host, port=influx_port, database=influx_dbname, retries=5)
|
2019-10-24 20:31:49 +02:00
|
|
|
|
|
|
|
# create database only once
|
2019-12-28 22:37:07 +01:00
|
|
|
client.create_database(influx_dbname)
|
2019-10-24 20:31:49 +02:00
|
|
|
|
|
|
|
# init influx class
|
|
|
|
influx = Influx(metrics, client)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
metrics.update()
|
2019-12-28 22:37:07 +01:00
|
|
|
influx.write_metrics()
|
2019-10-24 20:31:49 +02:00
|
|
|
|
|
|
|
time.sleep(10)
|