private JMXConnector initJMXConnector(String svcName)
      throws IOException, AttachNotSupportedException, AgentLoadException,
          AgentInitializationException {
    int pid = PlatformUtils.getServicePid(svcName);
    log.info("{} service pid {}", svcName, pid);

    VirtualMachine vm = VirtualMachine.attach(String.valueOf(pid));
    try {
      String connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
      if (connectorAddress == null) {
        String agent =
            Strings.join(
                File.separator,
                vm.getSystemProperties().getProperty("java.home"),
                "lib",
                "management-agent.jar");
        vm.loadAgent(agent);

        connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
      }

      JMXServiceURL serviceURL = new JMXServiceURL(connectorAddress);
      return JMXConnectorFactory.connect(serviceURL);
    } finally {
      vm.detach();
    }
  }
Esempio n. 2
0
  public JMXConnector createJMXConnector(String id)
      throws IOException, AgentLoadException, AgentInitializationException,
          AttachNotSupportedException {

    // attach to the target application
    VirtualMachine vm = VirtualMachine.attach(id);

    // get the connector address
    String connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);

    // no connector address, so we start the JMX agent
    if (connectorAddress == null) {
      String agent =
          vm.getSystemProperties().getProperty("java.home")
              + File.separator
              + "lib"
              + File.separator
              + "management-agent.jar";
      vm.loadAgent(agent);

      // agent is started, get the connector address
      connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
    }

    // establish connection to connector server
    JMXServiceURL url = new JMXServiceURL(connectorAddress);
    return JMXConnectorFactory.connect(url);
  }
  private void attachAgent()
      throws IOException, AttachNotSupportedException, AgentLoadException,
          AgentInitializationException {

    String name = ManagementFactory.getRuntimeMXBean().getName();
    String pid = name.substring(0, name.indexOf("@"));
    System.out.println(name);

    final VirtualMachine m = VirtualMachine.attach(pid);

    m.loadAgent("target/notsoserial-1.0-SNAPSHOT.jar");
  }
Esempio n. 4
0
  /**
   * @param args
   * @throws Exception
   */
  public static void main(final String[] args) throws Exception {
    if (args == null || args.length < 1) {
      throw new IllegalStateException("args");
    }

    final String pid = args[0];

    final File thisJar = getJarFile();

    final VirtualMachine vm = VirtualMachine.attach(pid);
    vm.loadAgent(thisJar.getAbsolutePath(), "");
    vm.detach();
  }
  @Nonnull
  protected MBeanServerConnection connectToMbeanServer(@Nonnull String pid) throws IOException {
    VirtualMachine vm;
    try {
      Integer.parseInt(pid);
    } catch (Exception e) {
      logger.warn("Exception parsing PID '{}'", pid, e);
    }
    try {
      vm = VirtualMachine.attach(pid);
    } catch (Exception e) {
      throw new IllegalStateException("Exception attaching VM with PID '" + pid + "'", e);
    }

    logger.trace("VM Agent Properties");
    for (String key : new TreeSet<String>(vm.getAgentProperties().stringPropertyNames())) {
      logger.trace("\t {}: {}", key, vm.getAgentProperties().get(key));
    }
    logger.trace("VM System Properties");
    for (String key : new TreeSet<String>(vm.getSystemProperties().stringPropertyNames())) {
      logger.trace("\t {}: {}", key, vm.getSystemProperties().get(key));
    }

    String connectorAddress =
        vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
    if (connectorAddress == null) {
      String agent =
          vm.getSystemProperties().getProperty("java.home")
              + File.separator
              + "lib"
              + File.separator
              + "management-agent.jar";
      try {
        vm.loadAgent(agent);
      } catch (Exception e) {
        throw new IllegalStateException("Exception loading agent " + agent);
      }
      connectorAddress =
          vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
    }

    if (connectorAddress == null) {
      throw new IllegalStateException(
          "Could not attach to pid: " + pid + ". No connector available");
    }
    logger.trace("Connect to {} ...", connectorAddress);

    JMXConnector connector = JMXConnectorFactory.connect(new JMXServiceURL(connectorAddress));

    return connector.getMBeanServerConnection();
  }
    private synchronized void loadManagementAgent() throws IOException {
      VirtualMachine vm = null;
      String name = String.valueOf(vmid);
      try {
        vm = VirtualMachine.attach(name);
      } catch (AttachNotSupportedException x) {
        throw new IOException(x);
      }
      // try to enable local JMX via jcmd command
      if (!loadManagementAgentViaJcmd(vm)) {
        // load the management agent into the target VM
        loadManagementAgentViaJar(vm);
      }

      // get the connector address
      Properties agentProps = vm.getAgentProperties();
      address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);

      vm.detach();
    }
Esempio n. 7
0
  public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    if (args.length != 4) {
      System.err.println("Please provide process id zabbix-host zabbix-port host-guid");
      System.exit(-1);
    }
    String processPid = args[0];
    String zabbixHost = args[1];
    String zabbixPort = args[2];
    String hostGuid = args[3];

    VirtualMachine vm = VirtualMachine.attach(processPid);
    String connectorAddr =
        vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
    if (connectorAddr == null) {
      String agent =
          vm.getSystemProperties().getProperty("java.home")
              + File.separator
              + "lib"
              + File.separator
              + "management-agent.jar";
      vm.loadAgent(agent);
      connectorAddr =
          vm.getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
    }
    JMXServiceURL serviceURL = new JMXServiceURL(connectorAddr);
    JMXConnector connector = JMXConnectorFactory.connect(serviceURL);
    MBeanServerConnection mbsc = connector.getMBeanServerConnection();
    ObjectName objName = new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
    Set<ObjectName> mbeans = mbsc.queryNames(objName, null);
    for (ObjectName name : mbeans) {
      ThreadMXBean threadBean;
      threadBean =
          ManagementFactory.newPlatformMXBeanProxy(mbsc, name.toString(), ThreadMXBean.class);
      long threadIds[] = threadBean.getAllThreadIds();
      for (long threadId : threadIds) {
        ThreadInfo threadInfo = threadBean.getThreadInfo(threadId);
        System.out.println(threadInfo.getThreadName() + " / " + threadInfo.getThreadState());
      }
    }
  }
  private Stream<VirtualMachine> attach(VirtualMachineDescriptor vmDescriptor) {
    try {
      com.sun.tools.attach.VirtualMachine vm =
          com.sun.tools.attach.VirtualMachine.attach(vmDescriptor);
      String vmArgs = vm.getAgentProperties().getProperty(VM_ARGS);

      String id = vmDescriptor.id();
      String displayName = vmDescriptor.displayName();
      boolean agentLoaded = vmArgs.contains(AGENT_NAME);
      String userDir = getUserDir(vm);

      return Stream.of(new VirtualMachine(id, displayName, agentLoaded, userDir));
    } catch (AttachNotSupportedException e) {
      logger.warn(e.getMessage());
    } catch (IOException e) {
      if (!noSuchProcess(e)) {
        logger.warn(e.getMessage(), e);
      }
    }
    return Stream.empty();
  }
Esempio n. 9
0
    private JMXServiceURL getURLForPid(String pid) throws Exception {

      // attach to the target application
      final VirtualMachine vm = VirtualMachine.attach(pid);

      // get the connector address
      String connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);

      // no connector address, so we start the JMX agent
      if (connectorAddress == null) {
        String agent =
            vm.getSystemProperties().getProperty("java.home")
                + File.separator
                + "lib"
                + File.separator
                + "management-agent.jar";
        vm.loadAgent(agent);

        // agent is started, get the connector address
        connectorAddress = vm.getAgentProperties().getProperty(CONNECTOR_ADDRESS);
        assert connectorAddress != null;
      }
      return new JMXServiceURL(connectorAddress);
    }