From 6bb4308de23065f824d14a20455192001c82794e Mon Sep 17 00:00:00 2001 From: genofire Date: Wed, 10 Jun 2020 12:41:38 +0200 Subject: [PATCH] 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):