aboutsummaryrefslogtreecommitdiffstats
path: root/prometheus.py
diff options
context:
space:
mode:
Diffstat (limited to 'prometheus.py')
-rwxr-xr-xprometheus.py34
1 files changed, 15 insertions, 19 deletions
diff --git a/prometheus.py b/prometheus.py
index dfca7d4..4aab1b3 100755
--- a/prometheus.py
+++ b/prometheus.py
@@ -1,14 +1,16 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
-from ejabberdrpc import EjabberdMetrics
-
-import time
-import threading
import socket
+import threading
+import time
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
+from config import Config
+from ejabberdrpc import EjabberdMetrics
+
+
class _ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
"""Thread per request HTTP server."""
# Make worker threads "fire and forget". Beginning with Python 3.7 this
@@ -65,10 +67,8 @@ class Prometheus():
for k, v in data.items():
output += self._parse_metric("ejabberd_online_client_ipversion", v, {"vhost": host, "node": node, "ipversion": str(k), "client": client})
-
return output
-
def listen(self, port, addr='::'):
"""Starts an HTTP server for prometheus metrics as a daemon thread"""
class myHandler(BaseHTTPRequestHandler):
@@ -79,28 +79,24 @@ class Prometheus():
result = self._get_metrics()
r.wfile.write(result.encode('utf-8'))
-
httpd = _ThreadingSimpleServer((addr, port), myHandler)
t = threading.Thread(target=httpd.serve_forever)
t.daemon = True
t.start()
-if __name__ == "__main__":
- import os
- import json
+if __name__ == "__main__":
# load config
- path = os.path.dirname(__file__)
- with open("/".join([path, "config.json"]), "r", encoding="utf-8") as f:
- config = json.load(f)
-
+ config = Config()
- 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"
+ # credentials and parameters
+ url = config.get('url', default='http://[::1]:5280/api')
+ login = config.get('login', default=None)
+ api = config.get('api', default='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
+ # config prometheus
+ prom_port = config.get('prometheus_port', default=8080)
+ prom_refresh = config.get('prometheus_refresh', default=10)
metrics = EjabberdMetrics(url, login, api)
prom = Prometheus(metrics)