Created
June 27, 2018 00:25
-
-
Save kay/489f6727e69e57639502df8a21d190cb to your computer and use it in GitHub Desktop.
List JVM monitoring metrics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.neverfear.services.jvmmonitor.api; | |
import static java.lang.System.out; | |
import java.net.URISyntaxException; | |
import java.util.List; | |
import java.util.Set; | |
import sun.jvmstat.monitor.HostIdentifier; | |
import sun.jvmstat.monitor.Monitor; | |
import sun.jvmstat.monitor.MonitorException; | |
import sun.jvmstat.monitor.MonitoredHost; | |
import sun.jvmstat.monitor.MonitoredVm; | |
import sun.jvmstat.monitor.VmIdentifier; | |
public class ListJvmMonitorMetrics { | |
public static VmIdentifier createVmId(final Integer id) { | |
try { | |
return new VmIdentifier(id.toString()); | |
} catch (final URISyntaxException e) { | |
// Shoudn't happen | |
throw new AssertionError(e); | |
} | |
} | |
private static final HostIdentifier LOCALHOST_IDENTIFIER = createHostId(null); | |
private static void logListOfUnmatchingMonitors(final MonitoredVm vm) | |
throws MonitorException { | |
final List<Monitor> allMonitors = vm.findByPattern(".*"); | |
for (final Monitor monitor : allMonitors) { | |
final Object value = monitor.getValue(); | |
out.format("monitor: %s %s %s (%s)=%s%n", | |
monitor.getName(), | |
monitor.getVariability(), | |
monitor.getUnits(), | |
value != null ? value.getClass().getSimpleName() : null, | |
value); | |
} | |
} | |
private static HostIdentifier createHostId(final String str) { | |
try { | |
return new HostIdentifier(str); | |
} catch (final URISyntaxException e) { | |
throw new IllegalArgumentException(e); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
final MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(LOCALHOST_IDENTIFIER); | |
MonitoredHost host = monitoredHost; | |
final Set<Integer> activeVms = host.activeVms(); | |
for (final Integer vmId : activeVms) { | |
out.println("VM ID: " + vmId); | |
final MonitoredVm monitoredVm = host.getMonitoredVm(createVmId(vmId)); | |
logListOfUnmatchingMonitors(monitoredVm); | |
out.println(); | |
out.println(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment