private static boolean test(String proto, MBeanServer mbs) throws Exception { System.out.println("Testing for proto " + proto); JMXConnectorServer cs; JMXServiceURL url = new JMXServiceURL(proto, null, 0); try { cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); } catch (MalformedURLException e) { System.out.println("System does not recognize URL: " + url + "; ignoring"); return true; } cs.start(); JMXServiceURL addr = cs.getAddress(); JMXServiceURL rmiurl = new JMXServiceURL("rmi", null, 0); JMXConnector client = JMXConnectorFactory.connect(addr); MBeanServerConnection mbsc = client.getMBeanServerConnection(); ObjectName on = new ObjectName("x:proto=" + proto + ",ok=yes"); mbsc.createMBean( RMIConnectorServer.class.getName(), on, mletName, new Object[] {rmiurl, null}, new String[] {JMXServiceURL.class.getName(), Map.class.getName()}); System.out.println("Successfully deserialized with " + proto); mbsc.unregisterMBean(on); client.close(); cs.stop(); return true; }
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 void tryConnect(boolean requireRemoteSSL) throws IOException { if (jmxUrl == null && "localhost".equals(hostName) && port == 0) { // Monitor self this.jmxc = null; this.mbsc = ManagementFactory.getPlatformMBeanServer(); this.server = Snapshot.newSnapshot(mbsc); } else { // Monitor another process if (lvm != null) { if (!lvm.isManageable()) { lvm.startManagementAgent(); if (!lvm.isManageable()) { // FIXME: what to throw throw new IOException(lvm + "not manageable"); } } if (this.jmxUrl == null) { this.jmxUrl = new JMXServiceURL(lvm.connectorAddress()); } } Map<String, Object> env = new HashMap<String, Object>(); if (requireRemoteSSL) { env.put("jmx.remote.x.check.stub", "true"); } // Need to pass in credentials ? if (userName == null && password == null) { if (isVmConnector()) { // Check for SSL config on reconnection only if (stub == null) { checkSslConfig(); } this.jmxc = new RMIConnector(stub, null); jmxc.connect(env); } else { this.jmxc = JMXConnectorFactory.connect(jmxUrl, env); } } else { env.put(JMXConnector.CREDENTIALS, new String[] {userName, password}); if (isVmConnector()) { // Check for SSL config on reconnection only if (stub == null) { checkSslConfig(); } this.jmxc = new RMIConnector(stub, null); jmxc.connect(env); } else { this.jmxc = JMXConnectorFactory.connect(jmxUrl, env); } } this.mbsc = jmxc.getMBeanServerConnection(); this.server = Snapshot.newSnapshot(mbsc); } this.isDead = false; try { ObjectName on = new ObjectName(THREAD_MXBEAN_NAME); this.hasPlatformMXBeans = server.isRegistered(on); this.hasHotSpotDiagnosticMXBean = server.isRegistered(new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME)); // check if it has 6.0 new APIs if (this.hasPlatformMXBeans) { MBeanOperationInfo[] mopis = server.getMBeanInfo(on).getOperations(); // look for findDeadlockedThreads operations; for (MBeanOperationInfo op : mopis) { if (op.getName().equals("findDeadlockedThreads")) { this.supportsLockUsage = true; break; } } on = new ObjectName(COMPILATION_MXBEAN_NAME); this.hasCompilationMXBean = server.isRegistered(on); } } catch (MalformedObjectNameException e) { // should not reach here throw new InternalError(e.getMessage()); } catch (IntrospectionException e) { InternalError ie = new InternalError(e.getMessage()); ie.initCause(e); throw ie; } catch (InstanceNotFoundException e) { InternalError ie = new InternalError(e.getMessage()); ie.initCause(e); throw ie; } catch (ReflectionException e) { InternalError ie = new InternalError(e.getMessage()); ie.initCause(e); throw ie; } if (hasPlatformMXBeans) { // WORKAROUND for bug 5056632 // Check if the access role is correct by getting a RuntimeMXBean getRuntimeMXBean(); } }
private static boolean test(String proto, MBeanServer mbs, ObjectName on) throws Exception { System.out.println("Testing for protocol " + proto); JMXConnectorServer cs; JMXServiceURL url = new JMXServiceURL(proto, null, 0); try { cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); } catch (MalformedURLException e) { System.out.println("System does not recognize URL: " + url + "; ignoring"); return true; } cs.start(); JMXServiceURL addr = cs.getAddress(); JMXConnector client = JMXConnectorFactory.connect(addr); MBeanServerConnection mbsc = client.getMBeanServerConnection(); Object getAttributeExotic = mbsc.getAttribute(on, "Exotic"); AttributeList getAttrs = mbsc.getAttributes(on, new String[] {"Exotic"}); AttributeList setAttrs = new AttributeList(); setAttrs.add(new Attribute("Exotic", new Exotic())); setAttrs = mbsc.setAttributes(on, setAttrs); Object invokeExotic = mbsc.invoke(on, "anExotic", new Object[] {}, new String[] {}); MBeanInfo exoticMBI = mbsc.getMBeanInfo(on); mbsc.setAttribute(on, new Attribute("Exception", Boolean.TRUE)); Exception getAttributeException, setAttributeException, invokeException; try { try { mbsc.getAttribute(on, "Exotic"); throw noException("getAttribute"); } catch (Exception e) { getAttributeException = e; } try { mbsc.setAttribute(on, new Attribute("Exotic", new Exotic())); throw noException("setAttribute"); } catch (Exception e) { setAttributeException = e; } try { mbsc.invoke(on, "anExotic", new Object[] {}, new String[] {}); throw noException("invoke"); } catch (Exception e) { invokeException = e; } } finally { mbsc.setAttribute(on, new Attribute("Exception", Boolean.FALSE)); } client.close(); cs.stop(); boolean ok = true; ok &= checkAttrs("getAttributes", getAttrs); ok &= checkAttrs("setAttributes", setAttrs); ok &= checkType("getAttribute", getAttributeExotic, Exotic.class); ok &= checkType("getAttributes", attrValue(getAttrs), Exotic.class); ok &= checkType("setAttributes", attrValue(setAttrs), Exotic.class); ok &= checkType("invoke", invokeExotic, Exotic.class); ok &= checkType("getMBeanInfo", exoticMBI, ExoticMBeanInfo.class); ok &= checkExceptionType("getAttribute", getAttributeException, ExoticException.class); ok &= checkExceptionType("setAttribute", setAttributeException, ExoticException.class); ok &= checkExceptionType("invoke", invokeException, ExoticException.class); if (ok) System.out.println("Test passes for protocol " + proto); return ok; }