-
Notifications
You must be signed in to change notification settings - Fork 21
/
JMX.pm
624 lines (565 loc) · 21.6 KB
/
JMX.pm
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
package Collectd::Plugin::JMX;
use Collectd qw(:all);
#WARNING WIP DOES NOT WORK YET
=head1 NAME
Collectd::Plugins::JMX - Monitor JMX stats from within perl from within Collectd with an embedded java JVM. Fun!
=head1 VERSION
Version 1
=cut
our $VERSION = '1';
=head1 SYNOPSIS
This is a collectd plugin for monitoring JMX stats. In progress!
In your collectd config:
<LoadPlugin "perl">
Globals true
</LoadPlugin>
<Plugin "perl">
BaseName "Collectd::Plugins"
LoadPlugin "JMX"
<Plugin "JMX">
... TBD ...
</Plugin>
</Plugin>
=head1 AUTHOR
Mark Steele, C<< <mark at control-alt-del.org> >>, heavily inspired/code borrowed from tcollector
=cut
## Shameless rip from here: https://github.com/tsuna/tcollector/blob/master/collectors/0/hadoop_datanode_jmx.py
## and https://github.com/tsuna/tcollector/blob/master/stumbleupon/monitoring/jmx.java
use Inline Java => <<'END_OF_JAVA_CODE', AUTOSTUDY => 1, CLASSPATH=> '/usr/java/jdk1.7.0_45/lib/tools.jar',PACKAGE => 'main';
//
//
// This file is part of OpenTSDB.
// Copyright (C) 2010 The tcollector Authors.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or (at your
// option) any later version. This program is distributed in the hope that it
// will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details. You should have received a copy
// of the GNU Lesser General Public License along with this program. If not,
// see <http://www.gnu.org/licenses/>.
/** Quick CLI tool to get JMX MBean attributes. */
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.openmbean.TabularData;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
// Composite Data
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.OpenType;
// Sun specific
import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
// Sun private
import sun.management.ConnectorAddressLink;
import sun.jvmstat.monitor.HostIdentifier;
import sun.jvmstat.monitor.MonitoredHost;
import sun.jvmstat.monitor.MonitoredVm;
import sun.jvmstat.monitor.MonitoredVmUtil;
import sun.jvmstat.monitor.VmIdentifier;
final class jmx {
private static final String LOCAL_CONNECTOR_ADDRESS =
"com.sun.management.jmxremote.localConnectorAddress";
private static void usage() {
System.out.println("Usage:\n"
+ " jmx -l Lists all reachable VMs.\n"
+ " jmx <JVM> Lists all MBeans for this JVM (PID or regexp).\n"
+ " jmx <JVM> <MBean> Prints all the attributes of this MBean.\n"
+ " jmx <JVM> <MBean> <attr> Prints the matching attributes of this MBean.\n"
+ "\n"
+ "You can pass multiple <MBean> <attr> pairs to match multiple different\n"
+ "attributes for different MBeans. For example:\n"
+ " jmx --long JConsole Class Count Thread Total Garbage Collection\n"
+ " LoadedClassCount 2808 java.lang:type=ClassLoading\n"
+ " UnloadedClassCount 0 java.lang:type=ClassLoading\n"
+ " TotalLoadedClassCount 2808 java.lang:type=ClassLoading\n"
+ " CollectionCount 0 java.lang:type=GarbageCollector,name=ConcurrentMarkSweep\n"
+ " CollectionTime 0 java.lang:type=GarbageCollector,name=ConcurrentMarkSweep\n"
+ " CollectionCount 1 java.lang:type=GarbageCollector,name=ParNew\n"
+ " CollectionTime 19 java.lang:type=GarbageCollector,name=ParNew\n"
+ " TotalStartedThreadCount 43 java.lang:type=Threading\n"
+ "The command above searched for a JVM with `JConsole' in its name, and then searched\n"
+ "for MBeans with `Class' in the name and `Count' in the attribute (first 3 matches\n"
+ "in this output), MBeans with `Thread' in the name and `Total' in the attribute (last\n"
+ "line in the output) and MBeans matching `Garbage' with a `Collection' attribute.\n"
+ "\n"
+ "Other flags you can pass:\n"
+ " --long Print a longer but more explicit output for each value.\n"
+ " --timestamp Print a timestamp at the beginning of each line.\n"
+ " --watch N Reprint the output every N seconds.\n"
+ "\n"
+ "Return value:\n"
+ " 0: Everything OK.\n"
+ " 1: Invalid usage or unexpected error.\n"
+ " 2: No JVM matched.\n"
+ " 3: No MBean matched.\n"
+ " 4: No attribute matched for the MBean(s) selected.");
}
private static void fatal(final int rv, final String errmsg) {
System.err.println(errmsg);
System.exit(rv);
throw new AssertionError("You should never see this, really.");
}
public static void main(final String[] args) throws Exception {
if (args.length == 0 || "-h".equals(args[0]) || "--help".equals(args[0])) {
usage();
System.exit(args.length == 0 ? 1 : 0);
return;
}
int current_arg = 0;
int watch = 0;
boolean long_output = false;
boolean print_timestamps = false;
while (current_arg < args.length) {
if ("--watch".equals(args[current_arg])) {
current_arg++;
try {
watch = Integer.parseInt(args[current_arg]);
} catch (NumberFormatException e) {
fatal(1, "Invalid value for --watch: " + e.getMessage());
return;
}
if (watch < 1) {
fatal(1, "Invalid value for --watch: " + watch);
}
current_arg++;
} else if ("--long".equals(args[current_arg])) {
long_output = true;
current_arg++;
} else if ("--timestamp".equals(args[current_arg])) {
print_timestamps = true;
current_arg++;
} else {
break;
}
}
if (current_arg == args.length) {
usage();
fatal(1, "error: Missing argument (-l or JVM specification).");
return;
}
HashMap<Integer, JVM> vms = getJVMs();
if ("-l".equals(args[current_arg])) {
printVmList(vms.values());
return;
}
final JVM jvm = selectJVM(args[current_arg++], vms);
vms = null;
final JMXConnector connection = JMXConnectorFactory.connect(jvm.jmxUrl());
try {
final MBeanServerConnection mbsc = connection.getMBeanServerConnection();
if (args.length == current_arg) {
for (final ObjectName mbean : listMBeans(mbsc)) {
System.out.println(mbean);
}
return;
}
final TreeMap<ObjectName, Pattern> objects = selectMBeans(args, current_arg, mbsc);
if (objects.isEmpty()) {
fatal(3, "No MBean matched your query in " + jvm.name());
return;
}
do {
boolean found = false;
for (final Map.Entry<ObjectName, Pattern> entry : objects.entrySet()) {
final ObjectName object = entry.getKey();
final MBeanInfo mbean = mbsc.getMBeanInfo(object);
final Pattern wanted = entry.getValue();
for (final MBeanAttributeInfo attr : mbean.getAttributes()) {
if (wanted == null || wanted.matcher(attr.getName()).find()) {
dumpMBean(long_output, print_timestamps, mbsc, object, attr);
found = true;
}
}
}
if (!found) {
fatal(4, "No attribute of " + objects.keySet()
+ " matched your query in " + jvm.name());
return;
}
System.out.flush();
Thread.sleep(watch * 1000);
} while (watch > 0);
} finally {
connection.close();
}
}
private static TreeMap<ObjectName, Pattern> selectMBeans(final String[] args,
final int current_arg,
final MBeanServerConnection mbsc) throws IOException {
final TreeMap<ObjectName, Pattern> mbeans = new TreeMap<ObjectName, Pattern>();
for (int i = current_arg; i < args.length; i += 2) {
final Pattern object_re = compile_re(args[i]);
final Pattern attr_re = i + 1 < args.length ? compile_re(args[i + 1]) : null;
for (final ObjectName o : listMBeans(mbsc)) {
if (object_re.matcher(o.toString()).find()) {
mbeans.put(o, attr_re);
}
}
}
return mbeans;
}
private static void dumpMBean(final boolean long_output,
final boolean print_timestamps,
final MBeanServerConnection mbsc,
final ObjectName object,
final MBeanAttributeInfo attr) throws Exception {
final String name = attr.getName();
Object value = null;
try {
value = mbsc.getAttribute(object, name);
} catch (Exception e) {
// Above may raise errors for some attributes like
// CollectionUsage
return;
}
if (value instanceof TabularData) {
final TabularData tab = (TabularData) value;
int i = 0;
for (final Object o : tab.keySet()) {
dumpMBeanValue(long_output, print_timestamps, object, name + "." + i, o);
i++;
}
} else if (value instanceof CompositeDataSupport){
CompositeDataSupport cds = (CompositeDataSupport) value;
CompositeType ct = cds.getCompositeType();
for (final String item: ct.keySet()){
dumpMBeanValue(long_output, print_timestamps, object, name + "." + item, cds.get(item));
}
} else {
dumpMBeanValue(long_output, print_timestamps, object, name, value);
}
}
private static void dumpMBeanValue(final boolean long_output,
final boolean print_timestamps,
final ObjectName object,
final String name,
final Object value) {
// Ignore non numeric values
if ((value instanceof String)||
(value instanceof String[])||
(value instanceof Boolean)) {
return;
}
final StringBuilder buf = new StringBuilder();
final long timestamp = System.currentTimeMillis() / 1000;
if (print_timestamps) {
buf.append(timestamp).append('\t');
}
if (value instanceof Object[]) {
for (final Object o : (Object[]) value) {
buf.append(o).append('\t');
}
if (buf.length() > 0) {
buf.setLength(buf.length() - 1);
}
} else {
buf.append(name).append('\t').append(value);
}
if (long_output) {
buf.append('\t').append(object);
}
buf.append('\n');
System.out.print(buf);
}
private static ArrayList<ObjectName> listMBeans(final MBeanServerConnection mbsc) throws IOException {
ArrayList<ObjectName> mbeans = new ArrayList<ObjectName>(mbsc.queryNames(null, null));
Collections.sort(mbeans, new Comparator<ObjectName>() {
public int compare(final ObjectName a, final ObjectName b) {
return a.toString().compareTo(b.toString());
}
});
return mbeans;
}
private static Pattern compile_re(final String re) {
try {
return Pattern.compile(re);
} catch (PatternSyntaxException e) {
fatal(1, "Invalid regexp: " + re + ", " + e.getMessage());
throw new AssertionError("Should never be here");
}
}
private static final String MAGIC_STRING = "this.is.jmx.magic";
private static JVM selectJVM(final String selector,
final HashMap<Integer, JVM> vms) {
String error = null;
try {
final int pid = Integer.parseInt(selector);
if (pid < 2) {
throw new IllegalArgumentException("Invalid PID: " + pid);
}
final JVM jvm = vms.get(pid);
if (jvm != null) {
return jvm;
}
error = "Couldn't find a JVM with PID " + pid;
} catch (NumberFormatException e) {
/* Ignore. */
}
if (error == null) {
try {
final Pattern p = compile_re(selector);
final ArrayList<JVM> matches = new ArrayList<JVM>(2);
for (final JVM jvm : vms.values()) {
if (p.matcher(jvm.name()).find()) {
matches.add(jvm);
}
}
// Exclude ourselves from the matches.
System.setProperty(MAGIC_STRING,
"LOL Java processes can't get their own PID");
final String me = jmx.class.getName();
final Iterator<JVM> it = matches.iterator();
while (it.hasNext()) {
final JVM jvm = it.next();
final String name = jvm.name();
// Ignore other long running jmx clients too.
if (name.contains("--watch") && name.contains(me)) {
it.remove();
continue;
}
final VirtualMachine vm = VirtualMachine.attach(String.valueOf(jvm.pid()));
try {
if (vm.getSystemProperties().containsKey(MAGIC_STRING)) {
it.remove();
continue;
}
} finally {
vm.detach();
}
}
System.clearProperty(MAGIC_STRING);
if (matches.size() == 0) {
error = "No JVM matched your regexp " + selector;
} else if (matches.size() > 1) {
printVmList(matches);
error = matches.size() + " JVMs matched your regexp " + selector
+ ", it's too ambiguous, please refine it.";
} else {
return matches.get(0);
}
} catch (PatternSyntaxException e) {
error = "Invalid pattern: " + selector + ", " + e.getMessage();
} catch (Exception e) {
e.printStackTrace();
error = "Unexpected Exception: " + e.getMessage();
}
}
fatal(2, error);
return null;
}
private static void printVmList(final Collection<JVM> vms) {
final ArrayList<JVM> sorted_vms = new ArrayList<JVM>(vms);
Collections.sort(sorted_vms, new Comparator<JVM>() {
public int compare(final JVM a, final JVM b) {
return a.pid() - b.pid();
}
});
for (final JVM jvm : sorted_vms) {
System.out.println(jvm.pid() + "\t" + jvm.name());
}
}
private static final class JVM {
final int pid;
final String name;
String address;
public JVM(final int pid, final String name, final String address) {
if (name.isEmpty()) {
throw new IllegalArgumentException("empty name");
}
this.pid = pid;
this.name = name;
this.address = address;
}
public int pid() {
return pid;
}
public String name() {
return name;
}
public JMXServiceURL jmxUrl() {
if (address == null) {
ensureManagementAgentStarted();
}
try {
return new JMXServiceURL(address);
} catch (Exception e) {
throw new RuntimeException("Error", e);
}
}
public void ensureManagementAgentStarted() {
if (address != null) { // already started
return;
}
VirtualMachine vm;
try {
vm = VirtualMachine.attach(String.valueOf(pid));
} catch (AttachNotSupportedException e) {
throw new RuntimeException("Failed to attach to " + this, e);
} catch (IOException e) {
throw new RuntimeException("Failed to attach to " + this, e);
}
try {
// java.sun.com/javase/6/docs/technotes/guides/management/agent.html#gdhkz
// + code mostly stolen from JConsole's code.
final String home = vm.getSystemProperties().getProperty("java.home");
// Normally in ${java.home}/jre/lib/management-agent.jar but might
// be in ${java.home}/lib in build environments.
String agent = home + File.separator + "jre" + File.separator
+ "lib" + File.separator + "management-agent.jar";
File f = new File(agent);
if (!f.exists()) {
agent = home + File.separator + "lib" + File.separator
+ "management-agent.jar";
f = new File(agent);
if (!f.exists()) {
throw new RuntimeException("Management agent not found");
}
}
agent = f.getCanonicalPath();
try {
vm.loadAgent(agent, "com.sun.management.jmxremote");
} catch (AgentLoadException e) {
throw new RuntimeException("Failed to load the agent into " + this, e);
} catch (AgentInitializationException e) {
throw new RuntimeException("Failed to initialize the agent into " + this, e);
}
address = (String) vm.getAgentProperties().get(LOCAL_CONNECTOR_ADDRESS);
} catch (IOException e) {
throw new RuntimeException("Error while loading agent into " + this, e);
} finally {
try {
vm.detach();
} catch (IOException e) {
throw new RuntimeException("Failed to detach from " + vm + " = " + this, e);
}
}
if (address == null) {
throw new RuntimeException("Couldn't start the management agent.");
}
}
public String toString() {
return "JVM(" + pid + ", \"" + name + "\", "
+ (address == null ? null : '"' + address + '"') + ')';
}
}
/**
* Returns a map from PID to JVM.
*/
private static HashMap<Integer, JVM> getJVMs() throws Exception {
final HashMap<Integer, JVM> vms = new HashMap<Integer, JVM>();
getMonitoredVMs(vms);
getAttachableVMs(vms);
return vms;
}
private static void getMonitoredVMs(final HashMap<Integer, JVM> out) throws Exception {
final MonitoredHost host =
MonitoredHost.getMonitoredHost(new HostIdentifier((String) null));
@SuppressWarnings("unchecked")
final Set<Integer> vms = host.activeVms();
for (final Integer pid : vms) {
try {
final VmIdentifier vmid = new VmIdentifier(pid.toString());
final MonitoredVm vm = host.getMonitoredVm(vmid);
out.put(pid, new JVM(pid, MonitoredVmUtil.commandLine(vm),
ConnectorAddressLink.importFrom(pid)));
vm.detach();
} catch (Exception x) {
System.err.println("Ignoring exception:");
x.printStackTrace();
}
}
}
private static void getAttachableVMs(final HashMap<Integer, JVM> out) {
for (final VirtualMachineDescriptor vmd : VirtualMachine.list()) {
int pid;
try {
pid = Integer.parseInt(vmd.id());
} catch (NumberFormatException e) {
System.err.println("Ignoring invalid vmd.id(): " + vmd.id()
+ ' ' + e.getMessage());
continue;
}
if (out.containsKey(pid)) {
continue;
}
try {
final VirtualMachine vm = VirtualMachine.attach(vmd);
out.put(pid, new JVM(pid, String.valueOf(pid),
(String) vm.getAgentProperties().get(LOCAL_CONNECTOR_ADDRESS)));
vm.detach();
} catch (AttachNotSupportedException e) {
System.err.println("VM not attachable: " + vmd.id()
+ ' ' + e.getMessage());
} catch (IOException e) {
System.err.println("Could not attach: " + vmd.id()
+ ' ' + e.getMessage());
}
}
}
}
END_OF_JAVA_CODE
my @options = ('--long','--timestamp','DataNode','hadoop','','Threading','Count|Time$','OperatingSystem','OpenFile','GarbageCollector','Collection');
#print jmx->main(\@options);
#print jmx->main(${('-l')});
my $blah;
#plugin_register (TYPE_READ, 'JMX', 'jmx_read');
#plugin_register (TYPE_CONFIG, "JMX", "jmx_config");
#
#sub jmx_config {
# my ($ci) = @_;
# foreach my $item (@{$ci->{'children'}}) {
# my $key = lc($item->{'key'});
# my $val = $item->{'values'}->[0];
# if ($key eq 'blah' ) {
# $blah = $val;
# }
# }
# return 1;
#}
#
#sub jmx_read
#{
#
# my $vl = {};
# $vl->{'plugin'} = 'rabbitmq';
# $vl->{'type'} = 'rabbitmq';
#
# foreach my $result (@{$ref}) {
# $vl->{'plugin_instance'} = $result->{'vhost'};
# $vl->{'type_instance'} = $result->{'name'};
# $vl->{'plugin_instance'} =~ s#[/-]#_#g;
# $vl->{'type_instance'} =~ s#[/-]#_#g;
# $vl->{'values'} = [
# $result->{'messages'} ? $result->{'messages'} : 0,
# ];
# plugin_log(LOG_ERR, "RabbitMQ: dispatching stats for " . $result->{'vhost'} . '/' . $result->{'name'});
# plugin_dispatch_values($vl);
# }
# plugin_log(LOG_ERR, "RabbitMQ: done processing results");
# return 1;
#}
#
#1;