shirou/gopsutil/disk
子包用于获取磁盘信息,包括IO统计、分区、使用率等。
分区信息 disk.Partitions
Linux中可通过df
命令查看磁盘分区信息
$ df -lh
Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs 3.8G 432K 3.8G 1% /run
tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
/dev/vda1 20G 5.9G 15G 30% /
tmpfs 763M 0 763M 0% /run/user/0
disk.Partitions()
用于返回分区信息,返回类型为[]PartitionStat
每个分区对应一个PartitionStat
结构。
func Partitions(all bool) ([]PartitionStat, error)
当入参all = true
时会返回所有分区,当入参all = false
时只会返回物理分区(包括磁盘、CD-ROM、USB),忽略其它的虚拟分区。
例如:获取本机所有分区信息
stat, err := disk.Partitions(true)
if err != nil {
panic(err)
}
for k, v := range stat {
fmt.Println(k, v)
}
0 {"device":"C:","mountpoint":"C:","fstype":"NTFS","opts":"rw.compress"}
1 {"device":"D:","mountpoint":"D:","fstype":"NTFS","opts":"rw.compress"}
2 {"device":"E:","mountpoint":"E:","fstype":"NTFS","opts":"rw.compress"}
3 {"device":"F:","mountpoint":"F:","fstype":"NTFS","opts":"rw.compress"}
分区信息保存在disk.PartitionStat
结构上
type PartitionStat struct {
Device string `json:"device"`
Mountpoint string `json:"mountpoint"`
Fstype string `json:"fstype"`
Opts string `json:"opts"`
}
字段 | 描述 |
---|---|
Device | 分区标识,Windows上表示为盘符。 |
Mountpoint | 挂载点,分区文件路径的起始位置。 |
Fstpe | 文件系统类型,Windows如FAT/NTFS等,Linux如EXT/EXT2/EXT3等。 |
Opts | 系统相关属性 |
使用信息 disk.Usage
Linux中可通过df
工具获取磁盘分区使用情况,shirou/gopsutil/disk
子包提供了disk.Usage()
来获取指定路径所在磁盘的使用情况,返回一个UsageStat
结构。
package disk
func Usage(path string) (*UsageStat, error)
type UsageStat struct {
Path string `json:"path"`
Fstype string `json:"fstype"`
Total uint64 `json:"total"`
Free uint64 `json:"free"`
Used uint64 `json:"used"`
UsedPercent float64 `json:"usedPercent"`
InodesTotal uint64 `json:"inodesTotal"`
InodesUsed uint64 `json:"inodesUsed"`
InodesFree uint64 `json:"inodesFree"`
InodesUsedPercent float64 `json:"inodesUsedPercent"`
}
disk.UsageStat
结构
字段 | 描述 |
---|---|
Path | 入参路径 |
Fstype | 分区的文件系统类型 |
Total | 分区总字节容量 |
Free | 分区空闲字节容量 |
Used | 分区已使用字节容量 |
UsedPercent | 使用百分比 |
例如:获取当前目录所在磁盘的使用情况
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
stat, err := disk.Usage(pwd)
if err != nil {
panic(err)
}
fmt.Println(stat)
{
"path":"F:\\Go\\project\\test",
"fstype":"",
"total":262142947328,
"free":132697026560,
"used":129445920768,
"usedPercent":49.37989829115407,
"inodesTotal":0,
"inodesUsed":0,
"inodesFree":0,
"inodesUsedPercent":0
}
字节单位转换
//字节单位转换
func fmtByte(size int64) string {
if size < 1024 {
return fmt.Sprintf("%.2fB", float64(size)/float64(1))
} else if size < 1024*1024 {
return fmt.Sprintf("%.2fKB", float64(size)/float64(1024))
} else if size < 1024*1024*1024 {
return fmt.Sprintf("%.2fMB", float64(size)/float64(1024*1024))
} else if size < 1024*1024*1024*1024 {
return fmt.Sprintf("%.2fGB", float64(size)/float64(1024*1024*1024))
} else if size < 1024*1024*1024*1024*1024 {
return fmt.Sprintf("%.2fTB", float64(size)/float64(1024*1024*1024*1024))
} else {
return fmt.Sprintf("%.2fEB", float64(size)/float64(1024*1024*1024*1024*2014))
}
}
I/O统计 disk.IOCounters
Linux中经常会采用iostat
工具查看磁盘I/O信息
$ iostat -d
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
vda 0.49 0.12 6.52 536914 28260578
shirou/gopsutil/disk
子包提供了disk.IOCounters()
方法用来获取磁盘I/O信息
stat, err := disk.IOCounters()
if err != nil {
fmt.Println(err)
return
}
for k, v := range stat {
fmt.Println(k, v)
}
E: {"readCount":39,"mergedReadCount":0,"writeCount":36,"mergedWriteCount":0,"readBytes":184320,"writeBytes":151552,"readTime":0,"writeTime":0,"iopsInProgress":0,"ioTime":0,"weightedIO":0,"name":"E:","serialNumber":"","label":""}
F: {"readCount":170344,"mergedReadCount":0,"writeCount":67792,"mergedWriteCount":0,"readBytes":4773946368,"writeBytes":888930304,"readTime":609,"writeTime":164,"iopsInProgress":0,"ioTime":0,"weightedIO":0,"name":"F:","serialNumber":"","label":""}
C: {"readCount":333033,"mergedReadCount":0,"writeCount":651920,"mergedWriteCount":0,"readBytes":11970601472,"writeBytes":21629909504,"readTime":2157,"writeTime":1129,"iopsInProgress":0,"ioTime":0,"weightedIO":0,"name":"C:","serialNumber":"","label":""}
D: c
disk.IOCounters()
获取磁盘I/O统计信息,返回map[string]IOCountersStat
类型,每个分区一个结构,其中键名为分区名,值为IOCountersStat
统计信息。
func IOCounters(names ...string) (map[string]IOCountersStat, error)
IOCountersStat
结构用来表示I/O统计信息
type IOCountersStat struct {
ReadCount uint64 `json:"readCount"`
MergedReadCount uint64 `json:"mergedReadCount"`
WriteCount uint64 `json:"writeCount"`
MergedWriteCount uint64 `json:"mergedWriteCount"`
ReadBytes uint64 `json:"readBytes"`
WriteBytes uint64 `json:"writeBytes"`
ReadTime uint64 `json:"readTime"`
WriteTime uint64 `json:"writeTime"`
IopsInProgress uint64 `json:"iopsInProgress"`
IoTime uint64 `json:"ioTime"`
WeightedIO uint64 `json:"weightedIO"`
Name string `json:"name"`
SerialNumber string `json:"serialNumber"`
Label string `json:"label"`
}
例如:
{
"readCount":19,
"mergedReadCount":0,
"writeCount":48,
"mergedWriteCount":0,
"readBytes":102400,
"writeBytes":196608,
"readTime":0,
"writeTime":13,
"iopsInProgress":0,
"ioTime":0,
"weightedIO":0,
"name":"D:",
"serialNumber":"",
"label":""
}
字段 | 描述 |
---|---|
ReadCount | 磁盘读取的数据个数 |
MergedReadCount | - |
WriteCount | 磁盘写入的数据个数 |
MergedWriteCount | - |
ReadBytes | 磁盘读取的总字节数据量 |
WriteBytes | 磁盘写入的总字节数据量 |
网友评论