mirror of
https://github.com/fankes/komari-agent.git
synced 2026-02-04 12:17:30 +08:00
fix: disk.Partitions改为false防止未知分区 / 添加zfs去重避免重复计算磁盘空间
This commit is contained in:
@@ -14,7 +14,7 @@ type DiskInfo struct {
|
||||
|
||||
func Disk() DiskInfo {
|
||||
diskinfo := DiskInfo{}
|
||||
usage, err := disk.Partitions(true)
|
||||
usage, err := disk.Partitions(false)
|
||||
if err != nil {
|
||||
diskinfo.Total = 0
|
||||
diskinfo.Used = 0
|
||||
@@ -36,19 +36,41 @@ func Disk() DiskInfo {
|
||||
}
|
||||
} else {
|
||||
// 使用默认逻辑,排除临时文件系统和网络驱动器
|
||||
deviceMap := make(map[string]*disk.UsageStat)
|
||||
|
||||
for _, part := range usage {
|
||||
if isPhysicalDisk(part) {
|
||||
u, err := disk.Usage(part.Mountpoint)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
deviceID := part.Device
|
||||
// ZFS去重: 基于 pool 名称 (例如 pool/dataset -> pool)
|
||||
if strings.ToLower(part.Fstype) == "zfs" {
|
||||
if idx := strings.Index(deviceID, "/"); idx != -1 {
|
||||
deviceID = deviceID[:idx]
|
||||
}
|
||||
}
|
||||
|
||||
// 如果该设备已存在,且当前挂载点的 Total 更大,则替换(处理 quota 等情况)
|
||||
// 否则保留现有的(通常我们希望统计物理 pool 的总量)
|
||||
if existing, ok := deviceMap[deviceID]; ok {
|
||||
if u.Total > existing.Total {
|
||||
deviceMap[deviceID] = u
|
||||
}
|
||||
} else {
|
||||
deviceMap[deviceID] = u
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, u := range deviceMap {
|
||||
diskinfo.Total += u.Total
|
||||
diskinfo.Used += u.Used
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return diskinfo
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user