增加nvme类型健康分数计算

This commit is contained in:
gaoyutao
2026-02-06 17:11:30 +08:00
parent 3e7c9ec5cf
commit 01fd405e0b
3 changed files with 87 additions and 10 deletions
@@ -51,7 +51,9 @@ public class DiskVO implements Serializable {
private long usedSpace;
/** 健康状态 0 不健康,1健康 */
@Schema(description = "健康状态")
private long healthStatus;
private Long healthStatus;
@Schema(description = "健康分数")
private Integer healthScore;
}
@@ -941,7 +941,7 @@ public class AgentServiceImpl implements AgentService {
if(jsonObject.containsKey("tcpdumpTimes")){
String tcpdumpTimes = jsonObject.getString("tcpdumpTimes");
TcpdumpEO tcpdumpEO = JSON.parseObject(tcpdumpTimes, TcpdumpEO.class);
if(tcpdumpEO.getDetectFlag() == 1){
if(tcpdumpEO.getDetectFlag() != null && tcpdumpEO.getDetectFlag() == 1){
if("1".equals(tcpdumpEO.getFrequency())){
handleTcpdump(tcpdumpEO);
}else if("2".equals(tcpdumpEO.getFrequency())){
@@ -32,11 +32,13 @@ public class DiskServiceImpl implements DiskService {
// 处理磁盘名称:去掉/dev/前缀
String diskFullName = disk.getName();
// 获取磁盘是否健康
boolean isHealth = isDiskHealthy(diskFullName);
if(isHealth){
diskVO.setHealthStatus(1);
}else{
diskVO.setHealthStatus(0);
Long isHealth = isDiskHealthy(diskFullName);
if (diskFullName.startsWith("/dev/nvme")) {
int score = getNvmeHealthScore(diskFullName);
diskVO.setHealthScore(score);
}
if (!diskFullName.startsWith("/dev/dm")) {
diskVO.setHealthStatus(isHealth);
}
String diskName = diskFullName.replace("/dev/", "");
diskVO.setName(diskName); // 保持原样存储
@@ -226,7 +228,7 @@ public class DiskServiceImpl implements DiskService {
return partitionToDiskMap;
}
public boolean isDiskHealthy(String diskDevice) {
public Long isDiskHealthy(String diskDevice) {
try {
Process process = new ProcessBuilder("smartctl", "-H", diskDevice)
.redirectErrorStream(true)
@@ -235,13 +237,86 @@ public class DiskServiceImpl implements DiskService {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()))) {
return reader.lines()
boolean isHealth = reader.lines()
.anyMatch(line -> line.contains("PASSED") || line.contains("OK"))
&& process.waitFor() == 0;
if(isHealth){
return 1L;
}else{
return 0L;
}
}
} catch (Exception e) {
return false;
return null;
}
}
public int getNvmeHealthScore(String diskDevice) {
try {
// 检查是否为 NVMe 设备
if (!diskDevice.startsWith("/dev/nvme")) {
return -1; // 非 NVMe 设备
}
Process process = new ProcessBuilder("smartctl", "-a", diskDevice)
.redirectErrorStream(true)
.start();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()))) {
int exitCode = process.waitFor();
if (exitCode != 0) {
return -1; // 命令执行失败
}
int percentageUsed = -1;
int availableSpare = -1;
String line;
// 解析 smartctl 输出
while ((line = reader.readLine()) != null) {
// 查找 Percentage Used(已用百分比)
if (line.contains("Percentage Used:")) {
String[] parts = line.split(":");
if (parts.length > 1) {
String value = parts[1].trim().split("\\s+")[0];
try {
percentageUsed = Integer.parseInt(value.replace("%", ""));
} catch (NumberFormatException e) {
// 忽略解析错误
}
}
}
// 查找 Available Spare(可用备用空间)
else if (line.contains("Available Spare:")) {
String[] parts = line.split(":");
if (parts.length > 1) {
String value = parts[1].trim().split("\\s+")[0];
try {
availableSpare = Integer.parseInt(value.replace("%", ""));
} catch (NumberFormatException e) {
// 忽略解析错误
}
}
}
}
// 计算健康分数
if (percentageUsed >= 0) {
// 健康分数 = 100 - 已用百分比
return 100 - percentageUsed;
} else if (availableSpare >= 0) {
// 如果没有 Percentage Used,使用 Available Spare
return availableSpare;
} else {
return -1; // 无法获取健康信息
}
}
} catch (Exception e) {
return -1;
}
}