REST API's for JBOSS using through Python(3.7)
rohan153 Jul 26, 2018 10:20 AMHi All,
I am trying to implement REST API's to find the services like {Server Status, Deployments, Data Sources, JNDI Details} on standalone and domain mode. I am able to pull those details using curl commands (Curl Based Management & Monitoring in JBossAS7 with HTTP-JSON APIs « JBoss ), but trying to get these details using service calls through Python (3.7). Would appreciate if anyone could provide sample implementation or any links that might help me.
I am trying to get this service details through the following code which i found on Github. But was encountering an error.
Github Link: jbosscli-python/jbosscli.py at master · yasny/jbosscli-python · GitHub
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -* | |
| import sys | |
| import logging | |
| import json | |
| import requests | |
| from tabulate import tabulate | |
| logging.basicConfig(level=logging.INFO) | |
| log = logging.getLogger("jbosscli") | |
| # disable requests logging | |
| logging.getLogger("requests").setLevel(logging.CRITICAL) | |
| class JBossHelper(object): | |
| "" " JBoss Application Server helper class to easily use the REST management API of. " "" | |
| def __init__(self, url="http://localhost:9990/management", auth="admin:admin"): | |
| self.url = url | |
| self.credentials = auth.split(":") | |
| self._get_server_info() | |
| def _get_server_info(self): | |
| command = {"operation":"read-resource"} | |
| result = self._invoke(command) | |
| result = result['result'] | |
| self.product_name = result['product-name'] | |
| self.product_version = result['product-version'] | |
| self.release_codename = result['release-codename'] | |
| self.release_version = result['release-version'] | |
| def _invoke(self, command): | |
| log.debug("Making request to %s with command %s"%(self.url, command)) | |
| headers = {"Content-type": "application/json"} | |
| r = requests.post( | |
| self.url, | |
| data=json.dumps(command), | |
| headers=headers, | |
| auth=requests.auth.HTTPDigestAuth(self.credentials[0], self.credentials[1]) | |
| ) | |
| log.debug("Response code: %s"%r.status_code) | |
| if (r.status_code >= 400 and not r.text): | |
| raise JBossException("Got %s code from server."%(r.status_code)) | |
| response = r.json() | |
| if 'outcome' not in response: | |
| raise JBossException("Unknown response: %s"%(r.text), response) | |
| if response['outcome'] != 'success': | |
| raise JBossException(response['failure-description'], response) | |
| return response | |
| def __str__(self): | |
| msg = "Connected to %s\nServer: %s (%s)"%(self.url, self.product_name, self.product_version) | |
| return msg | |
| def _get_mbean(self, mbean_type): | |
| command = { | |
| "operation":"read-resource", | |
| "include-runtime":"true", | |
| "address":[ | |
| {"core-service":"platform-mbean"}, | |
| {"type":mbean_type} | |
| ] | |
| } | |
| return self._invoke(command) | |
| def get_used_heap(self): | |
| "" "Get used heap memory and maximum heap memory size. | |
| Returns: | |
| Used heap (bytes) and maximum heap size (bytes) | |
| """ | |
| result = self._get_mbean('memory') | |
| heap_memory_usage = result['result']['heap-memory-usage'] | |
| used_heap = heap_memory_usage['used'] | |
| max_heap = heap_memory_usage['max'] | |
| return (used_heap, max_heap) | |
| def get_thread_count(self): | |
| "" "Get the number of running threads and the maximum number of threads in the past. | |
| Returns: | |
| Number of threads and maximum number of threads in the past | |
| """ | |
| result = self._get_mbean('threading') | |
| thread_count = result['result']['thread-count'] | |
| peak_thread_count = result['result']['peak-thread-count'] | |
| return (thread_count, peak_thread_count) | |
| def get_jboss_status(self): | |
| "" "Get server status (running, stopped etc) " "" | |
| command = { | |
| "operation":"read-attribute", | |
| "name":"server-state", | |
| "include-runtime":"true" | |
| } | |
| result = self._invoke(command) | |
| return result['result'] | |
| def list_mdbs_by_deployment(self, deployment): | |
| "" " List the MDB names for the specified deployment. | |
| Args: | |
| deployment (str): Deployment name | |
| """ | |
| command = { | |
| "operation":"read-children-names", | |
| "child-type":"message-driven-bean", | |
| "address":[ | |
| {"deployment":deployment}, | |
| {"subsystem":"ejb3"} | |
| ] | |
| } | |
| result = self._invoke(command) | |
| return result['result'] | |
| def get_mdbs_by_deployment(self, deployment): | |
| "" " Obtains MDB statistical information etc. for the specified deployment and returns it as a list. | |
| Args: | |
| deployment (str): Deployment name | |
| Returns: | |
| List of MessageDrivenBeans | |
| """ | |
| command = { | |
| "operation":"read-children-resources", | |
| "child-type":"message-driven-bean", | |
| "include-runtime":"true", | |
| "address":[ | |
| {"deployment":deployment}, | |
| {"subsystem":"ejb3"} | |
| ] | |
| } | |
| result = self._invoke(command) | |
| result = result['result'] | |
| mdbs = [] | |
| for bean in result: | |
| info = result[bean] | |
| mdb = MessageDrivenBean(bean, info['invocations'], info['delivery-active'], info['pool-current-size']) | |
| mdbs.append(mdb) | |
| return mdbs | |
| def get_mdb_status(self, deployment, mdb_list): | |
| "" Display delivery - active item of MDB. | |
| Args: | |
| deployment (strg): Deployment name | |
| mdb_list (list): MDBの名前 | |
| """ | |
| for mdb in mdb_list: | |
| command = { | |
| "operation":"read-resource", | |
| "include-runtime":"true", | |
| "address":[ | |
| {'deployment':deployment}, | |
| {"subsystem":"ejb3"}, | |
| {"message-driven-bean":mdb} | |
| ] | |
| } | |
| result = self._invoke(command) | |
| print("MDB[%s]: %s"%(mdb,result['result']['delivery-active'])) | |
| class MessageDrivenBean(object): | |
| def __init__(self, name, invocations, delivery, pool_count): | |
| self.name = name | |
| self.invocations = invocations | |
| self.delivery = delivery | |
| self.pool_count = pool_count | |
| def __str__(self): | |
| return "%s: Invocations = %s, Delivery = %s, Pool Count = %s"%(self.name, self.invocations, self.delivery, self.pool_count) | |
| class JBossException(Exception): | |
| def __init__(self, msg, raw=None): | |
| self.msg = msg; | |
| self.raw = raw if raw else self.msg | |
| def __str__(self): | |
| return repr(self.msg) | |
| def tabulate_mdb(mdbs, tablefmt='simple'): | |
| table = [[m.name, m.invocations, m.delivery, m.pool_count] for m in mdbs] | |
| print tabulate(table, headers=['Message Driven Bean', 'Invocations', 'Delivery', 'Pool Count'], tablefmt=tablefmt) | |
| if __name__=="__main__": | |
| jboss = JBossHelper(auth="admin:Admin#70365") | |
| print(jboss) | |
| print("JBoss is %s."%jboss.get_jboss_status()) | |
| print("HEAP: used:%s max:%s"%jboss.get_used_heap()) | |
| print("THREAD: current:%s peak:%s"%jboss.get_thread_count()) | |
| print("# get_mdbs_by_deployment()") | |
| mdbs = jboss.get_mdbs_by_deployment("wildfly-helloworld-mdb.war") | |
| tabulate_mdb(mdbs, tablefmt='grid') | |
| print("# MDB Delivery Active?") | |
| jboss.get_mdb_status('wildfly-helloworld-mdb.war',['HelloWorldQueueMDB','HelloWorldQTopicMDB']) | |
Error:
root@jboss-1:/opt/wildfly/bin# python jboss.py
Traceback (most recent call last):
File "jboss.py", line 173, in <module>
jboss = JBossHelper(auth="matilda:matilda")
File "jboss.py", line 20, in __init__
self._get_server_info()
File "jboss.py", line 24, in _get_server_info
result = self._invoke(command)
File "jboss.py", line 41, in _invoke
auth=requests.auth.HTTPDigestAuth(self.credentials[0], self.credentials[1])
File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 99, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 49, in request
response = session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 461, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 415, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', error(111, 'Connection refused'))
Thanks In advance.
