From 6bb4308de23065f824d14a20455192001c82794e Mon Sep 17 00:00:00 2001 From: genofire Date: Wed, 10 Jun 2020 12:41:38 +0200 Subject: [PATCH 1/6] metrics: use inherits of control/api --- metrics.py | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/metrics.py b/metrics.py index ad23033..5fd5320 100755 --- a/metrics.py +++ b/metrics.py @@ -8,17 +8,15 @@ from control import EjabberdCtl nat64 = ipaddress.ip_network("64:ff9b::/96") -class EjabberdMetrics: +class EjabberdMetrics(EjabberdCtl): """ class to fetch metrics per xmlrpc """ def __init__(self, url, login=None, api="rpc", muc_host: str = 'conference'): # init ejabberd api - self.api = EjabberdCtl(url, login, api) - self._cmd = self.api.cmd + super().__init__(url, login, api) # variables - self._verstring = self.api.verstring self.muc_host = muc_host def _client(self, resource): @@ -58,26 +56,26 @@ class EjabberdMetrics: def fetch_muc(self, vhost=None): host = "global" if vhost is not None: - if self._verstring.major >= 19: + if self.verstring.major >= 19: host = '.'.join([self.muc_host, vhost]) else: host = vhost - result = self._cmd("muc_online_rooms", {"host": host}) + result = self.cmd("muc_online_rooms", {"host": host}) if "rooms" in result: return len(result["rooms"]) return len(result) def update(self): # nodes - self._nodes = self.api.fetch_nodes() + self._nodes = self.fetch_nodes() # vhosts - self._vhosts = self.api.fetch_vhosts() + self._vhosts = self.fetch_vhosts() # registered if not hasattr(self, "_registered"): self._registered = {} - self._registered[None] = self.api.fetch_registered() + self._registered[None] = self.fetch_registered() # muc if not hasattr(self, "_muc"): @@ -86,20 +84,20 @@ class EjabberdMetrics: # registered + muc for vhost in self._vhosts: - self._registered[vhost] = self.api.fetch_registered(vhost) + self._registered[vhost] = self.fetch_registered(vhost) self._muc[vhost] = self.fetch_muc(vhost) # online user - self._onlineuser = self.api.fetch_onlineuser() + self._onlineuser = self.fetch_onlineuser() # s2s - self._s2s_in = self.api.fetch_s2s_in() - self._s2s_out = self.api.fetch_s2s_out() + self._s2s_in = self.fetch_s2s_in() + self._s2s_out = self.fetch_s2s_out() def get_online_by(self, by="node", parse=None, vhost=None, node=None): parser = parse or (lambda a: a) if not hasattr(self, "_onlineuser"): - self._onlineuser = self.api.fetch_onlineuser() + self._onlineuser = self.fetch_onlineuser() data = {} for conn in self._onlineuser: @@ -137,7 +135,7 @@ class EjabberdMetrics: def get_online_client_by(self, by="ip", parse=None, vhost=None, node=None): parser = parse or self._ipversion if not hasattr(self, "_onlineuser"): - self._onlineuser = self.api.fetch_onlineuser() + self._onlineuser = self.fetch_onlineuser() data = {} for conn in self._onlineuser: @@ -166,7 +164,7 @@ class EjabberdMetrics: if not hasattr(self, "_registered"): self._registered = {} if vhost not in self._registered: - self._registered[vhost] = self.api.fetch_registered(vhost) + self._registered[vhost] = self.fetch_registered(vhost) return self._registered[vhost] def get_muc(self, vhost=None): @@ -178,17 +176,17 @@ class EjabberdMetrics: def get_vhosts(self): if not hasattr(self, "_vhosts"): - self._vhosts = self.api.fetch_vhosts() + self._vhosts = self.fetch_vhosts() return self._vhosts def get_s2s_in(self): if not hasattr(self, "_s2s_in"): - self._s2s_in = self.api.fetch_s2s_in() + self._s2s_in = self.fetch_s2s_in() return self._s2s_in def get_s2s_out(self): if not hasattr(self, "_s2s_out"): - self._s2s_out = self.api.fetch_s2s_out() + self._s2s_out = self.fetch_s2s_out() return self._s2s_out def get_vhost_metrics(self, vhost): @@ -206,7 +204,7 @@ class EjabberdMetrics: def get_nodes(self): if not hasattr(self, "_nodes"): - self._nodes = self.api.fetch_nodes() + self._nodes = self.fetch_nodes() return self._nodes def get_node_metrics(self, node): From dd30520c628e175761930b111e31020db8cfcb36 Mon Sep 17 00:00:00 2001 From: genofire Date: Wed, 10 Jun 2020 12:48:18 +0200 Subject: [PATCH 2/6] improve control.py --- control.py | 14 +++++++++++++- metrics.py | 24 ++++++------------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/control.py b/control.py index 3bbf3c7..f865444 100644 --- a/control.py +++ b/control.py @@ -73,7 +73,7 @@ class EjabberdCtl(EjabberdApi): return result return result["s2s_outgoing"] - def fetch_registered(self, vhost=None): + def fetch_registered_count(self, vhost=None): if vhost is None: result = self.cmd("stats", {"name":"registeredusers"}) if "stat" in result: @@ -82,3 +82,15 @@ class EjabberdCtl(EjabberdApi): result = self.cmd("stats_host", {"name":"registeredusers", "host": vhost}) if "stat" in result: return result["stat"] + + def fetch_muc_count(self, vhost=None, muc_host="conference"): + host = "global" + if vhost is not None: + if self.verstring.major >= 19: + host = '.'.join([muc_host, vhost]) + else: + host = vhost + result = self.cmd("muc_online_rooms", {"host": host}) + if "rooms" in result: + return len(result["rooms"]) + return len(result) diff --git a/metrics.py b/metrics.py index 5fd5320..c21511b 100755 --- a/metrics.py +++ b/metrics.py @@ -53,18 +53,6 @@ class EjabberdMetrics(EjabberdCtl): return 4 return addr.version - def fetch_muc(self, vhost=None): - host = "global" - if vhost is not None: - if self.verstring.major >= 19: - host = '.'.join([self.muc_host, vhost]) - else: - host = vhost - result = self.cmd("muc_online_rooms", {"host": host}) - if "rooms" in result: - return len(result["rooms"]) - return len(result) - def update(self): # nodes self._nodes = self.fetch_nodes() @@ -75,17 +63,17 @@ class EjabberdMetrics(EjabberdCtl): # registered if not hasattr(self, "_registered"): self._registered = {} - self._registered[None] = self.fetch_registered() + self._registered[None] = self.fetch_registered_count() # muc if not hasattr(self, "_muc"): self._muc = {} - self._muc[None] = self.fetch_muc() + self._muc[None] = self.fetch_muc_count() # registered + muc for vhost in self._vhosts: - self._registered[vhost] = self.fetch_registered(vhost) - self._muc[vhost] = self.fetch_muc(vhost) + self._registered[vhost] = self.fetch_registered_count(vhost) + self._muc[vhost] = self.fetch_muc_count(vhost) # online user self._onlineuser = self.fetch_onlineuser() @@ -164,14 +152,14 @@ class EjabberdMetrics(EjabberdCtl): if not hasattr(self, "_registered"): self._registered = {} if vhost not in self._registered: - self._registered[vhost] = self.fetch_registered(vhost) + self._registered[vhost] = self.fetch_registered_count(vhost) return self._registered[vhost] def get_muc(self, vhost=None): if not hasattr(self, "_muc"): self._muc = {} if vhost not in self._muc: - self._muc[vhost] = self.fetch_muc(vhost) + self._muc[vhost] = self.fetch_muc_count(vhost) return self._muc[vhost] def get_vhosts(self): From c7bf9f6b9f186b1eb24bd3d3665f04fafeb68246 Mon Sep 17 00:00:00 2001 From: genofire Date: Wed, 10 Jun 2020 23:23:34 +0200 Subject: [PATCH 3/6] fix muc_host --- metrics.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/metrics.py b/metrics.py index c21511b..90d3fc5 100755 --- a/metrics.py +++ b/metrics.py @@ -68,12 +68,12 @@ class EjabberdMetrics(EjabberdCtl): # muc if not hasattr(self, "_muc"): self._muc = {} - self._muc[None] = self.fetch_muc_count() + self._muc[None] = self.fetch_muc_count(muc_host=self.muc_host) # registered + muc for vhost in self._vhosts: self._registered[vhost] = self.fetch_registered_count(vhost) - self._muc[vhost] = self.fetch_muc_count(vhost) + self._muc[vhost] = self.fetch_muc_count(vhost,muc_host=self.muc_host) # online user self._onlineuser = self.fetch_onlineuser() @@ -159,7 +159,7 @@ class EjabberdMetrics(EjabberdCtl): if not hasattr(self, "_muc"): self._muc = {} if vhost not in self._muc: - self._muc[vhost] = self.fetch_muc_count(vhost) + self._muc[vhost] = self.fetch_muc_count(vhost, muc_host=self.muc_host) return self._muc[vhost] def get_vhosts(self): From 990d3abdc60bea8a525d4fbdc56cdff2e44947ce Mon Sep 17 00:00:00 2001 From: nico Date: Wed, 17 Jun 2020 00:41:59 +0200 Subject: [PATCH 4/6] add more global values + add global uptime value + add global processes count value --- control.py | 10 ++++++++++ metrics.py | 8 +++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/control.py b/control.py index f865444..ecf3994 100644 --- a/control.py +++ b/control.py @@ -73,6 +73,16 @@ class EjabberdCtl(EjabberdApi): return result return result["s2s_outgoing"] + def fetch_uptime(self): + result = self.cmd("stats", {"name": "uptimeseconds"}) + if "stat" in result: + return result["stat"] + + def fetch_processes(self): + result = self.cmd("stats", {"name": "processes"}) + if "stat" in result: + return result["stat"] + def fetch_registered_count(self, vhost=None): if vhost is None: result = self.cmd("stats", {"name":"registeredusers"}) diff --git a/metrics.py b/metrics.py index 90d3fc5..761825e 100755 --- a/metrics.py +++ b/metrics.py @@ -210,6 +210,10 @@ class EjabberdMetrics(EjabberdCtl): data = { "registered": self.get_registered(), "muc": self.get_muc(), + "s2s_in": self.get_s2s_in(), + "s2s_out": self.get_s2s_out(), + "uptime": self.fetch_uptime(), + "processes": self.fetch_processes(), "online_by_status": self.get_online_by_status(), "online_by_client": self.get_online_by_client(), "online_by_ipversion": self.get_online_by_ipversion(), @@ -229,9 +233,7 @@ class EjabberdMetrics(EjabberdCtl): data["online_client_by_ipversion"] = self.get_online_client_by_ipversion() data["nodes"] = nodes - - data["s2s_in"] = self.get_s2s_in() - data["s2s_out"] = self.get_s2s_out() + return data From d36dbe03c8c4c11730a8da3c07844ef57fc21ff5 Mon Sep 17 00:00:00 2001 From: nico Date: Wed, 17 Jun 2020 13:29:34 +0200 Subject: [PATCH 5/6] Revert "add more global values" This reverts commit 990d3abdc60bea8a525d4fbdc56cdff2e44947ce. my mistake, I hadn't checked if I am on the correct branch --- control.py | 10 ---------- metrics.py | 8 +++----- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/control.py b/control.py index ecf3994..f865444 100644 --- a/control.py +++ b/control.py @@ -73,16 +73,6 @@ class EjabberdCtl(EjabberdApi): return result return result["s2s_outgoing"] - def fetch_uptime(self): - result = self.cmd("stats", {"name": "uptimeseconds"}) - if "stat" in result: - return result["stat"] - - def fetch_processes(self): - result = self.cmd("stats", {"name": "processes"}) - if "stat" in result: - return result["stat"] - def fetch_registered_count(self, vhost=None): if vhost is None: result = self.cmd("stats", {"name":"registeredusers"}) diff --git a/metrics.py b/metrics.py index 761825e..90d3fc5 100755 --- a/metrics.py +++ b/metrics.py @@ -210,10 +210,6 @@ class EjabberdMetrics(EjabberdCtl): data = { "registered": self.get_registered(), "muc": self.get_muc(), - "s2s_in": self.get_s2s_in(), - "s2s_out": self.get_s2s_out(), - "uptime": self.fetch_uptime(), - "processes": self.fetch_processes(), "online_by_status": self.get_online_by_status(), "online_by_client": self.get_online_by_client(), "online_by_ipversion": self.get_online_by_ipversion(), @@ -233,7 +229,9 @@ class EjabberdMetrics(EjabberdCtl): data["online_client_by_ipversion"] = self.get_online_client_by_ipversion() data["nodes"] = nodes - + + data["s2s_in"] = self.get_s2s_in() + data["s2s_out"] = self.get_s2s_out() return data From 71440b7a8fa8b20cb1174e5451efc6ab7f9824ae Mon Sep 17 00:00:00 2001 From: nico Date: Wed, 17 Jun 2020 15:47:31 +0200 Subject: [PATCH 6/6] pep8 and friends * add default logging NullHandler * fix indentation mismatch * rename control to calls to better point out its purpose * rename class to EjabberdApiCalls to better point out it purpose * changed get_nodes to an internal support method --- api.py | 3 +-- control.py => calls.py | 32 +++++++++++++++++--------------- cleanup.py | 4 ++-- metrics.py | 8 ++++---- 4 files changed, 24 insertions(+), 23 deletions(-) rename control.py => calls.py (78%) diff --git a/api.py b/api.py index ab25f0a..b14fa52 100644 --- a/api.py +++ b/api.py @@ -1,9 +1,8 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- import logging -import re -from packaging import version +logging.getLogger(__name__).addHandler(logging.NullHandler()) class EjabberdApi: diff --git a/control.py b/calls.py similarity index 78% rename from control.py rename to calls.py index f865444..bd6bd0a 100644 --- a/control.py +++ b/calls.py @@ -1,14 +1,16 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -import re import logging +import re from packaging import version + from api import EjabberdApi -class EjabberdCtl(EjabberdApi): +log = logging.getLogger(__name__) +class EjabberdApiCalls(EjabberdApi): @property def verstring(self): if self._login is not None: @@ -23,7 +25,7 @@ class EjabberdCtl(EjabberdApi): raise SystemExit(17) # return parsed version string - logging.debug(f"fetch version: {tmp}") + log.debug(f"fetched version string: {tmp}") return version.parse(tmp) return None @@ -75,22 +77,22 @@ class EjabberdCtl(EjabberdApi): def fetch_registered_count(self, vhost=None): if vhost is None: - result = self.cmd("stats", {"name":"registeredusers"}) + result = self.cmd("stats", {"name": "registeredusers"}) if "stat" in result: return result["stat"] else: - result = self.cmd("stats_host", {"name":"registeredusers", "host": vhost}) + result = self.cmd("stats_host", {"name": "registeredusers", "host": vhost}) if "stat" in result: return result["stat"] def fetch_muc_count(self, vhost=None, muc_host="conference"): - host = "global" - if vhost is not None: - if self.verstring.major >= 19: - host = '.'.join([muc_host, vhost]) - else: - host = vhost - result = self.cmd("muc_online_rooms", {"host": host}) - if "rooms" in result: - return len(result["rooms"]) - return len(result) + host = "global" + if vhost is not None: + if self.verstring.major >= 19: + host = '.'.join([muc_host, vhost]) + else: + host = vhost + result = self.cmd("muc_online_rooms", {"host": host}) + if "rooms" in result: + return len(result["rooms"]) + return len(result) diff --git a/cleanup.py b/cleanup.py index 1654497..f66bea9 100755 --- a/cleanup.py +++ b/cleanup.py @@ -3,10 +3,10 @@ import datetime import logging -from control import EjabberdCtl +from calls import EjabberdApiCalls -class EjabberdCleanup(EjabberdCtl): +class EjabberdCleanup(EjabberdApiCalls): def __init__(self, url, login, api): super().__init__(url, login, api) self.ignore_hosts = [] diff --git a/metrics.py b/metrics.py index 90d3fc5..bd8a3ea 100755 --- a/metrics.py +++ b/metrics.py @@ -2,13 +2,13 @@ # -*- coding: utf-8 -*- import ipaddress -from control import EjabberdCtl +from calls import EjabberdApiCalls # rfc6052: IPv6 Addressing of IPv4/IPv6 Translators nat64 = ipaddress.ip_network("64:ff9b::/96") -class EjabberdMetrics(EjabberdCtl): +class EjabberdMetrics(EjabberdApiCalls): """ class to fetch metrics per xmlrpc """ @@ -190,7 +190,7 @@ class EjabberdMetrics(EjabberdCtl): return data - def get_nodes(self): + def _get_nodes(self): if not hasattr(self, "_nodes"): self._nodes = self.fetch_nodes() return self._nodes @@ -224,7 +224,7 @@ class EjabberdMetrics(EjabberdCtl): data["vhosts"] = vhosts nodes = {} - for node in self.get_nodes(): + for node in self._get_nodes(): nodes[node] = self.get_node_metrics(node) data["online_client_by_ipversion"] = self.get_online_client_by_ipversion()