client by (ipversion)

This commit is contained in:
Martin/Geno 2019-10-17 13:45:55 +02:00
parent 4ceca3f87e
commit 47683773ff
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
2 changed files with 65 additions and 29 deletions

View File

@ -3,6 +3,10 @@
from xmlrpc import client from xmlrpc import client
import ipaddress import ipaddress
# rfc6052: IPv6 Addressing of IPv4/IPv6 Translators
nat64 = ipaddress.ip_network("64:ff9b::/96")
class EjabberdMetrics(): class EjabberdMetrics():
""" """
class to fetch metrics per xmlrpc class to fetch metrics per xmlrpc
@ -20,6 +24,31 @@ class EjabberdMetrics():
except: except:
return {} 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): def fetch_onlineuser(self):
data = None data = None
tmp = self._cmd("connected_users_info", {}) tmp = self._cmd("connected_users_info", {})
@ -141,36 +170,38 @@ class EjabberdMetrics():
return self.get_online_by("connection", vhost=vhost, node=node) return self.get_online_by("connection", vhost=vhost, node=node)
def get_online_by_client(self, vhost=None, node=None): def get_online_by_client(self, vhost=None, node=None):
def client(r): return self.get_online_by("resource", parse=self._client, vhost=vhost, node=node)
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)
def get_online_by_ipversion(self, vhost=None, node=None): def get_online_by_ipversion(self, vhost=None, node=None):
# rfc6052: IPv6 Addressing of IPv4/IPv6 Translators return self.get_online_by("ip", parse=self._ipversion, vhost=vhost, node=node)
nat64 = ipaddress.ip_network("64:ff9b::/96")
def ipversion(ip): def get_online_client_by(self, by="ip", parse=None, vhost=None, node=None):
addr = ipaddress.ip_address(ip) parser = parse or self._ipversion
if addr.version == 6: if not hasattr(self, "_onlineuser"):
if addr.ipv4_mapped: self._onlineuser = self.fetch_onlineuser()
return 4 data = {}
if addr in nat64:
return 4 for conn in self._onlineuser:
return addr.version client = "other"
return self.get_online_by("ip", parse=ipversion, vhost=vhost, node=node) 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): def get_registered(self, vhost=None):
if not hasattr(self, "_registered"): if not hasattr(self, "_registered"):
@ -248,6 +279,8 @@ class EjabberdMetrics():
nodes = {} nodes = {}
for node in self.get_nodes(): for node in self.get_nodes():
nodes[node] = self.get_node_metrics(node) nodes[node] = self.get_node_metrics(node)
data["online_client_by_ipversion"] = self.get_online_client_by_ipversion()
data["nodes"] = nodes data["nodes"] = nodes
data["s2s_in"] = self.get_s2s_in() data["s2s_in"] = self.get_s2s_in()

View File

@ -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_in", self._metrics.get_s2s_in())
output += self._parse_metric("ejabberd_node_s2s_out", self._metrics.get_s2s_out()) output += self._parse_metric("ejabberd_node_s2s_out", self._metrics.get_s2s_out())
for host in self._metrics.get_vhosts(): for host in self._metrics.get_vhosts():
output += self._parse_metric("ejabberd_registered_vhosts", self._metrics.get_registered(host), {"vhost": host}) output += self._parse_metric("ejabberd_registered_vhosts", self._metrics.get_registered(host), {"vhost": host})
muc = self._metrics.get_muc(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}) 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(): 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)}) 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 return output