-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathjbossapi.py
More file actions
398 lines (333 loc) · 15.1 KB
/
jbossapi.py
File metadata and controls
398 lines (333 loc) · 15.1 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# coding=utf-8
"""
V. 1.0
JbossApiCollector is a collector that uses JBOSS 7 native API to collect data
Tested on jboss 7.X.X??
Much of the code was borrowed from:
http://bit.ly/XRrCWx
https://github.com/lukaf/munin-plugins/blob/master/jboss7_
References:
https://docs.jboss.org/author/display/AS7/Management+API+reference
http://middlewaremagic.com/jboss/?p=2476
TODO:
This code was made to work with the local system 'curl' command, due to
difficulties getting urllib2 or pycurl to work under the python 2.4 options
successfully doing SSL Digest Authentication.
Plan is to make this code work with newer versions of python and possibly
Requests. (http://docs.python-requests.org/en/latest/)
If possible, please make future updates backwards compatible to call the local
curl as an option.
#### Dependencies
* java
* jboss
* curl
* json
##### Configuration
# Uses local system curl until can be made to work with either urllib2, pycurl,
or requests (http://docs.python-requests.org/en/latest/)
enabled = True
path_suffix = ""
measure_collector_time = False
interface_regex = ^(.+?)\.
curl_bin = /usr/bin/curl
connect_timeout = 4
hosts = wasadmin:pass@host:9443:https, wasadmin:pass@host:9443:https
curl_options = "-s --digest -L "
ssl_options = "--sslv3 -k"
connector_stats = True | False
connector_options = http, ajp
app_stats = True | False
jvm_memory_stats = True | False
jvm_buffer_pool_stats = True | False
jvm_memory_pool_stats = True | False
jvm_gc_stats = True | False
jvm_thread_stats = True | False
"""
import diamond.collector
import os
import re
import subprocess
try:
import json
except ImportError:
import simplejson as json
# Setup a set of VARs
# Set this for use in curl request
header = '"Content-Type: application/json"'
operational_type = [
'app',
'web',
'jvm',
'thread_pool',
]
web_stats = [
'errorCount',
'requestCount',
'bytesReceived',
'bytesSent',
'processingTime',
]
memory_types = [
'init',
'used',
'committed',
'max',
]
buffer_pool_types = [
'count',
'memory-used',
]
thread_types = [
'thread-count',
'daemon-thread-count'
]
memory_topics = [
'heap-memory-usage',
'non-heap-memory-usage',
]
gc_types = [
'collection-count',
'collection-time',
]
class JbossApiCollector(diamond.collector.Collector):
def process_config(self):
super(JbossApiCollector, self).process_config()
if self.config['hosts'].__class__.__name__ != 'list':
self.config['hosts'] = [self.config['hosts']]
# get the params for each host
if 'host' in self.config:
hoststr = "%s:%s@%s:%s:%s" % (
self.config['user'],
self.config['password'],
self.config['host'],
self.config['port'],
self.config['proto'],
)
self.config['hosts'].append(hoststr)
if type(self.config['connector_options']) is not list:
self.config['connector_options'] = [
self.config['connector_options']]
def get_default_config_help(self):
# Need to update this when done to help explain details when running
# diamond-setup.
config_help = super(JbossApiCollector, self).get_default_config_help()
config_help.update({
'curl_bin': 'Path to system curl executable',
'hosts': 'List of hosts to collect from. Format is yourusername:yourpassword@host:port:proto', # NOQA
'app_stats': 'Collect application pool stats',
'jvm_memory_pool_stats': 'Collect JVM memory-pool stats',
'jvm_buffer_pool_stats': 'Collect JVM buffer-pool stats',
'jvm_memory_stats': 'Collect JVM basic memory stats',
'jvm_gc_stats': 'Collect JVM garbage-collector stats',
'jvm_thread_stats': 'Collect JVM thread stats',
'thread_pool_stats': 'Collect JBoss thread pool stats',
'connector_stats': 'Collect HTTP and AJP Connector stats',
'connector_options': 'Types of connectors to collect'
})
return config_help
def get_default_config(self):
# Initialize default config
config = super(JbossApiCollector, self).get_default_config()
config.update({
'path': 'jboss',
'curl_bin': '/usr/bin/curl',
'connect_timeout': '4',
'ssl_options': '--sslv3 -k',
'curl_options': '-s --digest -L ',
'interface_regex': '^(.+?)\.', # matches up to first "."
'hosts': [],
'app_stats': 'True',
'connector_options': ['http', 'ajp'],
'jvm_memory_pool_stats': 'True',
'jvm_buffer_pool_stats': 'True',
'jvm_memory_stats': 'True',
'jvm_gc_stats': 'True',
'jvm_thread_stats': 'True',
'connector_stats': 'True',
'thread_pool_stats': 'True'
})
# Return default config
return config
def get_stats(self, current_host, current_port, current_proto, current_user,
current_pword):
if not os.access(self.config['curl_bin'], os.X_OK):
self.log.error("%s is not executable or does not exist.",
self.config['curl_bin'])
# Check if there is a RegEx to perform on the interface names
if self.config['interface_regex'] != '':
interface = self.string_regex(self.config['interface_regex'],
current_host)
else:
# Clean up any possible extra "."'s in the interface, keeps
# graphite from creating directories
interface = self.string_fix(current_host)
for op_type in operational_type:
output = self.get_data(op_type, current_host, current_port,
current_proto, current_user, current_pword)
if op_type == 'app' and self.config['app_stats'] == 'True':
if output:
# Grab the pool stats for each Instance
if 'xa-data-source' in output['result']:
output['result']['data-source'].update(output['result']['xa-data-source'])
for instance in output['result']['data-source']:
datasource = output['result']['data-source'][instance]
for metric in datasource['statistics']['pool']:
metricName = '%s.%s.%s.statistics.pool.%s' % (
interface, op_type, instance, metric)
metricValue = datasource[
'statistics']['pool'][metric]
self.publish(metricName, float(metricValue))
if (op_type == 'thread_pool' and
self.config['thread_pool_stats'] == 'True' and output):
# Grab the stats from each thread pool type
for pool_type in output['result']:
if output['result'][pool_type]:
pool_types = output['result'][pool_type]
for pool in pool_types:
for metric in pool_types[pool]:
metricName = '%s.%s.%s.%s.statistics.%s' % (
interface, op_type, pool_type,
pool, metric)
metricValue = pool_types[pool][metric]
if self.is_number(metricValue):
self.publish(metricName, float(metricValue))
if op_type == 'web' and self.config['connector_stats'] == 'True':
if output:
# Grab http and ajp info (make these options)
for c_type in self.config['connector_options']:
for metric in web_stats:
metricName = ('%s.%s.connector.%s.%s' %
(interface,
op_type,
c_type,
metric))
connector = output['result']['connector']
metricValue = connector[c_type][metric]
self.publish(metricName, float(metricValue))
if op_type == 'jvm':
if output:
if self.config['jvm_memory_pool_stats'] == 'True':
# Grab JVM memory pool stats
mempool = output['result']['type']['memory-pool']
for pool_name in mempool['name']:
for metric in memory_types:
metricName = ('%s.%s.%s.%s.%s.%s' %
(interface,
op_type,
'memory-pool',
pool_name,
'usage',
metric))
metricValue = mempool['name'][pool_name][
'usage'][metric]
self.publish(metricName, float(metricValue))
# Grab JVM buffer-pool stats
if self.config['jvm_buffer_pool_stats'] == 'True':
bufferpool = output['result']['type']['buffer-pool']
for pool in bufferpool['name']:
for metric in buffer_pool_types:
metricName = ('%s.%s.%s.%s.%s' %
(interface,
op_type,
'buffer-pool',
pool,
metric))
metricValue = bufferpool['name'][pool][metric]
self.publish(metricName, float(metricValue))
# Grab basic memory stats
if self.config['jvm_memory_stats'] == 'True':
for mem_type in memory_topics:
for metric in memory_types:
metricName = ('%s.%s.%s.%s.%s' %
(interface,
op_type,
'memory',
mem_type,
metric))
memory = output['result']['type']['memory']
metricValue = memory[mem_type][metric]
self.publish(metricName, float(metricValue))
# Grab Garbage collection stats
if self.config['jvm_gc_stats'] == 'True':
garbage = output['result']['type']['garbage-collector']
for gc_name in garbage['name']:
for metric in gc_types:
metricName = ('%s.%s.%s.%s.%s' %
(interface,
op_type,
'garbage-collector',
gc_name,
metric))
metricValue = garbage['name'][gc_name][metric]
self.publish(metricName, float(metricValue))
# Grab threading stats
if self.config['jvm_thread_stats'] == 'True':
for metric in thread_types:
metricName = ('%s.%s.%s.%s' %
(interface,
op_type,
'threading',
metric))
threading = output['result']['type']['threading']
metricValue = threading[metric]
self.publish(metricName, float(metricValue))
return True
def get_data(self, op_type, current_host, current_port, current_proto,
current_user, current_pword):
output = {}
if op_type == 'app':
data = ('{"operation":"read-resource", ' +
'"include-runtime":"true", ' +
'"recursive":"true", ' +
'"address":["subsystem","datasources"]}')
if op_type == 'thread_pool':
data = ('{"operation":"read-resource", "include-runtime":"true", ' +
'"recursive":"true" , "address":["subsystem","threads"]}')
if op_type == 'web':
data = ('{"operation":"read-resource", ' +
'"include-runtime":"true", ' +
'"recursive":"true", ' +
'"address":["subsystem","web"]}')
if op_type == 'jvm':
data = ('{"operation":"read-resource", ' +
'"include-runtime":"true", ' +
'"recursive":"true", ' +
'"address":["core-service","platform-mbean"]}')
the_cmd = (("%s --connect-timeout %s %s %s %s://%s:%s/management " +
"--header %s -d '%s' -u %s:%s") % (
self.config['curl_bin'], self.config['connect_timeout'],
self.config['ssl_options'], self.config['curl_options'],
current_proto, current_host, current_port, header, data,
current_user, current_pword))
try:
attributes = subprocess.Popen(the_cmd, shell=True,
stdout=subprocess.PIPE
).communicate()[0]
output = json.loads(attributes)
except Exception, e:
self.log.error("JbossApiCollector: There was an exception %s", e)
output = ''
return output
def is_number(self, value):
return (isinstance(value, (int, long, float)) and
not isinstance(value, bool))
def string_fix(self, s):
return re.sub(r"[^a-zA-Z0-9_]", "_", s)
def string_regex(self, pattern, s):
tmp_result = re.match(pattern, s)
return tmp_result.group(1)
def collect(self):
for host in self.config['hosts']:
matches = re.search(
'^([^:]*):([^@]*)@([^:]*):([^:]*):?(.*)', host)
if not matches:
continue
current_host = matches.group(3)
current_port = int(matches.group(4))
current_proto = matches.group(5)
current_user = matches.group(1)
current_pword = matches.group(2)
# Call get_stats for each instance of jboss
self.get_stats(current_host, current_port, current_proto,
current_user, current_pword)
return True