private void attachAgent(final String targetJvmPid, final String agentJarPath, final String cfg) throws Exception { VirtualMachine vmObj = null; try { vmObj = VirtualMachine.attach(targetJvmPid); if (vmObj != null) { vmObj.loadAgent(agentJarPath, cfg); } } finally { if (null != vmObj) { vmObj.detach(); } } }
public String getId() { return descriptor.id(); }
public String getDisplayName() { return descriptor.displayName(); }
String name = String.valueOf(vmid); try { vm = VirtualMachine.attach(name); } catch (AttachNotSupportedException x) { throw new IOException(x.getMessage(), x); String home = vm.getSystemProperties().getProperty("java.home"); vm.loadAgent(agent, "com.sun.management.jmxremote"); } catch (AgentLoadException x) { throw new IOException(x.getMessage(), x); } catch (AgentInitializationException x) { throw new IOException(x.getMessage(), x); Properties agentProps = vm.getAgentProperties(); String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); vm.detach();
public static JMXServiceURL extractJMXServiceURLFromPid(String pid) throws IOException { try { VirtualMachine vm = VirtualMachine.attach(pid); try { String connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS); if (connectorAddress == null) { String agent = vm.getSystemProperties().getProperty("java.home") + File.separator + "lib" + File.separator + "management-agent.jar"; vm.loadAgent(agent); connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS); } return new JMXServiceURL(connectorAddress); } finally { vm.detach(); } } catch(Exception e) { throw new IOException(e); } }
private static void getAttachableVMs(Map<Integer, LocalVirtualMachine> map, Map<Integer, LocalVirtualMachine> existingVmMap) List<VirtualMachineDescriptor> vms = VirtualMachine.list(); for (VirtualMachineDescriptor vmd : vms) Integer vmid = Integer.valueOf(vmd.id()); if (!map.containsKey(vmid) && !existingVmMap.containsKey(vmid)) try VirtualMachine vm = VirtualMachine.attach(vmd); attachable = true; Properties agentProps = vm.getAgentProperties(); address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); vm.detach(); x.printStackTrace(System.err); new LocalVirtualMachine(vmid.intValue(), vmd.displayName(), attachable, address));
vm = VirtualMachine.attach( String.valueOf( pid ) ); Properties agentProps = vm.getAgentProperties(); String address = (String) agentProps.get( "com.sun.management.jmxremote.localConnectorAddress" ); address = vm.startLocalManagementAgent(); return new LocalVirtualMachine( address, vm.getSystemProperties() ); throw new IOException( x.getMessage(), x ); vm.detach();
VirtualMachine vm = VirtualMachine.attach(String.valueOf(pid)); try { Properties agentProps = vm.getAgentProperties(); connectorAddress = agentProps.getProperty(PROPERTY_LOCAL_CONNECTOR_ADDRESS); vm.startLocalManagementAgent(); agentProps = vm.getAgentProperties(); connectorAddress = agentProps.getProperty(PROPERTY_LOCAL_CONNECTOR_ADDRESS); vm.detach();
public static LocalVirtualMachine getLocalVirtualMachine(int vmid) throws Exception { Map<Integer, LocalVirtualMachine> map = getAllVirtualMachines(); LocalVirtualMachine lvm = map.get(vmid); if (lvm == null) { // Check if the VM is attachable but not included in the list // if it's running with a different security context. // For example, Windows services running // local SYSTEM account are attachable if you have Adminstrator // privileges. boolean attachable = false; String address = null; String name = String.valueOf(vmid); // default display name to pid VirtualMachine vm = VirtualMachine.attach(name); attachable = true; Properties agentProps = vm.getAgentProperties(); address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); vm.detach(); lvm = new LocalVirtualMachine(vmid, name, attachable, address); } return lvm; }
@Override public int getTaskPort(BTraceTask task) { VirtualMachine vm = null; try { vm = VirtualMachine.attach(String.valueOf(task.getPid())); String portStr = vm.getSystemProperties().getProperty(PORT_PROPERTY); return portStr != null ? Integer.parseInt(portStr) : findFreePort(); } catch (AttachNotSupportedException ex) { Logger.getLogger(PortLocatorImpl.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(PortLocatorImpl.class.getName()).log(Level.SEVERE, null, ex); } finally { if (vm != null) { try { vm.detach(); } catch (IOException e) { LOGGER.log(Level.SEVERE, null, e); } } } return findFreePort(); }
private static String getJavaHome(BTraceTask btt) { try { VirtualMachine vm = VirtualMachine.attach(String.valueOf(btt.getPid())); return vm.getSystemProperties().getProperty("java.home"); } catch (AttachNotSupportedException e) { LOGGER.log(Level.SEVERE, null, e); } catch (IOException e) { LOGGER.log(Level.SEVERE, null, e); } return null; } }
public static LocalVirtualMachine getDelegateMachine(VirtualMachine vm) throws IOException { // privileges. boolean attachable = false; String address = null; String name = String.valueOf(vm.id()); // default display name to pid attachable = true; Properties agentProps = vm.getAgentProperties(); address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); vm.detach(); return new LocalVirtualMachine(Integer.parseInt(vm.id()), name, attachable, address); }
@Override public List<VMDescriptorDTO> listLocalJVMs() { List<VMDescriptorDTO> rc = new ArrayList<>(); try { List<VirtualMachineDescriptor> processes = VirtualMachine.list(); for (VirtualMachineDescriptor process : processes) { VMDescriptorDTO dto = new VMDescriptorDTO(process); dto.setAgentUrl(agentStatus(dto.getId())); String alias = getVmAlias(process.displayName(), dto.getAgentUrl()); dto.setAlias(alias); // provide fine grained url details if (dto.getAgentUrl() != null) { try { URL url = new URL(dto.getAgentUrl()); dto.setScheme(url.getProtocol()); dto.setHostname(url.getHost()); dto.setPort(url.getPort()); dto.setPath(url.getPath()); } catch (Exception e) { // ignore } } rc.add(dto); } } catch (Exception e) { LOG.warn("Failed to get local JVM processes due to: {}", e.getMessage()); throw new RuntimeException("Failed to get local JVM processes due to: " + e.getMessage(), e); } return rc; }
List<VirtualMachineDescriptor> vms = VirtualMachine.list(); for (VirtualMachineDescriptor desc : vms) { VirtualMachine vm; try { vm = VirtualMachine.attach(desc); } catch (AttachNotSupportedException e) { continue; } Properties props = vm.getAgentProperties(); String connectorAddress = props.getProperty("com.sun.management.jmxremote.localConnectorAddress"); if (connectorAddress == null) { continue; } JMXServiceURL url = new JMXServiceURL(connectorAddress); JMXConnector connector = JMXConnectorFactory.connect(url); try { MBeanServerConnection mbeanConn = connector.getMBeanServerConnection(); Set<ObjectName> beanSet = mbeanConn.queryNames(null, null); ... } finally { jmxConnector.close(); } }
vm = VirtualMachine.attach(pid); } catch (AttachNotSupportedException x) { IOException ioe = new IOException(x.getMessage()); ioe.initCause(x); throw ioe; Properties agentProps = vm.getAgentProperties(); String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); String home = vm.getSystemProperties().getProperty("java.home"); vm.loadAgent(agentPath, "com.sun.management.jmxremote"); } catch (AgentLoadException x) { IOException ioe = new IOException(x.getMessage()); ioe.initCause(x); throw ioe; } catch (AgentInitializationException x) { IOException ioe = new IOException(x.getMessage()); ioe.initCause(x); throw ioe; agentProps = vm.getAgentProperties(); address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); vm.detach();
vm = VirtualMachine.attach(pid); Properties agentProps = vm.getAgentProperties(); String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); String home = vm.getSystemProperties().getProperty("java.home"); vm.loadAgent(agentPath, "com.sun.management.jmxremote"); agentProps = vm.getAgentProperties(); address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); vm.detach();
try vm = VirtualMachine.attach(name); IOException ioe = new IOException(x.getMessage()); ioe.initCause(x); throw ioe; String home = vm.getSystemProperties().getProperty("java.home"); vm.loadAgent(agent, "com.sun.management.jmxremote"); IOException ioe = new IOException(x.getMessage()); ioe.initCause(x); throw ioe; IOException ioe = new IOException(x.getMessage()); ioe.initCause(x); throw ioe; Properties localProperties = vm.getSystemProperties(); this.address = ((String) localProperties .get("com.sun.management.jmxremote.localConnectorAddress")); Properties agentProps = vm.getAgentProperties(); address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); vm.detach();