From 54256587b1bf8c4a1bf0cefa38eece5aaee3103d Mon Sep 17 00:00:00 2001 From: nico Date: Sun, 16 Feb 2020 12:08:56 +0100 Subject: [PATCH] update plugins * add new config support to the Prometheus plugin * pep8 improvements --- influx.py | 2 +- prometheus.py | 34 +++++++++++++++------------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/influx.py b/influx.py index 0b372ca..59728ac 100644 --- a/influx.py +++ b/influx.py @@ -79,7 +79,7 @@ if __name__ == '__main__': # load config config = Config() - # credentials and parameter + # credentials and parameters url = config.get('url', default='http://localhost:5280/api') login = config.get('login', default=None) api = config.get('api', default='rest') 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 - # 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() + # credentials and parameters + url = config.get('url', default='http://[::1]:5280/api') + login = config.get('login', default=None) + api = config.get('api', default='rest') - 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 + # 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)