Java 实现查看cpu信息、服务器信息、操作系统信息、jvm 信息
程序员文章站
2022-05-15 19:09:01
...
Java想要实现跨平台查看服务器信息,可以使用 OSHI 开源 jar 包实现
引入依赖
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>3.12.2</version>
</dependency>
获取 cpu 信息
/**
* 获取 cpu 信息
*/
private static void getCpuInfo() {
System.err.println("===============cpu信息begin===============");
SystemInfo systemInfo = new SystemInfo();
CentralProcessor processor = systemInfo.getHardware().getProcessor();
long[] prevTicks = processor.getSystemCpuLoadTicks();
try {
// 睡眠 1s
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
long[] ticks = processor.getSystemCpuLoadTicks();
long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
System.out.println("cpu核数:" + processor.getLogicalProcessorCount());
System.out.println("cpu系统使用率:" + new DecimalFormat("#.##%").format(cSys * 1.0 / totalCpu));
System.out.println("cpu用户使用率:" + new DecimalFormat("#.##%").format(user * 1.0 / totalCpu));
System.out.println("cpu当前等待率:" + new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu));
System.out.println("cpu当前使用率:" + new DecimalFormat("#.##%").format(1.0 - (idle * 1.0 / totalCpu)));
System.err.println("===============cpu信息end===============\n");
}
获取服务器信息
/**
* 获取主机内存信息
*/
private static void getMemoryInfo() {
System.err.println("===============主机内存信息begin===============");
SystemInfo systemInfo = new SystemInfo();
GlobalMemory memory = systemInfo.getHardware().getMemory();
long totalMemory = memory.getTotal(); // 总内存
long acaliableMemory = memory.getAvailable(); // 剩余内存
System.out.println("总内存:" + formatByte(totalMemory));
System.out.println("已经使用的内存:" + formatByte(totalMemory - acaliableMemory));
System.out.println("剩余内存:" + formatByte(acaliableMemory));
System.out.println("使用率:" + new DecimalFormat("#.##%").format((totalMemory - acaliableMemory) * 1.0 / totalMemory));
System.err.println("===============主机内存信息end===============\n");
}
formatByte 换算单位工具类
/**
* 换算单位
*
* @param byteNumber 换算的数字
*/
private static String formatByte(long byteNumber) {
double format = 1024.0;
double kbNumber = byteNumber / format;
if (kbNumber < format)
return new DecimalFormat("#.##KB").format(kbNumber);
double mbNumber = kbNumber / format;
if (mbNumber < format)
return new DecimalFormat("#.##MB").format(mbNumber);
double gbNumber = mbNumber / format;
if (gbNumber < format)
return new DecimalFormat("#.##GB").format(gbNumber);
double tbNumber = gbNumber / format;
return new DecimalFormat("#.##TB").format(tbNumber);
}
获取操作系统信息
/**
* 获取操作系统信息
*/
private static void getSystemInfo() {
System.err.println("===============操作系统信息begin==============");
String osName = System.getProperty("os.name"); // 系统名称
String osArch = System.getProperty("os.arch"); // 架构名称
System.out.println("操作系统名称:" + osName);
System.out.println("系统架构:" + osArch);
System.err.println("===============操作系统信息end==============\n");
}
获取 JVM 信息
/**
* 获取 Jvm 信息
*/
private static void getJvmInfo() {
System.err.println("===============Jvm信息begin===============");
Runtime runtime = Runtime.getRuntime();
long jvmTotalMemoryByte = runtime.totalMemory(); // jvm总内存
long jvmMaxMemoryByte = runtime.maxMemory(); // jvm 最大内存
long freeMemoryByte = runtime.freeMemory(); // jvm 空闲空间
String jdkVersion = System.getProperty("java.version"); // jdk 版本
String jdkHome = System.getProperty("java.home"); // jdk 安装路径
System.out.println("jvm总内存:" + formatByte(jvmTotalMemoryByte));
System.out.println("jvm已使用内存:" + formatByte(jvmTotalMemoryByte - freeMemoryByte));
System.out.println("jvm最大内存:" + formatByte(jvmMaxMemoryByte));
System.out.println("jvm剩余内存:" + formatByte(freeMemoryByte));
System.out.println("jvm内存使用率:" + new DecimalFormat("#.##%").format((jvmTotalMemoryByte - freeMemoryByte) * 1.0 / jvmTotalMemoryByte));
System.out.println("jdk版本号:" + jdkVersion);
System.out.println("jdk安装路径:" + jdkHome);
System.err.println("===============Jvm信息end===============");
}
测试结果
上一篇: qml加载图片资源的四种方式
下一篇: vue组件的四种写法