diff --git a/ejabberdrpc.py b/ejabberdrpc.py index 4dd089b..180579e 100755 --- a/ejabberdrpc.py +++ b/ejabberdrpc.py @@ -13,9 +13,12 @@ class EjabberdMetrics(): def _cmd(self, command, data): fn = getattr(self._server, command) - if self._login is not None: - return fn(self._login, data) - return fn(data) + try: + if self._login is not None: + return fn(self._login, data) + return fn(data) + except: + return {} def fetch_onlineuser(self): data = None @@ -51,6 +54,18 @@ class EjabberdMetrics(): data.append(vhost["vhost"]) return data + def fetch_s2s_in(self): + result = self._cmd("incoming_s2s_number",{}) + if "s2s_incoming" not in result: + return None + return result["s2s_incoming"] + + def fetch_s2s_out(self): + result = self._cmd("outgoing_s2s_number",{}) + if "s2s_outgoing" not in result: + return None + return result["s2s_outgoing"] + def fetch_registered(self, vhost=None): if vhost is None: result = self._cmd("stats", {"name":"registeredusers"}) @@ -61,6 +76,14 @@ class EjabberdMetrics(): if "stat" in result: return result["stat"] + def fetch_muc(self, vhost=None): + host = "global" + if vhost is not None: + host = "conference." + vhost + result = self._cmd("muc_online_rooms", {"host": host}) + if "rooms" in result: + return len(result["rooms"]) + def update(self): # nodes self._nodes = self.fetch_nodes() @@ -70,10 +93,19 @@ class EjabberdMetrics(): if not hasattr(self, "_registered"): self._registered = {} self._registered[None] = self.fetch_registered() + # muc + if not hasattr(self, "_muc"): + self._muc = {} + self._muc[None] = self.fetch_muc() + # registered + muc for vhost in self._vhosts: self._registered[vhost] = self.fetch_registered(vhost) + self._muc[vhost] = self.fetch_muc(vhost) # online user self._onlineuser = self.fetch_onlineuser() + # s2s + 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): @@ -147,14 +179,32 @@ class EjabberdMetrics(): self._registered[vhost] = self.fetch_registered(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) + return self._muc[vhost] + def get_vhosts(self): if not hasattr(self, "_vhosts"): self._vhosts = self.fetch_vhosts() return self._vhosts + def get_s2s_in(self): + if not hasattr(self, "_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.fetch_s2s_out() + return self._s2s_out + def get_vhost_metrics(self, vhost): data = {} data["registered"] = self.get_registered(vhost) + data["muc"] = self.get_muc(vhost) data["online_by_status"] = self.get_online_by_status(vhost) data["online_by_client"] = self.get_online_by_client(vhost) data["online_by_ipversion"] = self.get_online_by_ipversion(vhost) @@ -181,6 +231,7 @@ class EjabberdMetrics(): def get_all(self): data = {} data["registered"] = self.get_registered() + data["muc"] = self.get_muc() data["online_by_status"] = self.get_online_by_status() data["online_by_client"] = self.get_online_by_client() data["online_by_ipversion"] = self.get_online_by_ipversion() @@ -199,6 +250,8 @@ class EjabberdMetrics(): nodes[node] = self.get_node_metrics(node) data["nodes"] = nodes + data["s2s_in"] = self.get_s2s_in() + data["s2s_out"] = self.get_s2s_out() return data diff --git a/prometheus.py b/prometheus.py index 8a053f1..2067f83 100755 --- a/prometheus.py +++ b/prometheus.py @@ -41,10 +41,14 @@ class Prometheus(): def _get_metrics(self): output = "" - output += self._parse_metric("ejabberd_registered_total", self._metrics.get_registered()) + 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) + if muc is not None: + output += self._parse_metric("ejabberd_muc", muc, {"vhost": host}) for k, v in self._metrics.get_online_by_node(vhost=host).items(): output += self._parse_metric("ejabberd_online_vhost_node", v, {"vhost": host, "node": k}) @@ -59,12 +63,6 @@ class Prometheus(): 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)}) - # next four lines should be dropped - it should be calc by ejabberd_online_vhost_node - for k, v in self._metrics.get_online_by_node().items(): - output += self._parse_metric("ejabberd_online_node_total", v, {"node": k}) - for k, v in self._metrics.get_online_by_vhost().items(): - output += self._parse_metric("ejabberd_online_vhost_total", v, {"vhost": k}) - return output