SpringBoot项目,使用OSHI
获取系统状态信息时,后端打印正常,返回至前端的过程中遇到了以下两个报错
- java.lang.IllegalStateException: Unmapped relationship: 7
- COM exception querying MSAcpi_ThermalZoneTemperature, which might not be on
解决方式如下:
1. java.lang.IllegalStateException: Unmapped relationship: 7
解决方式:添加依赖net.java.dev.jna
<!-- OSHI -->
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>5.2.5</version>
</dependency>
<!-- jna -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.10.0</version>
</dependency>
2. COM exception querying MSAcpi_ThermalZoneTemperature, which might not be on
解决方式:将获取到的系统状态信息转为String或者整型后再添加到VO中返回
(1)业务逻辑代码
import com.geo.integrated.model.vo.SystemStatus;
import com.geo.integrated.service.VisualStatusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import oshi.SystemInfo;
import oshi.hardware.*;
import oshi.software.os.OperatingSystem;
@Service
@Slf4j
public class VisualStatusServiceImpl implements VisualStatusService {
/**
* 获取系统状态信息
*
* @return 系统状态信息
*/
@Override
public SystemStatus getSystemState() {
// 系统信息
SystemInfo systemInfo = new SystemInfo();
// 操作系统信息
OperatingSystem operationSystemInfo = systemInfo.getOperatingSystem();
// 硬件信息
HardwareAbstractionLayer hardwareInfo = systemInfo.getHardware();
/*有了代表硬件信息的对象HardwareAbstractionLayer之后,就可以获取硬件相关的信息了*/
// 内存相关信息
GlobalMemory memoryInfo = hardwareInfo.getMemory();
// 内存总容量
String totalMemory = String.valueOf(memoryInfo.getTotal());
// 可用内存的容量
String availableMemory = String.valueOf(memoryInfo.getAvailable());
/*有了内存总容量和内存可用容量,就可以计算出当前内存的利用率了*/
// CPU相关信息
CentralProcessor processor = hardwareInfo.getProcessor();
// CPU型号
String processorName = processor.getProcessorIdentifier().getName();
// 物理CPU数
int physicalPackageCount = processor.getPhysicalPackageCount();
//物理核心数
int physicalProcessorCount = processor.getPhysicalProcessorCount();
SystemStatus systemStatus = new SystemStatus(String.valueOf(systemInfo),
String.valueOf(operationSystemInfo),
String.valueOf(hardwareInfo),
String.valueOf(memoryInfo),
Long.parseLong(totalMemory) / 1000 / 1000 / 1000,
Long.parseLong(availableMemory) / 1000 / 1000 / 1000,
String.valueOf(processor), String.valueOf(processorName),
Integer.parseInt(String.valueOf(physicalPackageCount)),
Integer.parseInt(String.valueOf(physicalProcessorCount)));
return systemStatus;
}
}
(2)VO代码
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SystemStatus {
/**
* 系统信息
*/
private String systemInfo;
/**
* 操作系统信息
*/
private String operationSystemInfo;
/**
* 硬件信息
*/
private String hardwareInfo;
/**
* 内存相关信息
*/
private String memoryInfo;
/**
* 内存总容量
*/
private Long totalMemory;
/**
* 可用内存的容量
*/
private Long availableMemory;
/**
* CPU相关信息
*/
private String processor;
/**
* CPU型号
*/
private String processorName;
/**
* 物理CPU数
*/
private Integer physicalPackageCount;
/**
* 物理核心数
*/
private Integer physicalProcessorCount;
}
本文作者:
whtli
本文链接: https://hexo.whtli.cn/archives/ce540f07.html
版权声明: 遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
本文链接: https://hexo.whtli.cn/archives/ce540f07.html
版权声明: 遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。