增加nvme类型健康分数计算
This commit is contained in:
@@ -51,7 +51,9 @@ public class DiskVO implements Serializable {
|
|||||||
private long usedSpace;
|
private long usedSpace;
|
||||||
/** 健康状态 0 不健康,1健康 */
|
/** 健康状态 0 不健康,1健康 */
|
||||||
@Schema(description = "健康状态")
|
@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")){
|
if(jsonObject.containsKey("tcpdumpTimes")){
|
||||||
String tcpdumpTimes = jsonObject.getString("tcpdumpTimes");
|
String tcpdumpTimes = jsonObject.getString("tcpdumpTimes");
|
||||||
TcpdumpEO tcpdumpEO = JSON.parseObject(tcpdumpTimes, TcpdumpEO.class);
|
TcpdumpEO tcpdumpEO = JSON.parseObject(tcpdumpTimes, TcpdumpEO.class);
|
||||||
if(tcpdumpEO.getDetectFlag() == 1){
|
if(tcpdumpEO.getDetectFlag() != null && tcpdumpEO.getDetectFlag() == 1){
|
||||||
if("1".equals(tcpdumpEO.getFrequency())){
|
if("1".equals(tcpdumpEO.getFrequency())){
|
||||||
handleTcpdump(tcpdumpEO);
|
handleTcpdump(tcpdumpEO);
|
||||||
}else if("2".equals(tcpdumpEO.getFrequency())){
|
}else if("2".equals(tcpdumpEO.getFrequency())){
|
||||||
|
|||||||
@@ -32,11 +32,13 @@ public class DiskServiceImpl implements DiskService {
|
|||||||
// 处理磁盘名称:去掉/dev/前缀
|
// 处理磁盘名称:去掉/dev/前缀
|
||||||
String diskFullName = disk.getName();
|
String diskFullName = disk.getName();
|
||||||
// 获取磁盘是否健康
|
// 获取磁盘是否健康
|
||||||
boolean isHealth = isDiskHealthy(diskFullName);
|
Long isHealth = isDiskHealthy(diskFullName);
|
||||||
if(isHealth){
|
if (diskFullName.startsWith("/dev/nvme")) {
|
||||||
diskVO.setHealthStatus(1);
|
int score = getNvmeHealthScore(diskFullName);
|
||||||
}else{
|
diskVO.setHealthScore(score);
|
||||||
diskVO.setHealthStatus(0);
|
}
|
||||||
|
if (!diskFullName.startsWith("/dev/dm")) {
|
||||||
|
diskVO.setHealthStatus(isHealth);
|
||||||
}
|
}
|
||||||
String diskName = diskFullName.replace("/dev/", "");
|
String diskName = diskFullName.replace("/dev/", "");
|
||||||
diskVO.setName(diskName); // 保持原样存储
|
diskVO.setName(diskName); // 保持原样存储
|
||||||
@@ -226,7 +228,7 @@ public class DiskServiceImpl implements DiskService {
|
|||||||
return partitionToDiskMap;
|
return partitionToDiskMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isDiskHealthy(String diskDevice) {
|
public Long isDiskHealthy(String diskDevice) {
|
||||||
try {
|
try {
|
||||||
Process process = new ProcessBuilder("smartctl", "-H", diskDevice)
|
Process process = new ProcessBuilder("smartctl", "-H", diskDevice)
|
||||||
.redirectErrorStream(true)
|
.redirectErrorStream(true)
|
||||||
@@ -235,13 +237,86 @@ public class DiskServiceImpl implements DiskService {
|
|||||||
try (BufferedReader reader = new BufferedReader(
|
try (BufferedReader reader = new BufferedReader(
|
||||||
new InputStreamReader(process.getInputStream()))) {
|
new InputStreamReader(process.getInputStream()))) {
|
||||||
|
|
||||||
return reader.lines()
|
boolean isHealth = reader.lines()
|
||||||
.anyMatch(line -> line.contains("PASSED") || line.contains("OK"))
|
.anyMatch(line -> line.contains("PASSED") || line.contains("OK"))
|
||||||
&& process.waitFor() == 0;
|
&& process.waitFor() == 0;
|
||||||
|
if(isHealth){
|
||||||
|
return 1L;
|
||||||
|
}else{
|
||||||
|
return 0L;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user