fix: 使用正则表达式提取磁盘温度值,并在输出中添加摄氏度符号

This commit is contained in:
lixiangwuxian 2025-04-12 16:12:19 +08:00
parent 98790877ea
commit ed49a27fd4

View File

@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
@ -63,12 +64,14 @@ func checkRaidStatus() {
// 检查温度
tempStr := disk.DriveTemperature
if strings.Contains(tempStr, "C") {
// 提取温度数值
tempParts := strings.Split(tempStr, "C")
tempValue, err := strconv.Atoi(strings.TrimSpace(tempParts[0]))
if err == nil && tempValue > temperatureThreshold {
alertMessages = append(alertMessages,
fmt.Sprintf("槽位 %d 的磁盘温度过高: %s", disk.SlotNumber, tempStr))
// 使用更精确的正则表达式
tempRegex := regexp.MustCompile(`(\d+)C`)
if matches := tempRegex.FindStringSubmatch(tempStr); len(matches) > 1 {
tempValue, err := strconv.Atoi(matches[1])
if err == nil && tempValue > temperatureThreshold {
alertMessages = append(alertMessages,
fmt.Sprintf("槽位 %d 的磁盘温度过高: %s", disk.SlotNumber, tempStr))
}
}
}
@ -150,7 +153,7 @@ func RaidHandler(msg model.Message) (reply *model.Reply) {
}
userId := strconv.FormatInt(int64(selfInfo.Data.UserID), 10)
nickname := selfInfo.Data.Nickname
nodes = append(nodes, *message.NewNodeMessage().ParseMessage(userId, nickname, []any{message.NewTextMessage().ParseMessage("阵列信息:\n")}))
nodes = append(nodes, *message.NewNodeMessage().ParseMessage(userId, nickname, []any{message.NewTextMessage().ParseMessage("阵列信息:")}))
for _, diskInfo := range diskInfoList {
textMsg := message.NewTextMessage().ParseMessage(diskInfo.String())
nodes = append(nodes, *message.NewNodeMessage().ParseMessage(userId, nickname, []any{textMsg}))
@ -176,7 +179,7 @@ type DiskInfo struct {
func (d *DiskInfo) String() string {
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("槽位: %d\n", d.SlotNumber+1))
sb.WriteString(fmt.Sprintf("温度: %s\n", d.DriveTemperature))
sb.WriteString(fmt.Sprintf("温度: %s\n", d.DriveTemperature))
sb.WriteString(fmt.Sprintf("型号: %s\n", d.Type))
sb.WriteString(fmt.Sprintf("SN: %s\n", d.Sn))
sb.WriteString(fmt.Sprintf("状态: %s\n", d.FirmwareState))
@ -220,8 +223,14 @@ func ParseDiskInfoList(lines []string) []*DiskInfo {
}
} else if strings.HasPrefix(line, "Drive Temperature") {
tempParts := strings.Split(line, ":")
mode := regexp.MustCompile(`(\d+) C`)
if len(tempParts) >= 2 {
currentDisk.DriveTemperature = strings.TrimSpace(tempParts[1])
matches := mode.FindStringSubmatch(tempParts[1])
if len(matches) >= 1 {
currentDisk.DriveTemperature = matches[0]
} else {
currentDisk.DriveTemperature = strings.TrimSpace(tempParts[1])
}
}
} else if strings.HasPrefix(line, "Drive has flagged a S.M.A.R.T alert") {
smartParts := strings.Split(line, ":")