add OshiUtil

This commit is contained in:
Looly
2019-08-28 18:50:50 +08:00
parent f4a5b8b9b7
commit f8d3e3fb14
6 changed files with 145 additions and 5 deletions

View File

@@ -0,0 +1,99 @@
package cn.hutool.system.oshi;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.ComputerSystem;
import oshi.hardware.GlobalMemory;
import oshi.hardware.HWDiskStore;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.Sensors;
import oshi.software.os.OperatingSystem;
/**
* Oshi库封装的工具类通过此工具类可获取系统、硬件相关信息
*
* <pre>
* 1、系统信息
* 2、硬件信息
* </pre>
*
* @author Looly
* @since 4.6.4
*/
public class OshiUtil {
private static final SystemInfo systemInfo;
/** 硬件信息 */
private static final HardwareAbstractionLayer hardware;
/** 系统信息 */
private static final OperatingSystem os;
static {
systemInfo = new SystemInfo();
hardware = systemInfo.getHardware();
os = systemInfo.getOperatingSystem();
}
/**
* 获取操作系统相关信息,包括系统版本、文件系统、进程等
*
* @return 操作系统相关信息
*/
public static OperatingSystem getOs() {
return os;
}
/**
* 获取硬件相关信息包括内存、硬盘、网络设备、显示器、USB、声卡等
*
* @return 硬件相关信息
*/
public static HardwareAbstractionLayer getHardware() {
return hardware;
}
/**
* 获取BIOS中计算机相关信息比如序列号、固件版本等
*
* @return 获取BIOS中计算机相关信息
*/
public static ComputerSystem getSystem() {
return hardware.getComputerSystem();
}
/**
* 获取内存相关信息,比如总内存、可用内存等
*
* @return 内存相关信息
*/
public static GlobalMemory getMemory() {
return hardware.getMemory();
}
/**
* 获取CPU处理器相关信息比如CPU负载等
*
* @return CPU处理器相关信息
*/
public static CentralProcessor getProcessor() {
return hardware.getProcessor();
}
/**
* 获取传感器相关信息例如CPU温度、风扇转速等传感器可能有多个
*
* @return 传感器相关信息
*/
public static Sensors getSensors() {
return hardware.getSensors();
}
/**
* 获取磁盘相关信息,可能有多个磁盘(包括可移动磁盘等)
*
* @return 磁盘相关信息
*/
public static HWDiskStore[] getDiskStores() {
return hardware.getDiskStores();
}
}

View File

@@ -0,0 +1,8 @@
/**
* Oshi库封装<br>
* https://github.com/oshi/oshi
*
* @author Looly
* @since 4.6.4
*/
package cn.hutool.system.oshi;

View File

@@ -0,0 +1,15 @@
package cn.hutool.system;
import org.junit.Assert;
import org.junit.Test;
import cn.hutool.system.oshi.OshiUtil;
public class OshiTest {
@Test
public void getMemeryTest() {
long total = OshiUtil.getMemory().getTotal();
Assert.assertTrue(total > 0);
}
}