update plugins

* add new config support to the Prometheus plugin
* pep8 improvements
This commit is contained in:
nico 2020-02-16 12:08:56 +01:00
parent ee4cb50ba6
commit 54256587b1
Signed by: mightyBroccoli
GPG Key ID: EA7C31AAB1BDC1A2
2 changed files with 16 additions and 20 deletions

View File

@ -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')

View File

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