From 71440b7a8fa8b20cb1174e5451efc6ab7f9824ae Mon Sep 17 00:00:00 2001 From: nico Date: Wed, 17 Jun 2020 15:47:31 +0200 Subject: 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 +- calls.py | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cleanup.py | 4 +-- control.py | 96 ------------------------------------------------------------ metrics.py | 8 ++--- 5 files changed, 105 insertions(+), 104 deletions(-) create mode 100644 calls.py delete mode 100644 control.py 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/calls.py b/calls.py new file mode 100644 index 0000000..bd6bd0a --- /dev/null +++ b/calls.py @@ -0,0 +1,98 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +import logging +import re + +from packaging import version + +from api import EjabberdApi + +log = logging.getLogger(__name__) + + +class EjabberdApiCalls(EjabberdApi): + @property + def verstring(self): + if self._login is not None: + ver_str = re.compile('([1-9][0-9.]+(?![.a-z]))\\b') + status = self.cmd('status', {}) + + # matches + try: + tmp = ver_str.findall(status)[0] + # raise SystemExit code 17 if no status message is received + except TypeError: + raise SystemExit(17) + + # return parsed version string + log.debug(f"fetched version string: {tmp}") + return version.parse(tmp) + + return None + + def fetch_onlineuser(self): + tmp = self.cmd("connected_users_info", {}) + if "connected_users_info" not in tmp: + return tmp + data = [] + for c in tmp["connected_users_info"]: + if "session" not in c: + continue + user = {} + for attrs in c["session"]: + for k, v in attrs.items(): + user[k] = v + data.append(user) + return data + + def fetch_nodes(self): + result = self.cmd("list_cluster", {}) + if "nodes" not in result: + return result + data = [] + for node in result["nodes"]: + data.append(node["node"]) + return data + + def fetch_vhosts(self): + result = self.cmd("registered_vhosts", {}) + if "vhosts" not in result: + return result + data = [] + for vhost in result["vhosts"]: + data.append(vhost["vhost"]) + return data + + def fetch_s2s_in(self): + result = self.cmd("incoming_s2s_number", {}) + if "s2s_incoming" not in result: + return result + return result["s2s_incoming"] + + def fetch_s2s_out(self): + result = self.cmd("outgoing_s2s_number", {}) + if "s2s_outgoing" not in result: + return result + return result["s2s_outgoing"] + + def fetch_registered_count(self, vhost=None): + if vhost is None: + result = self.cmd("stats", {"name": "registeredusers"}) + if "stat" in result: + return result["stat"] + else: + 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/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/control.py b/control.py deleted file mode 100644 index f865444..0000000 --- a/control.py +++ /dev/null @@ -1,96 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -import re -import logging - -from packaging import version -from api import EjabberdApi - -class EjabberdCtl(EjabberdApi): - - - @property - def verstring(self): - if self._login is not None: - ver_str = re.compile('([1-9][0-9.]+(?![.a-z]))\\b') - status = self.cmd('status', {}) - - # matches - try: - tmp = ver_str.findall(status)[0] - # raise SystemExit code 17 if no status message is received - except TypeError: - raise SystemExit(17) - - # return parsed version string - logging.debug(f"fetch version: {tmp}") - return version.parse(tmp) - - return None - - def fetch_onlineuser(self): - tmp = self.cmd("connected_users_info", {}) - if "connected_users_info" not in tmp: - return tmp - data = [] - for c in tmp["connected_users_info"]: - if "session" not in c: - continue - user = {} - for attrs in c["session"]: - for k, v in attrs.items(): - user[k] = v - data.append(user) - return data - - def fetch_nodes(self): - result = self.cmd("list_cluster", {}) - if "nodes" not in result: - return result - data = [] - for node in result["nodes"]: - data.append(node["node"]) - return data - - def fetch_vhosts(self): - result = self.cmd("registered_vhosts", {}) - if "vhosts" not in result: - return result - data = [] - for vhost in result["vhosts"]: - data.append(vhost["vhost"]) - return data - - def fetch_s2s_in(self): - result = self.cmd("incoming_s2s_number", {}) - if "s2s_incoming" not in result: - return result - return result["s2s_incoming"] - - def fetch_s2s_out(self): - result = self.cmd("outgoing_s2s_number", {}) - if "s2s_outgoing" not in result: - return result - return result["s2s_outgoing"] - - def fetch_registered_count(self, vhost=None): - if vhost is None: - result = self.cmd("stats", {"name":"registeredusers"}) - if "stat" in result: - return result["stat"] - else: - 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 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() -- cgit v1.2.3-54-g00ecf