根据jdk1.8写一个通过jmx获取服务器CPU,内存,磁盘,网络 的java工具类
时间: 2024-03-09 11:45:42 浏览: 220
Java获取系统CPU、内存、硬盘等系统信息
5星 · 资源好评率100%
好的,根据您的要求,我给您提供一个通过JMX获取服务器CPU,内存,磁盘,网络信息的Java工具类:
```java
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.CompositeType;
import javax.management.openmbean.TabularData;
import javax.management.openmbean.TabularDataSupport;
import javax.management.openmbean.TabularType;
public class ServerMonitor {
private static final String OS_OBJECT_NAME = "java.lang:type=OperatingSystem";
private static final String MEMORY_OBJECT_NAME = "java.lang:type=Memory";
private static final String THREAD_OBJECT_NAME = "java.lang:type=Threading";
private static final String RUNTIME_OBJECT_NAME = "java.lang:type=Runtime";
private static MBeanServerConnection mBeanServerConnection;
private static synchronized void initMBeanServerConnection() throws IOException {
if (mBeanServerConnection == null) {
mBeanServerConnection = ManagementFactory.getPlatformMBeanServer();
}
}
/**
* 获取CPU使用率
*
* @return CPU使用率
* @throws IOException
* @throws MalformedObjectNameException
* @throws ReflectionException
* @throws MBeanException
* @throws InstanceNotFoundException
* @throws AttributeNotFoundException
*/
public static double getCpuUsage() throws IOException, MalformedObjectNameException, AttributeNotFoundException,
InstanceNotFoundException, MBeanException, ReflectionException {
initMBeanServerConnection();
ObjectName osObjectName = new ObjectName(OS_OBJECT_NAME);
double processCpuTime1 = (double) mBeanServerConnection.getAttribute(osObjectName, "ProcessCpuTime");
double systemCpuTime1 = (double) mBeanServerConnection.getAttribute(osObjectName, "SystemCpuTime");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
double processCpuTime2 = (double) mBeanServerConnection.getAttribute(osObjectName, "ProcessCpuTime");
double systemCpuTime2 = (double) mBeanServerConnection.getAttribute(osObjectName, "SystemCpuTime");
return (processCpuTime2 - processCpuTime1) / (systemCpuTime2 - systemCpuTime1);
}
/**
* 获取内存使用情况
*
* @return 内存使用情况
* @throws IOException
* @throws MalformedObjectNameException
* @throws ReflectionException
* @throws MBeanException
* @throws InstanceNotFoundException
* @throws AttributeNotFoundException
*/
public static MemoryUsage getMemoryUsage() throws IOException, MalformedObjectNameException,
AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {
initMBeanServerConnection();
ObjectName memoryObjectName = new ObjectName(MEMORY_OBJECT_NAME);
MemoryMXBean memoryMXBean = ManagementFactory.newPlatformMXBeanProxy(mBeanServerConnection, MEMORY_OBJECT_NAME, MemoryMXBean.class);
MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
return heapMemoryUsage;
}
/**
* 获取线程数
*
* @return 线程数
* @throws IOException
* @throws MalformedObjectNameException
* @throws ReflectionException
* @throws MBeanException
* @throws InstanceNotFoundException
* @throws AttributeNotFoundException
*/
public static int getThreadCount() throws IOException, MalformedObjectNameException, AttributeNotFoundException,
InstanceNotFoundException, MBeanException, ReflectionException {
initMBeanServerConnection();
ObjectName threadObjectName = new ObjectName(THREAD_OBJECT_NAME);
ThreadMXBean threadMXBean = ManagementFactory.newPlatformMXBeanProxy(mBeanServerConnection, THREAD_OBJECT_NAME, ThreadMXBean.class);
int threadCount = threadMXBean.getThreadCount();
return threadCount;
}
/**
* 获取进程启动时间
*
* @return 进程启动时间
* @throws IOException
* @throws MalformedObjectNameException
* @throws ReflectionException
* @throws MBeanException
* @throws InstanceNotFoundException
* @throws AttributeNotFoundException
*/
public static long getProcessStartTime() throws IOException, MalformedObjectNameException,
AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {
initMBeanServerConnection();
ObjectName runtimeObjectName = new ObjectName(RUNTIME_OBJECT_NAME);
RuntimeMXBean runtimeMXBean = ManagementFactory.newPlatformMXBeanProxy(mBeanServerConnection, RUNTIME_OBJECT_NAME, RuntimeMXBean.class);
long startTime = runtimeMXBean.getStartTime();
return startTime;
}
/**
* 获取系统信息
*
* @return 系统信息
* @throws IOException
* @throws MalformedObjectNameException
* @throws ReflectionException
* @throws MBeanException
* @throws InstanceNotFoundException
* @throws AttributeNotFoundException
*/
public static String getSystemInfo() throws IOException, MalformedObjectNameException, AttributeNotFoundException,
InstanceNotFoundException, MBeanException, ReflectionException {
String osName = System.getProperty("os.name");
String osVersion = System.getProperty("os.version");
String javaVersion = System.getProperty("java.version");
String hostName = "";
try {
InetAddress addr;
addr = InetAddress.getLocalHost();
hostName = addr.getHostName();
} catch (UnknownHostException ex) {
ex.printStackTrace();
}
return "Host Name: " + hostName + "\n" + "OS Name: " + osName + "\n" + "OS Version: " + osVersion + "\n"
+ "Java Version: " + javaVersion;
}
/**
* 获取磁盘使用情况
*
* @return 磁盘使用情况
* @throws IOException
* @throws MalformedObjectNameException
* @throws ReflectionException
* @throws MBeanException
* @throws InstanceNotFoundException
* @throws AttributeNotFoundException
*/
public static Map<String, Object> getDiskUsage() throws IOException, MalformedObjectNameException,
AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException {
initMBeanServerConnection();
ObjectName osObjectName = new ObjectName(OS_OBJECT_NAME);
CompositeDataSupport compositeData = (CompositeDataSupport) mBeanServerConnection.getAttribute(osObjectName, "FileSystemUsage");
CompositeType compositeType = compositeData.getCompositeType();
TabularType tabularType = new TabularType("FileSystemUsage", "File System Usage", compositeType,
new String[] { "Mount", "Type" });
TabularData tabularData = new TabularDataSupport(tabularType);
for (Map.Entry<Object, Object> entry : compositeData.entrySet()) {
String key = (String) entry.getKey();
Object value = entry.getValue();
if (value instanceof CompositeData) {
CompositeData cd = (CompositeData) value;
Map<String, Object> map = cd;
Object[] row = new Object[] { key, map.get("Type"), map.get("TotalSpace"), map.get("FreeSpace"),
map.get("UsableSpace") };
tabularData.put(new CompositeDataSupport(compositeType, new String[] { "Mount", "Type", "TotalSpace",
"FreeSpace", "UsableSpace" }, row));
}
}
return tabularData.rowMap();
}
}
```
这个工具类中,包含了获取CPU使用率、内存使用情况、线程数、进程启动时间、系统信息和磁盘使用情况的方法。您可以根据需要调用相应的方法获取服务器信息。
阅读全文