写一个通过jmx获取服务器CPU,内存,磁盘,网络 的java工具类
时间: 2024-03-08 20:47:51 浏览: 87
好的,以下是一个通过JMX获取服务器CPU,内存,磁盘,网络信息的Java工具类的示例代码:
```java
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.util.ArrayList;
import java.util.List;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
public class ServerMonitoringUtil {
private static final String OS_MXBEAN_NAME = "java.lang:type=OperatingSystem";
private static final String MEMORY_MXBEAN_NAME = "java.lang:type=Memory";
private static final String CLASS_LOADING_MXBEAN_NAME = "java.lang:type=ClassLoading";
private static final String RUNTIME_MXBEAN_NAME = "java.lang:type=Runtime";
private static final String THREAD_MXBEAN_NAME = "java.lang:type=Threading";
private static final String GARABAGE_COLLECTOR_MXBEAN_NAME = "java.lang:type=GarbageCollector,*";
private static final String NETWORK_INTERFACE_MXBEAN_NAME = "java.lang:type=OperatingSystem";
private MBeanServerConnection mbsc;
public ServerMonitoringUtil(MBeanServerConnection mbsc) {
this.mbsc = mbsc;
}
public double getCpuUsage() throws AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {
OperatingSystemMXBean osMxBean = ManagementFactory.newPlatformMXBeanProxy(mbsc, OS_MXBEAN_NAME, OperatingSystemMXBean.class);
return osMxBean.getSystemCpuLoad();
}
public long getMemoryUsed() throws AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {
ObjectName memoryMxBeanName = new ObjectName(MEMORY_MXBEAN_NAME);
long used = (Long) mbsc.getAttribute(memoryMxBeanName, "HeapMemoryUsage.used");
used += (Long) mbsc.getAttribute(memoryMxBeanName, "NonHeapMemoryUsage.used");
return used;
}
public long getMemoryMax() throws AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {
ObjectName memoryMxBeanName = new ObjectName(MEMORY_MXBEAN_NAME);
long max = (Long) mbsc.getAttribute(memoryMxBeanName, "HeapMemoryUsage.max");
max += (Long) mbsc.getAttribute(memoryMxBeanName, "NonHeapMemoryUsage.max");
return max;
}
public double getClassLoadingRate() throws AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {
ObjectName classLoadingMxBeanName = new ObjectName(CLASS_LOADING_MXBEAN_NAME);
long loadedCount = (Long) mbsc.getAttribute(classLoadingMxBeanName, "LoadedClassCount");
long totalLoadedCount = (Long) mbsc.getAttribute(classLoadingMxBeanName, "TotalLoadedClassCount");
long unloadedCount = (Long) mbsc.getAttribute(classLoadingMxBeanName, "UnloadedClassCount");
return (double) loadedCount / totalLoadedCount;
}
public long getThreadCount() throws AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {
ObjectName threadMxBeanName = new ObjectName(THREAD_MXBEAN_NAME);
return (Long) mbsc.getAttribute(threadMxBeanName, "ThreadCount");
}
public long getGarbageCollectionTime() throws MalformedObjectNameException, AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {
long gcTime = 0;
List<ObjectName> gcMxBeanNames = new ArrayList<ObjectName>();
gcMxBeanNames.addAll(mbsc.queryNames(new ObjectName(GARABAGE_COLLECTOR_MXBEAN_NAME), null));
for (ObjectName gcMxBeanName : gcMxBeanNames) {
gcTime += (Long) mbsc.getAttribute(gcMxBeanName, "CollectionTime");
}
return gcTime;
}
public long getDiskUsed() {
// TODO: Implement disk usage monitoring
return 0;
}
public long getNetworkInboundBytes() {
// TODO: Implement network inbound bytes monitoring
return 0;
}
public long getNetworkOutboundBytes() {
// TODO: Implement network outbound bytes monitoring
return 0;
}
}
```
该工具类实现了获取CPU使用率、内存使用情况、类加载速率、线程数、垃圾回收时间等信息的方法,同时也提供了获取磁盘使用情况、网络流量等信息的桩函数,需要根据实际情况进行实现。在使用该工具类时,需要传入一个已经连接到目标服务器的MBeanServerConnection对象。
阅读全文