From 3aaffbeab28e5f6eaae4cfcddfffe7dc750b4474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 12 Oct 2021 21:39:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=A3=81=E7=9B=98=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- system/define.go | 22 ++++++++++++++++++++++ system/disk.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 system/disk.go diff --git a/system/define.go b/system/define.go index 0a773fc..4aee89d 100644 --- a/system/define.go +++ b/system/define.go @@ -9,6 +9,7 @@ package system import ( "github.com/shirou/gopsutil/cpu" + "github.com/shirou/gopsutil/disk" "github.com/shirou/gopsutil/load" ) @@ -34,3 +35,24 @@ type CPUCoreCnt struct { Logical int `json:"logical"` // 逻辑核数 Physical int `json:"physical"` // 物理核数 } + +// DiskDetail 磁盘信息 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 8:12 下午 2021/10/12 +type DiskDetail struct { + PartitionStat disk.PartitionStat `json:"partition_stat"` + DiskStat *disk.UsageStat `json:"disk_stat"` + MemoryUnit string `json:"memory_unit"` // 空间的单位 KB / MB / GB +} + +// OutputDiskInfo 磁盘信息 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 8:40 下午 2021/10/12 +type OutputDiskInfo struct { + DiskList []*DiskDetail `json:"disk_list"` + IOStat map[string]disk.IOCountersStat `json:"io_stat"` +} diff --git a/system/disk.go b/system/disk.go new file mode 100644 index 0000000..a7cf20d --- /dev/null +++ b/system/disk.go @@ -0,0 +1,48 @@ +// Package system... +// +// Description : system... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2021-10-12 8:06 下午 +package system + +import ( + "github.com/shirou/gopsutil/disk" +) + +// GetDiskInfo 获取磁盘信息 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 8:06 下午 2021/10/12 +func GetDiskInfo() (*OutputDiskInfo, error) { + var ( + partitionList []disk.PartitionStat + diskUseStatList []*disk.UsageStat + err error + diskList []*DiskDetail + ) + + partitionList = make([]disk.PartitionStat, 0) + diskUseStatList = make([]*disk.UsageStat, 0) + diskList = make([]*DiskDetail, 0) + + if partitionList, err = disk.Partitions(true); nil != err { + return nil, err + } + + for _, part := range partitionList { + diskInfo, _ := disk.Usage(part.Mountpoint) + diskList = append(diskList, &DiskDetail{ + PartitionStat: part, + DiskStat: diskInfo, + }) + diskUseStatList = append(diskUseStatList, diskInfo) + } + ioStat, _ := disk.IOCounters() + return &OutputDiskInfo{ + DiskList: diskList, + IOStat: ioStat, + }, nil +}