improve configuration (with default values) - also for testing ejabberdrpc.py

This commit is contained in:
genofire 2019-12-07 16:01:03 +01:00
parent 39ae668307
commit a5a39958cc
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
4 changed files with 62 additions and 9 deletions

18
config_example.json Normal file
View File

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

16
ejabberdrpc.py Normal file → Executable file
View File

@ -319,8 +319,18 @@ class EjabberdMetrics():
if __name__ == "__main__": if __name__ == "__main__":
from json import dumps import os
metric = EjabberdMetrics("http://[::1]:5280/api", api="rest") 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() data = metric.get_all()
print(dumps(data, indent=True)) print(json.dumps(data, indent=True))

View File

@ -83,12 +83,21 @@ if __name__ == "__main__":
with open("/".join([path, "config.json"]), "r", encoding="utf-8") as f: with open("/".join([path, "config.json"]), "r", encoding="utf-8") as f:
config = json.load(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 # init global handler
metrics = EjabberdMetrics("http://localhost:4560", config['login']) metrics = EjabberdMetrics(url, login, api)
client = InfluxDBClient(host='localhost', port=8086, database=config['database'], retries=5) client = InfluxDBClient(host=influxdb_host, port=influxdb_port, database=influxdb_database, retries=5)
# create database only once # create database only once
client.create_database(config['database']) client.create_database(influxdb_database)
# init influx class # init influx class
influx = Influx(metrics, client) influx = Influx(metrics, client)

View File

@ -86,9 +86,25 @@ class Prometheus():
t.start() t.start()
if __name__ == "__main__": 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 = Prometheus(metrics)
prom.listen(8080) prom.listen(prom_port)
while True: while True:
metrics.update() metrics.update()
time.sleep(10) time.sleep(prom_refresh)