From 85fc07c900ce7b0cdefb4c089a9a79be95094e35 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Fri, 31 Mar 2023 11:20:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=A6=82=E8=A7=88=E9=A1=B5=E7=A3=81?= =?UTF-8?q?=E7=9B=98=E6=98=BE=E7=A4=BA=E8=BF=87=E6=BB=A4=E8=A7=84=E5=88=99?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=20(#458)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/dashboard.go | 43 ++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/backend/app/service/dashboard.go b/backend/app/service/dashboard.go index e2bc21d58..49a67ec7e 100644 --- a/backend/app/service/dashboard.go +++ b/backend/app/service/dashboard.go @@ -2,9 +2,11 @@ package service import ( "encoding/json" + "strings" "time" "github.com/1Panel-dev/1Panel/backend/app/dto" + "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/shirou/gopsutil/v3/cpu" "github.com/shirou/gopsutil/v3/disk" "github.com/shirou/gopsutil/v3/host" @@ -141,29 +143,54 @@ func (u *DashboardService) LoadCurrentInfo(ioOption string, netOption string) *d return ¤tInfo } +type diskInfo struct { + Type string + Mount string + Device string +} + func loadDiskInfo() []dto.DiskInfo { var datas []dto.DiskInfo - parts, err := disk.Partitions(false) + stdout, err := cmd.Exec("df -hT -P|grep '/'|grep -v tmpfs|grep -v 'snap/core'|grep -v udev") if err != nil { return datas } + lines := strings.Split(stdout, "\n") + + var mounts []diskInfo var excludes = []string{"/mnt/cdrom", "/boot", "/boot/efi", "/dev", "/dev/shm", "/run/lock", "/run", "/run/shm", "/run/user"} - for i := 0; i < len(parts); i++ { + for _, line := range lines { + fields := strings.Fields(line) + if len(fields) < 7 { + continue + } + if fields[1] == "tmpfs" { + continue + } + if strings.Contains(fields[2], "M") || strings.Contains(fields[2], "K") { + continue + } + if strings.Contains(fields[6], "docker") { + continue + } isExclude := false for _, exclude := range excludes { - if parts[i].Mountpoint == exclude { + if exclude == fields[6] { isExclude = true - break } } if isExclude { continue } - state, _ := disk.Usage(parts[i].Mountpoint) + mounts = append(mounts, diskInfo{Type: fields[1], Device: fields[0], Mount: fields[6]}) + } + + for i := 0; i < len(mounts); i++ { + state, _ := disk.Usage(mounts[i].Mount) var itemData dto.DiskInfo - itemData.Path = parts[i].Mountpoint - itemData.Type = parts[i].Fstype - itemData.Device = parts[i].Device + itemData.Path = mounts[i].Mount + itemData.Type = mounts[i].Type + itemData.Device = mounts[i].Device itemData.Total = state.Total itemData.Free = state.Free itemData.Used = state.Used