JMX remote server system monitoring II

Source: Internet
Author: User

Next, we provide another implementation method:

First look at a class diagram:

JDK implements nine built-in mbeans that can monitor all the information of virtual machines:

 


ManagementFactoryThe factory class can get the corresponding MBean instance through ObjectName, and naturally the desired data can be obtained.


2 The code is as follows:

Package pyc. jvm. monitor; import java. io. IOException; import java. lang. management. classLoadingMXBean; import java. lang. management. managementFactory; import java. lang. management. memoryMXBean; import java. lang. management. memoryUsage; import java. lang. management. operatingSystemMXBean; import java. lang. management. threadMXBean; import java.net. malformedURLException; import java. util. timer; import java. util. timerTa Sk; import java. util. concurrent. timeUnit; import javax. management. attributeNotFoundException; import javax. management. instanceNotFoundException; import javax. management. MBeanException; import javax. management. MBeanServerConnection; import javax. management. reflectionException; import javax. management. remote. JMXConnector; import javax. management. remote. JMXConnectorFactory; import javax. management. remote. JMXS ErviceURL;/*** creation Time: 11:06:34 ** @ author zhangtianyou * @ version 2.2 */public class JMXTest2 {private final static String SERVICE_1 = "service: jmx: rmi: /// jndi/rmi: // 192.168.85.54: 8787/jmxrmi ";/*** @ param args */public static void main (String [] args) {// call Timer timer once every 10 seconds = new Timer (); timer. schedule (new JMXTest2 (). new MonitorTask (SERVICE_1), 0, 10000);} private class MonitorTask exte Nds TimerTask {private String service; public MonitorTask (String service) {this. service = service ;}@ Overridepublic void run () {JMXmonitor (service) ;}} private static void JMXmonitor (String service) {jmxconnejmxconnector = null; try {JMXServiceURL ServiceURL = new JMXServiceURL (service); jmxConnector = JMXConnectorFactory. connect (ServiceURL); // MBean server (whether local or remote) communication method MBeanServerConnectionMBe AnServerConnection mBeanServerConnection = jmxConnector. getMBeanServerConnection (); // get MemoryMXBeanSystem. out. println ("\ nMemory"); MemoryMXBean memoryMXBean = ManagementFactory. newPlatformMXBeanProxy (mBeanServerConnection, ManagementFactory. MEMORY_MXBEAN_NAME, MemoryMXBean. class); MemoryUsage heapMemoryUsage = memoryMXBean. getHeapMemoryUsage (); System. out. println ("heapMemoryUsage:"); System. out. printl N ("committed =" + convertKB (heapMemoryUsage. getCommitted (); System. out. println ("init =" + convertKB (heapMemoryUsage. getInit (); System. out. println ("max =" + convertKB (heapMemoryUsage. getMax (); System. out. println ("used =" + convertKB (heapMemoryUsage. getUsed (); MemoryUsage nonHeapMemoryUsage = memoryMXBean. getNonHeapMemoryUsage (); System. out. println ("\ nnonHeapMemoryUsage:"); System. out. println ( "Committed =" + convertKB (nonHeapMemoryUsage. getCommitted (); System. out. println ("init =" + convertKB (nonHeapMemoryUsage. getInit (); System. out. println ("max =" + convertKB (nonHeapMemoryUsage. getMax (); System. out. println ("used =" + convertKB (nonHeapMemoryUsage. getUsed (); // Obtain ThreadMXBeanSystem. out. println ("\ nThread"); ThreadMXBean threadMXBean = ManagementFactory. newPlatformMXBeanProxy (mBeanServ ErConnection, ManagementFactory. THREAD_MXBEAN_NAME, ThreadMXBean. class); System. out. println ("ThreadCount =" + threadMXBean. getThreadCount (); System. out. println ("DaemonThreadCount =" + threadMXBean. getDaemonThreadCount (); System. out. println ("PeakThreadCount =" + threadMXBean. getPeakThreadCount (); System. out. println ("CurrentThreadCpuTime =" + threadMXBean. getCurrentThreadCpuTime (); System. out. println ( "CurrentThreadUserTime =" + threadMXBean. getCurrentThreadUserTime (); System. out. println ("\ nClassLoading"); ClassLoadingMXBean classLoadingMXBean = ManagementFactory. newPlatformMXBeanProxy (mBeanServerConnection, ManagementFactory. CLASS_LOADING_MXBEAN_NAME, ClassLoadingMXBean. class); // The number of classes currently loaded to the Java virtual machine System. out. println ("LoadedClassCount =" + classLoadingMXBean. getLoadedClassCount (); // The Java virtual machine starts to execute Total number of classes that have been loaded before. System. out. println ("TotalLoadedClassCount =" + classLoadingMXBean. getTotalLoadedClassCount (); // The total number of classes that have been uninstalled since the Java virtual machine started running. System. out. println ("UnloadedClassCount =" + classLoadingMXBean. getUnloadedClassCount (); System. out. println ("\ nCpu"); OperatingSystemMXBean operatingSystemMXBean = ManagementFactory. newPlatformMXBeanProxy (mBeanServerConnection, ManagementFactory. OPERATING_SYSTEM_MXBEAN_NAME, OperatingSystemMXBean. class); System. out. println ("AvailableProcessors =" + operatingSystemMXBean. getAvailableProcessors (); double Ratio = 0.0; long start = System. currentTimeMillis (); long startC; try {startC = (long) mBeanServerConnection. getAttribute (operatingSystemMXBean. getObjectName (), "ProcessCpuTime"); try {TimeUnit. SECONDS. sleep (5);} catch (InterruptedException e) {e. printStackTrace ();} long end = System. currentTimeMillis (); long endC = (long) mBeanServerConnection. getAttribute (operatingSystemMXBean. getObjectName (), "Proce SsCpuTime "); int availableProcessors = operatingSystemMXBean. getAvailableProcessors (); ratio = (endC-startC)/1000000.0/(end-start)/availableProcessors;} catch (AttributeNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace ();} catch (InstanceNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace ();} catch (MBeanException e1) {// TODO Auto-generated c Atch blocke1.printStackTrace ();} catch (ReflectionException e1) {// TODO Auto-generated catch blocke1.printStackTrace ();} System. out. println ("CUP usage" + ratio * 100 + "%");} catch (MalformedURLException e) {e. printStackTrace (); System. out. println ("invalid ServiceURL");} catch (IOException e) {e. printStackTrace ();} finally {try {if (jmxConnector! = Null) {jmxConnector. close () ;}} catch (IOException e) {e. printStackTrace () ;}} private static String convertKB (long src) {if (src <= 0L) {return "0KB" ;}return (double) src/1024 + "KB ";}}


3. The running effect is as follows:

MemoryheapMemoryUsage: committed = 59392.0 KBinit = 63667.75 KBmax = 906752.0 KBused = 21645.8046875 KBnonHeapMemoryUsage: committed = 30144.0 KBinit = 24000.0 KBmax = 133120.0 KBused = 17608.9921875 KBThreadThreadCount = 53 DaemonThreadCount = 52 PeakThreadCount = 56 CurrentThreadCpuTime = 0 CurrentThreadUserTime = 0 bytes = 2526 bytes = 2581 UnloadedClassCount = 55 bytes = 4CUP usage 0.07790701158609668%

This method conforms to the jconsole curve data, which is more common and avoids hard coding.

JMX remote server system monitoring II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.