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 +}