add rest api support to ejabberdrpc.py

This commit is contained in:
genofire 2019-12-07 15:30:48 +01:00
parent 6554a1f982
commit 39ae668307
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
1 changed files with 27 additions and 9 deletions

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from xmlrpc import client from xmlrpc import client
import requests
import ipaddress import ipaddress
# rfc6052: IPv6 Addressing of IPv4/IPv6 Translators # rfc6052: IPv6 Addressing of IPv4/IPv6 Translators
@ -11,11 +12,28 @@ class EjabberdMetrics():
""" """
class to fetch metrics per xmlrpc class to fetch metrics per xmlrpc
""" """
def __init__(self, url, login = None): def __init__(self, url, login = None, api = "rpc"):
self._server = client.ServerProxy(url)
self._login = login self._login = login
if api == "rpc":
self._server = client.ServerProxy(url)
self._cmd = self._rpc
else:
self._url = url
self._cmd = self._rest
@property
def _auth(self):
if self._login is not None:
return ( "%s@%s" % (self._login['user'], self._login['server']),
self._login['password'])
return None
def _cmd(self, command, data): def _rest(self, command, data):
r = requests.post("%s/%s" % (self._url, command), auth=self._auth, json=data)
if r is not None:
return r.json()
return {}
def _rpc(self, command, data):
fn = getattr(self._server, command) fn = getattr(self._server, command)
try: try:
if self._login is not None: if self._login is not None:
@ -60,7 +78,7 @@ class EjabberdMetrics():
def fetch_onlineuser(self): def fetch_onlineuser(self):
tmp = self._cmd("connected_users_info", {}) tmp = self._cmd("connected_users_info", {})
if "connected_users_info" not in tmp: if "connected_users_info" not in tmp:
return None return tmp
data = [] data = []
for c in tmp["connected_users_info"]: for c in tmp["connected_users_info"]:
if "session" not in c: if "session" not in c:
@ -75,7 +93,7 @@ class EjabberdMetrics():
def fetch_nodes(self): def fetch_nodes(self):
result = self._cmd("list_cluster",{}) result = self._cmd("list_cluster",{})
if "nodes" not in result: if "nodes" not in result:
return None return result
data = [] data = []
for node in result["nodes"]: for node in result["nodes"]:
data.append(node["node"]) data.append(node["node"])
@ -84,7 +102,7 @@ class EjabberdMetrics():
def fetch_vhosts(self): def fetch_vhosts(self):
result = self._cmd("registered_vhosts",{}) result = self._cmd("registered_vhosts",{})
if "vhosts" not in result: if "vhosts" not in result:
return None return result
data = [] data = []
for vhost in result["vhosts"]: for vhost in result["vhosts"]:
data.append(vhost["vhost"]) data.append(vhost["vhost"])
@ -93,13 +111,13 @@ class EjabberdMetrics():
def fetch_s2s_in(self): def fetch_s2s_in(self):
result = self._cmd("incoming_s2s_number",{}) result = self._cmd("incoming_s2s_number",{})
if "s2s_incoming" not in result: if "s2s_incoming" not in result:
return None return result
return result["s2s_incoming"] return result["s2s_incoming"]
def fetch_s2s_out(self): def fetch_s2s_out(self):
result = self._cmd("outgoing_s2s_number",{}) result = self._cmd("outgoing_s2s_number",{})
if "s2s_outgoing" not in result: if "s2s_outgoing" not in result:
return None return result
return result["s2s_outgoing"] return result["s2s_outgoing"]
def fetch_registered(self, vhost=None): def fetch_registered(self, vhost=None):
@ -302,7 +320,7 @@ class EjabberdMetrics():
if __name__ == "__main__": if __name__ == "__main__":
from json import dumps from json import dumps
metric = EjabberdMetrics("http://[::1]:4560") metric = EjabberdMetrics("http://[::1]:5280/api", api="rest")
data = metric.get_all() data = metric.get_all()
print(dumps(data, indent=True)) print(dumps(data, indent=True))