v0.0.10 修复BSD系统识别硬盘大小失败的问题,添加默认的KSM和气球驱动默认值

This commit is contained in:
spiritlhl
2025-03-19 08:47:05 +00:00
parent 880152ff7f
commit 7f0eddf8a7
5 changed files with 69 additions and 14 deletions

View File

@@ -24,7 +24,7 @@ jobs:
run: | run: |
git config --global user.name 'github-actions' git config --global user.name 'github-actions'
git config --global user.email 'github-actions@github.com' git config --global user.email 'github-actions@github.com'
TAG="v0.0.9-$(date +'%Y%m%d%H%M%S')" TAG="v0.0.10-$(date +'%Y%m%d%H%M%S')"
git tag $TAG git tag $TAG
git push origin $TAG git push origin $TAG
env: env:

View File

@@ -1,6 +1,6 @@
package model package model
const BasicsVersion = "v0.0.9" const BasicsVersion = "v0.0.10"
var EnableLoger bool var EnableLoger bool

View File

@@ -12,9 +12,37 @@ import (
// getDiskInfo 获取硬盘信息 // getDiskInfo 获取硬盘信息
func getDiskInfo() (string, string, string, string, error) { func getDiskInfo() (string, string, string, string, error) {
var diskTotalStr, diskUsageStr, percentageStr, bootPath string var diskTotalStr, diskUsageStr, percentageStr, bootPath string
// BSD系统特殊处理
if runtime.GOOS == "freebsd" || runtime.GOOS == "openbsd" || runtime.GOOS == "netbsd" {
cmd := exec.Command("df", "-h", "/")
output, err := cmd.Output()
if err == nil {
lines := strings.Split(string(output), "\n")
if len(lines) >= 2 {
fields := strings.Fields(lines[1])
if len(fields) >= 5 {
bootPath = fields[0]
diskTotalStr = fields[1]
diskUsageStr = fields[2]
percentageStr = fields[4]
// 两个%避免被转义
if percentageStr != "" && strings.Contains(percentageStr, "%") {
percentageStr = strings.ReplaceAll(percentageStr, "%", "%%")
}
return diskTotalStr, diskUsageStr, percentageStr, bootPath, nil
}
}
}
}
// 保持原有代码逻辑
tempDiskTotal, tempDiskUsage := getDiskTotalAndUsed() tempDiskTotal, tempDiskUsage := getDiskTotalAndUsed()
diskTotalGB := float64(tempDiskTotal) / (1024 * 1024 * 1024) diskTotalGB := float64(tempDiskTotal) / (1024 * 1024 * 1024)
diskUsageGB := float64(tempDiskUsage) / (1024 * 1024 * 1024) diskUsageGB := float64(tempDiskUsage) / (1024 * 1024 * 1024)
// 字节为单位 进行单位转换 // 字节为单位 进行单位转换
if diskTotalGB < 1 { if diskTotalGB < 1 {
diskTotalStr = strconv.FormatFloat(diskTotalGB*1024, 'f', 2, 64) + " MB" diskTotalStr = strconv.FormatFloat(diskTotalGB*1024, 'f', 2, 64) + " MB"
@@ -26,6 +54,7 @@ func getDiskInfo() (string, string, string, string, error) {
} else { } else {
diskUsageStr = strconv.FormatFloat(diskUsageGB, 'f', 2, 64) + " GB" diskUsageStr = strconv.FormatFloat(diskUsageGB, 'f', 2, 64) + " GB"
} }
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
parts, err := disk.Partitions(true) parts, err := disk.Partitions(true)
if err != nil { if err != nil {
@@ -86,6 +115,7 @@ func getDiskInfo() (string, string, string, string, error) {
} }
} }
} }
// 两个%避免被转义 // 两个%避免被转义
if percentageStr != "" && strings.Contains(percentageStr, "%") { if percentageStr != "" && strings.Contains(percentageStr, "%") {
percentageStr = strings.ReplaceAll(percentageStr, "%", "%%") percentageStr = strings.ReplaceAll(percentageStr, "%", "%%")
@@ -111,9 +141,18 @@ func getDiskTotalAndUsed() (total uint64, used uint64) {
used += diskUsageOf.Used used += diskUsageOf.Used
} }
} }
// Fallback 到这个方法,仅统计根路径,适用于OpenVZ之类的.
if runtime.GOOS == "linux" && total == 0 && used == 0 { // Fallback 到根路径的获取方法
cmd := exec.Command("df") if total == 0 && used == 0 {
var cmd *exec.Cmd
// BSD系统使用特定参数
if runtime.GOOS == "freebsd" || runtime.GOOS == "openbsd" || runtime.GOOS == "netbsd" {
cmd = exec.Command("df", "-k", "/")
} else {
cmd = exec.Command("df")
}
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err == nil { if err == nil {
s := strings.Split(string(out), "\n") s := strings.Split(string(out), "\n")
@@ -126,6 +165,16 @@ func getDiskTotalAndUsed() (total uint64, used uint64) {
// 默认获取的是1K块为单位的. // 默认获取的是1K块为单位的.
total = total * 1024 total = total * 1024
used = used * 1024 used = used * 1024
break
}
} else if len(info) == 5 && (runtime.GOOS == "freebsd" || runtime.GOOS == "openbsd" || runtime.GOOS == "netbsd") {
// BSD系统df输出格式可能只有5列
if info[4] == "/" {
total, _ = strconv.ParseUint(info[1], 0, 64)
used, _ = strconv.ParseUint(info[2], 0, 64)
total = total * 1024
used = used * 1024
break
} }
} }
} }

View File

@@ -85,11 +85,14 @@ func getMemoryInfo() (string, string, string, string, string, string) {
} else { } else {
virtioBalloonStatus = "✔️ Enabled" virtioBalloonStatus = "✔️ Enabled"
} }
} else {
virtioBalloonStatus = ""
} }
} else { }
virtioBalloonStatus = "" if virtioBalloonStatus == "" {
if runtime.GOOS == "windows" {
virtioBalloonStatus = "[N] Undetected"
} else {
virtioBalloonStatus = "❌ Undetected"
}
} }
ksmStatus, err := os.ReadFile("/sys/kernel/mm/ksm/run") ksmStatus, err := os.ReadFile("/sys/kernel/mm/ksm/run")
if err == nil { if err == nil {
@@ -99,11 +102,14 @@ func getMemoryInfo() (string, string, string, string, string, string) {
} else { } else {
KernelSamepageMerging = "✔️ Enabled" KernelSamepageMerging = "✔️ Enabled"
} }
} else {
KernelSamepageMerging = ""
} }
} else { }
KernelSamepageMerging = "" if KernelSamepageMerging == "" {
if runtime.GOOS == "windows" {
KernelSamepageMerging = "[N] Undetected"
} else {
KernelSamepageMerging = "❌ Undetected"
}
} }
return memoryTotalStr, memoryUsageStr, swapTotalStr, swapUsageStr, virtioBalloonStatus, KernelSamepageMerging return memoryTotalStr, memoryUsageStr, swapTotalStr, swapUsageStr, virtioBalloonStatus, KernelSamepageMerging
} }

View File

@@ -136,7 +136,7 @@ func CheckSystemInfo(language string) string {
res += " 气球驱动 : " + ret.VirtioBalloon + "\n" res += " 气球驱动 : " + ret.VirtioBalloon + "\n"
} }
if ret.KSM != "" { if ret.KSM != "" {
res += " 内存同页合并 : " + ret.KSM + "\n" res += " 内页合并 : " + ret.KSM + "\n"
} }
if ret.SwapTotal == "" && ret.SwapUsage == "" { if ret.SwapTotal == "" && ret.SwapUsage == "" {
res += " 虚拟内存 Swap : [ no swap partition or swap file detected ]" + "\n" res += " 虚拟内存 Swap : [ no swap partition or swap file detected ]" + "\n"