-
Notifications
You must be signed in to change notification settings - Fork 12
/
pyjolokia.py
190 lines (149 loc) · 5.68 KB
/
pyjolokia.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
try:
import json
except:
import simplejson as json
try:
import urllib2 as urllib
except ImportError:
import urllib
try:
from urllib2 import Request
except ImportError:
from urllib.request import Request
try:
from urllib2 import urlopen
except ImportError:
from urllib.request import urlopen
import base64
class Jolokia:
'''
pyJolokia class Jolokia is a JSON featching python class.
It uses urllib2 and json or simplejson to do post requests
to a jolokia URL. Then returns back a python dictionary.
.. code-block:: python
j4p = Jolokia('http://localhost:9199/jolokia/')
j4p.request(type = 'read', mbean = 'java.lang:type=Threading',
attribute = 'ThreadCount' )
>> { 'status' : 200, ...
'''
def __init__(self, url, **kwargs):
self.url = url
self.data = None
self.proxyConfig = {}
self.authConfig = {}
self.reqConfig = {}
self.timeout = kwargs.get('timeout', 10)
def auth(self, **kwargs):
'''
Used to add auth info if using jolokia via http to access the jmx
example
.. code-block:: python
j4p.auth(httpusername='user',httppassword='password')
'''
self.authConfig['auth'] = {}
if 'httpusername' in kwargs:
self.authConfig['auth']['username'] = kwargs.get('httpusername')
if 'httppassword' in kwargs:
self.authConfig['auth']['password'] = kwargs.get('httppassword')
def config(self, **kwargs):
'''
Used to set configuration options for the request
see: http://www.jolokia.org/reference/html/protocol.html#processing-parameters
example
.. code-block:: python
j4p.config(ignoreErrors=True)
'''
if kwargs is not None:
for key, value in kwargs.iteritems():
self.reqConfig[key] = value
def proxy(self, url, **kwargs):
'''
Used to add proxy info if using jolokia as a proxy to other
java jmx apps.
example
.. code-block:: python
j4p.proxy('service:jmx:rmi://somehost:1234/some.mbean.server',
user = 'cwood',
password = 'somePassword')
'''
self.proxyConfig['target'] = {}
self.proxyConfig['target']['url'] = url
if 'user' in kwargs:
self.proxyConfig['target']['user'] = kwargs.get('user')
if 'password' in kwargs:
self.proxyConfig['target']['password'] = kwargs.get('password')
def __getJson(self):
if isinstance(self.data, dict):
mainRequest = self.data.copy()
mainRequest.update(self.proxyConfig)
else:
mainRequest = []
for request in self.data:
request = request.copy()
request.update(self.proxyConfig)
mainRequest.append(request)
jdata = json.dumps(mainRequest).encode('utf-8')
authheader = None
if self.authConfig:
if self.authConfig['auth']['username'] and self.authConfig['auth']['password']:
authheader = base64.standard_b64encode(
('%s:%s' % (
self.authConfig['auth']['username'],
self.authConfig['auth']['password']
)
).encode()).decode()
try:
request = Request(self.url, jdata,
{'content-type': 'application/json'})
if authheader:
request.add_header("Authorization", 'Basic ' + authheader)
responseStream = urlopen(request, timeout=self.timeout)
jsonData = responseStream.read()
except Exception as e:
raise JolokiaError('Could not connect. Got error %s' % (e))
finally:
responseStream.close()
try:
pythonDict = json.loads(jsonData.decode())
except:
raise JolokiaError("Could not decode into json. \
Is Jolokia running at %s" % (self.url))
return pythonDict
def __mkrequest(self, type, **kwargs):
newRequest = {}
newRequest['type'] = type
newRequest['config'] = self.reqConfig
if type != 'list':
newRequest['mbean'] = kwargs.get('mbean')
else:
newRequest['path'] = kwargs.get('path')
if type == 'read':
newRequest['attribute'] = kwargs.get('attribute')
newRequest['path'] = kwargs.get('path')
elif type == 'write':
newRequest['attribute'] = kwargs.get('attribute', '')
newRequest['value'] = kwargs.get('value', '')
newRequest['path'] = kwargs.get('path', '')
elif type == 'exec':
newRequest['operation'] = kwargs.get('operation')
newRequest['arguments'] = kwargs.get('arguments')
return newRequest
def request(self, type, **kwargs):
if not isinstance(self.data, dict):
self.data = {}
self.data = self.__mkrequest(type, **kwargs)
response = self.__getJson()
return response
def add_request(self, type, **kwargs):
new_response = self.__mkrequest(type, **kwargs)
if not isinstance(self.data, list):
self.data = list()
self.data.append(new_response)
def getRequests(self):
response = self.__getJson()
return response
class JolokiaError(Exception):
def __init__(self, message):
self.message = message
def __str__(self):
return self.message