Inherits control in metrics (+ fix nameing to _count)

See merge request sum7/ejabberd-tools!5
This commit is contained in:
genofire 2020-06-24 11:31:40 +02:00
commit 87dc79a2f7
4 changed files with 45 additions and 46 deletions

3
api.py
View File

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

View File

@ -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
@ -73,12 +75,24 @@ 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"})
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)

View File

@ -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 = []

View File

@ -2,23 +2,21 @@
# -*- 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:
class EjabberdMetrics(EjabberdApiCalls):
"""
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):
@ -55,51 +53,39 @@ class EjabberdMetrics:
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.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_count()
# muc
if not hasattr(self, "_muc"):
self._muc = {}
self._muc[None] = self.fetch_muc()
self._muc[None] = self.fetch_muc_count(muc_host=self.muc_host)
# registered + muc
for vhost in self._vhosts:
self._registered[vhost] = self.api.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,muc_host=self.muc_host)
# 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 +123,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,29 +152,29 @@ 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_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, muc_host=self.muc_host)
return self._muc[vhost]
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):
@ -204,9 +190,9 @@ class EjabberdMetrics:
return data
def get_nodes(self):
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):
@ -238,7 +224,7 @@ class EjabberdMetrics:
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()