From 62572784267b82a9d601ac26a689cdfbd9360522 Mon Sep 17 00:00:00 2001 From: Akizon77 Date: Sun, 25 May 2025 18:17:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E7=A3=81=E7=9B=98?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E6=8E=92=E9=99=A4=E4=B8=B4=E6=97=B6=E6=96=87=E4=BB=B6=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E5=92=8C=E7=BD=91=E7=BB=9C=E9=A9=B1=E5=8A=A8=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- monitoring/unit/disk.go | 48 +++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/monitoring/unit/disk.go b/monitoring/unit/disk.go index 3497d15..5222d9f 100644 --- a/monitoring/unit/disk.go +++ b/monitoring/unit/disk.go @@ -1,6 +1,8 @@ package monitoring import ( + "strings" + "github.com/shirou/gopsutil/disk" ) @@ -11,19 +13,17 @@ type DiskInfo struct { func Disk() DiskInfo { diskinfo := DiskInfo{} - usage, err := disk.Partitions(true) + usage, err := disk.Partitions(false) // 使用 false 只获取物理分区 if err != nil { diskinfo.Total = 0 diskinfo.Used = 0 } else { for _, part := range usage { - if part.Mountpoint != "/tmp" && part.Mountpoint != "/var/tmp" && part.Mountpoint != "/dev/shm" { - // Skip /tmp, /var/tmp, and /dev/shm - // 获取磁盘使用情况 + // 排除临时文件系统和网络驱动器 + if isPhysicalDisk(part) { u, err := disk.Usage(part.Mountpoint) if err != nil { - diskinfo.Total = 0 - diskinfo.Used = 0 + continue } else { diskinfo.Total += u.Total diskinfo.Used += u.Used @@ -33,3 +33,39 @@ func Disk() DiskInfo { } return diskinfo } + +// isPhysicalDisk 判断分区是否为物理磁盘 +func isPhysicalDisk(part disk.PartitionStat) bool { + mountpoint := strings.ToLower(part.Mountpoint) + // 临时文件系统 + if mountpoint == "/tmp" || mountpoint == "/var/tmp" || mountpoint == "/dev/shm" || + mountpoint == "/run" || mountpoint == "/run/lock" { + return false + } + + fstype := strings.ToLower(part.Fstype) + // 网络驱动器 + if strings.HasPrefix(fstype, "nfs") || strings.HasPrefix(fstype, "cifs") || + strings.HasPrefix(fstype, "smb") || fstype == "vboxsf" || fstype == "9p" || + strings.Contains(fstype, "fuse") { + return false + } + // Windows 网络驱动器通常是映射盘符,但不容易通过fstype判断 + // 可以通过opts判断,Windows网络驱动通常有相关选项 + optsStr := strings.ToLower(part.Opts) + if strings.Contains(optsStr, "remote") || strings.Contains(optsStr, "network") { + return false + } + + // Docker overlay + if fstype == "overlay" { + return false + } + + // 虚拟内存 + if strings.HasPrefix(part.Device, "/dev/loop") || fstype == "devtmpfs" || fstype == "tmpfs" { + return false + } + + return true +}