diff options
author | genofire <geno+dev@fireorbit.de> | 2019-12-07 16:01:03 +0100 |
---|---|---|
committer | genofire <geno+dev@fireorbit.de> | 2019-12-07 16:01:58 +0100 |
commit | a5a39958cc37f1415457d8361d7bcde5c1d34325 (patch) | |
tree | db2ddc0590f9641230ad2a673c2d2da73db98701 | |
parent | 39ae668307bebc2069c23ec8542b4271fde94a8e (diff) |
improve configuration (with default values) - also for testing ejabberdrpc.py
-rw-r--r-- | config_example.json | 18 | ||||
-rwxr-xr-x[-rw-r--r--] | ejabberdrpc.py | 16 | ||||
-rw-r--r-- | influx.py | 15 | ||||
-rwxr-xr-x | prometheus.py | 22 |
4 files changed, 62 insertions, 9 deletions
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 index 4cdaf88..0e20986 100644..100755 --- 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)) @@ -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) |