aboutsummaryrefslogtreecommitdiffstats
path: root/prometheus.py
diff options
context:
space:
mode:
authorgenofire <geno+dev@fireorbit.de>2020-06-29 19:34:48 +0200
committergenofire <geno+dev@fireorbit.de>2020-06-29 19:34:48 +0200
commit091b6e525aacad1abf8d800338d242141dec11c2 (patch)
tree73137b27409b00a45c76c2bfbf4b5e9d029a4ff1 /prometheus.py
parent1256ea7de8a093f51b116f2802871480cd646ae8 (diff)
parent044e0334d2bfb9c4b076afc562e4fb3d17c7db20 (diff)
Merge branch 'add-ci' into 'master'
Add ci See merge request sum7/ejabberd-tools!6
Diffstat (limited to 'prometheus.py')
-rwxr-xr-xprometheus.py91
1 files changed, 58 insertions, 33 deletions
diff --git a/prometheus.py b/prometheus.py
index 9af4de3..a526851 100755
--- a/prometheus.py
+++ b/prometheus.py
@@ -7,9 +7,7 @@ from socket import AF_INET6
from time import time
from urllib.parse import parse_qs, urlparse
-from prometheus_client import (
- CollectorRegistry, Gauge, generate_latest, CONTENT_TYPE_LATEST
-)
+from prometheus_client import CollectorRegistry, Gauge, generate_latest, CONTENT_TYPE_LATEST
from config import Config
from metrics import EjabberdMetrics
@@ -17,26 +15,25 @@ from metrics import EjabberdMetrics
class DynamicMetricsHandler(BaseHTTPRequestHandler):
"""HTTP handler that gives metrics from ``core.REGISTRY``."""
+
def do_GET(self):
params = parse_qs(urlparse(self.path).query)
registry = self.generator(params)
- if 'name[]' in params:
- registry = registry.restricted_registry(params['name[]'])
+ if "name[]" in params:
+ registry = registry.restricted_registry(params["name[]"])
try:
output = generate_latest(registry)
- except:
- self.send_error(500, 'error generating metric output')
+ except: # noqa: E722
+ self.send_error(500, "error generating metric output")
raise
self.send_response(200)
- self.send_header('Content-Type', CONTENT_TYPE_LATEST)
+ self.send_header("Content-Type", CONTENT_TYPE_LATEST)
self.end_headers()
self.wfile.write(output)
@staticmethod
def factory(registry_generator):
- DynMetricsHandler = type('MetricsHandler',
- (DynamicMetricsHandler, object),
- {"generator": registry_generator})
+ DynMetricsHandler = type("MetricsHandler", (DynamicMetricsHandler, object), {"generator": registry_generator})
return DynMetricsHandler
@@ -45,7 +42,7 @@ class Prometheus:
self.ttl = 10
self._last_update = 0
self._metrics = metrics
-
+
def handler(self, metrics_handler):
now = time()
if now >= (self._last_update + self.ttl):
@@ -54,25 +51,53 @@ class Prometheus:
registry = CollectorRegistry(auto_describe=True)
- Gauge('ejabberd_node_s2s_in', 'count of incoming server-to-server connection', registry=registry).set(self._metrics.get_s2s_in())
- Gauge('ejabberd_node_s2s_out', 'count of outgoing server-to-server connection', registry=registry).set(self._metrics.get_s2s_out())
+ Gauge("ejabberd_node_s2s_in", "count of incoming server-to-server connection", registry=registry).set(
+ self._metrics.get_s2s_in()
+ )
+ Gauge("ejabberd_node_s2s_out", "count of outgoing server-to-server connection", registry=registry).set(
+ self._metrics.get_s2s_out()
+ )
nodename = self._metrics.nodename
- Gauge('ejabberd_node_uptime', 'uptime of ejabberd service', ["node"], registry=registry).labels(nodename).set(self._metrics.get_uptime())
- Gauge('ejabberd_node_proccess', 'count of pejabber proccess', ["node"], registry=registry).labels(nodename).set(self._metrics.get_processes())
+ Gauge("ejabberd_node_uptime", "uptime of ejabberd service", ["node"], registry=registry).labels(nodename).set(
+ self._metrics.get_uptime()
+ )
+ Gauge("ejabberd_node_proccess", "count of pejabber proccess", ["node"], registry=registry).labels(nodename).set(
+ self._metrics.get_processes()
+ )
labelnames_vhost = ["vhost"]
- registered_vhosts = Gauge('ejabberd_registered_vhosts', 'count of user per vhost', labelnames_vhost, registry=registry)
- muc = Gauge('ejabberd_muc', 'count of muc\'s per vhost', labelnames_vhost, registry=registry)
-
- online_vhost_node = Gauge('ejabberd_online_vhost_node', 'count of client connections', ["vhost", "node"], registry=registry)
-
- online_status = Gauge('ejabberd_online_status', 'count of client connections', ["vhost", "node", "status"], registry=registry)
- online_connection = Gauge('ejabberd_online_connection', 'count of client connections', ["vhost", "node", "connection"], registry=registry)
- online_client = Gauge('ejabberd_online_client', 'count of client software', ["vhost", "node", "client"], registry=registry)
- online_ipversion = Gauge('ejabberd_online_ipversion', 'count of client software', ["vhost", "node", "ipversion"], registry=registry)
- online_client_ipversion = Gauge('ejabberd_online_client_ipversion', 'count of client software', ["vhost", "node", "client", "ipversion"], registry=registry)
+ registered_vhosts = Gauge(
+ "ejabberd_registered_vhosts", "count of user per vhost", labelnames_vhost, registry=registry
+ )
+ muc = Gauge("ejabberd_muc", "count of muc's per vhost", labelnames_vhost, registry=registry)
+
+ online_vhost_node = Gauge(
+ "ejabberd_online_vhost_node", "count of client connections", ["vhost", "node"], registry=registry
+ )
+
+ online_status = Gauge(
+ "ejabberd_online_status", "count of client connections", ["vhost", "node", "status"], registry=registry
+ )
+ online_connection = Gauge(
+ "ejabberd_online_connection",
+ "count of client connections",
+ ["vhost", "node", "connection"],
+ registry=registry,
+ )
+ online_client = Gauge(
+ "ejabberd_online_client", "count of client software", ["vhost", "node", "client"], registry=registry
+ )
+ online_ipversion = Gauge(
+ "ejabberd_online_ipversion", "count of client software", ["vhost", "node", "ipversion"], registry=registry
+ )
+ online_client_ipversion = Gauge(
+ "ejabberd_online_client_ipversion",
+ "count of client software",
+ ["vhost", "node", "client", "ipversion"],
+ registry=registry,
+ )
for host in self._metrics.get_vhosts():
labels_vhost = host
@@ -108,19 +133,19 @@ class Prometheus:
if __name__ == "__main__":
# load config
config = Config()
- if config.get('debug', default=False):
+ if config.get("debug", default=False):
logging.getLogger().setLevel(logging.DEBUG)
# 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.get("url", default="http://[::1]:5280/api")
+ login = config.get("login", default=None)
+ api = config.get("api", default="rest")
# config prometheus
- prom_addr = config.get('prometheus_address', default='127.0.0.1')
- prom_port = config.get('prometheus_port', default=8080)
+ prom_addr = config.get("prometheus_address", default="127.0.0.1")
+ prom_port = config.get("prometheus_port", default=8080)
metrics = EjabberdMetrics(url, login, api)
prom = Prometheus(metrics)
- prom.ttl = config.get('prometheus_cache_ttl', default=10)
+ prom.ttl = config.get("prometheus_cache_ttl", default=10)
prom.listen((prom_addr, prom_port))