示例代码:
// 通过psutil获取所有分区的信息
// 重要字段:
/*
fmt.Printf("Device: %s\n", partition.Device) fmt.Printf("Mountpoint: %s\n", partition.Mountpoint) fmt.Printf("File system type: %s\n", partition.Fstype) fmt.Printf("Total: %d bytes\n", usage.Total) fmt.Printf("Used: %d bytes\n", usage.Used) fmt.Printf("Free: %d bytes\n", usage.Free) fmt.Printf("Percentage: %.2f%%\n\n", usage.UsedPercent)*/
func getPartitionUsageByPsutil() map[string]*disk.UsageStat {
var partUsage = make(map[string]*disk.UsageStat)
partitions, err := disk.Partitions(false)
if err != nil {
logger.Log.Errorf("%v报错:%v\n", "disk.Partitions(false)", err)
return partUsage
}
// 遍历磁盘分区信息
for _, partition := range partitions {
// 如果分区为物理磁盘(不是虚拟分区),则获取详细信息
if partition.Device == partition.Mountpoint {
usage, err := disk.Usage(partition.Mountpoint)
if err != nil {
fmt.Printf("Error getting disk usage for %s: %s\n", partition.Mountpoint, err)
continue
}
partUsage[partition.Mountpoint] = usage
}
}
return partUsage
}
注意事项
如果包含被加密的磁盘, 那么是无法拿到加密磁盘信息的; 并且如果它没有作为文件系统挂载, 那么其他的文件系统例如U盘之类的, 因为拿取列表的时候失败, 对应的也会失败;
标签:err,usage,fmt,partition,psutil,Printf,go,磁盘,disk From: https://www.cnblogs.com/runninginsilence/p/18034288