diff options
author | Martin/Geno <geno+dev@fireorbit.de> | 2019-10-17 13:45:55 +0200 |
---|---|---|
committer | Martin/Geno <geno+dev@fireorbit.de> | 2019-10-17 13:45:55 +0200 |
commit | 47683773ffa97f7371b3c32f1cf2f572c8c2531f (patch) | |
tree | 54c74fa6c010c4db88d2dcd4d049a34ccc29404e | |
parent | 4ceca3f87e0d60773fdb9395d4077c7310ca0423 (diff) |
client by (ipversion)
-rwxr-xr-x | ejabberdrpc.py | 89 | ||||
-rwxr-xr-x | prometheus.py | 5 |
2 files changed, 65 insertions, 29 deletions
diff --git a/ejabberdrpc.py b/ejabberdrpc.py index 180579e..6ae8409 100755 --- a/ejabberdrpc.py +++ b/ejabberdrpc.py @@ -3,6 +3,10 @@ from xmlrpc import client import ipaddress +# rfc6052: IPv6 Addressing of IPv4/IPv6 Translators +nat64 = ipaddress.ip_network("64:ff9b::/96") + + class EjabberdMetrics(): """ class to fetch metrics per xmlrpc @@ -20,6 +24,31 @@ class EjabberdMetrics(): except: return {} + def _client(self, resource): + clientmap = { + "Conv6ations for Sum7": ["Conversations with IPv6"], + "Conversations": [], + "Pix-Art Messenger": [], + "jitsi": [], + "dino": [], + "poezio": [], + } + for client,names in clientmap.items(): + for c in names: + if c in resource: + return client + if client in resource: + return client + return "other" + def _ipversion(self, ip): + addr = ipaddress.ip_address(ip) + if addr.version == 6: + if addr.ipv4_mapped: + return 4 + if addr in nat64: + return 4 + return addr.version + def fetch_onlineuser(self): data = None tmp = self._cmd("connected_users_info", {}) @@ -141,36 +170,38 @@ class EjabberdMetrics(): return self.get_online_by("connection", vhost=vhost, node=node) def get_online_by_client(self, vhost=None, node=None): - def client(r): - clientmap = { - "Conv6ations for Sum7": ["Conversations with IPv6"], - "Conversations": [], - "Pix-Art Messenger": [], - "jitsi": [], - "dino": [], - "poezio": [], - } - for client,names in clientmap.items(): - for c in names: - if c in r: - return client - if client in r: - return client - return "other" - return self.get_online_by("resource", parse=client, vhost=vhost, node=node) + return self.get_online_by("resource", parse=self._client, vhost=vhost, node=node) def get_online_by_ipversion(self, vhost=None, node=None): - # rfc6052: IPv6 Addressing of IPv4/IPv6 Translators - nat64 = ipaddress.ip_network("64:ff9b::/96") - def ipversion(ip): - addr = ipaddress.ip_address(ip) - if addr.version == 6: - if addr.ipv4_mapped: - return 4 - if addr in nat64: - return 4 - return addr.version - return self.get_online_by("ip", parse=ipversion, vhost=vhost, node=node) + return self.get_online_by("ip", parse=self._ipversion, vhost=vhost, node=node) + + 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.fetch_onlineuser() + data = {} + + for conn in self._onlineuser: + client = "other" + if "resource" in conn: + client = self._client(conn["resource"]) + if client not in data: + data[client] = {} + if vhost is not None and vhost not in conn["jid"]: + continue + if node is not None and node != conn["node"]: + continue + if by not in conn: + continue + value = parser(conn[by]) + if value not in data[client]: + data[client][value] = 1 + else: + data[client][value] += 1 + return data + + def get_online_client_by_ipversion(self, vhost=None, node=None): + return self.get_online_client_by("ip", parse=self._ipversion, vhost=vhost, node=node) def get_registered(self, vhost=None): if not hasattr(self, "_registered"): @@ -248,6 +279,8 @@ class EjabberdMetrics(): 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() data["nodes"] = nodes data["s2s_in"] = self.get_s2s_in() diff --git a/prometheus.py b/prometheus.py index 2067f83..e534f16 100755 --- a/prometheus.py +++ b/prometheus.py @@ -43,7 +43,6 @@ class Prometheus(): output += self._parse_metric("ejabberd_node_s2s_in", self._metrics.get_s2s_in()) output += self._parse_metric("ejabberd_node_s2s_out", self._metrics.get_s2s_out()) - for host in self._metrics.get_vhosts(): output += self._parse_metric("ejabberd_registered_vhosts", self._metrics.get_registered(host), {"vhost": host}) muc = self._metrics.get_muc(host) @@ -62,6 +61,10 @@ class Prometheus(): output += self._parse_metric("ejabberd_online_client", v, {"vhost": host, "node": node, "client": k}) for k, v in self._metrics.get_online_by_ipversion(node=node, vhost=host).items(): output += self._parse_metric("ejabberd_online_ipversion", v, {"vhost": host, "node": node, "ipversion": str(k)}) + for client, data in self._metrics.get_online_client_by_ipversion(node=node,vhost=host).items(): + for k, v in data.items(): + output += self._parse_metric("ejabberd_online_client_ipversion", v, {"vhost": host, "node": node, "ipversion": str(k), "client": client}) + return output |