From a5a39958cc37f1415457d8361d7bcde5c1d34325 Mon Sep 17 00:00:00 2001 From: genofire Date: Sat, 7 Dec 2019 16:01:03 +0100 Subject: [PATCH] improve configuration (with default values) - also for testing ejabberdrpc.py --- config_example.json | 18 ++++++++++++++++++ ejabberdrpc.py | 16 +++++++++++++--- influx.py | 15 ++++++++++++--- prometheus.py | 22 +++++++++++++++++++--- 4 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 config_example.json mode change 100644 => 100755 ejabberdrpc.py diff --git a/config_example.json b/config_example.json new file mode 100644 index 0000000..9f9e3d0 --- /dev/null +++ b/config_example.json @@ -0,0 +1,18 @@ +{ + "url": "http://[::1]:5280/api", + "login": { + "user": "metric-user", + "server": "chat.sum7.eu", + "password": "password" + }, + "api": "rest", + + "--comment": "influxdb", + "influxdb_host": "localhost", + "influxdb_port": 8086, + "database": "ejabberd", + + "--comment": "prometheus", + "prometheus_port": 8080, + "prometheus_refresh": 10 +} diff --git a/ejabberdrpc.py b/ejabberdrpc.py old mode 100644 new mode 100755 index 4cdaf88..0e20986 --- a/ejabberdrpc.py +++ b/ejabberdrpc.py @@ -319,8 +319,18 @@ class EjabberdMetrics(): if __name__ == "__main__": - from json import dumps - metric = EjabberdMetrics("http://[::1]:5280/api", api="rest") + import os + import json + # load config + path = os.path.dirname(__file__) + with open("/".join([path, "config.json"]), "r", encoding="utf-8") as f: + config = json.load(f) + + url = config['url'] if "url" in config else "http://[::1]:5280/api" + login = config['login'] if "login" in config else None + api = config['api'] if "api" in config else "rest" + + metric = EjabberdMetrics(url, login=login, api=api) data = metric.get_all() - print(dumps(data, indent=True)) + print(json.dumps(data, indent=True)) diff --git a/influx.py b/influx.py index 049bef0..b267e23 100644 --- a/influx.py +++ b/influx.py @@ -83,12 +83,21 @@ if __name__ == "__main__": with open("/".join([path, "config.json"]), "r", encoding="utf-8") as f: config = json.load(f) + url = config['url'] if "url" in config else "http://localhost:4560" + login = config['login'] if "login" in config else None + api = config['api'] if "api" in config else "rpc" + + # config influxdb + influxdb_host = config['influxdb_host'] if "influxdb_host" in config else "localhost" + influxdb_port = config['influxdb_port'] if "influxdb_port" in config else 8086 + influxdb_database = config['database'] if "database" in config else "ejabberd" + # init global handler - metrics = EjabberdMetrics("http://localhost:4560", config['login']) - client = InfluxDBClient(host='localhost', port=8086, database=config['database'], retries=5) + metrics = EjabberdMetrics(url, login, api) + client = InfluxDBClient(host=influxdb_host, port=influxdb_port, database=influxdb_database, retries=5) # create database only once - client.create_database(config['database']) + client.create_database(influxdb_database) # init influx class influx = Influx(metrics, client) diff --git a/prometheus.py b/prometheus.py index e534f16..dfca7d4 100755 --- a/prometheus.py +++ b/prometheus.py @@ -86,9 +86,25 @@ class Prometheus(): t.start() if __name__ == "__main__": - metrics = EjabberdMetrics("http://[::1]:4560") + import os + import json + + # load config + path = os.path.dirname(__file__) + with open("/".join([path, "config.json"]), "r", encoding="utf-8") as f: + config = json.load(f) + + + url = config['url'] if "url" in config else "http://[::1]:5280/api" + login = config['login'] if "login" in config else None + api = config['api'] if "api" in config else "rest" + + prom_port = config['prometheus_port'] if "prometheus_port" in config else 8080 + prom_refresh = config['prometheus_refresh'] if "prometheus_refresh" in config else 10 + + metrics = EjabberdMetrics(url, login, api) prom = Prometheus(metrics) - prom.listen(8080) + prom.listen(prom_port) while True: metrics.update() - time.sleep(10) + time.sleep(prom_refresh)