美文网首页
shirou/gopsutil/disk

shirou/gopsutil/disk

作者: JunChow520 | 来源:发表于2021-12-27 21:47 被阅读0次

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 磁盘写入的总字节数据量

相关文章

  • shirou/gopsutil/disk

    shirou/gopsutil/disk子包用于获取磁盘信息,包括IO统计、分区、使用率等。 分区信息 disk....

  • shirou/gopsutil/cpu

    psutil是一个跨平台进程和系统监控的Python库,gopsutil是其Go版本的实现。 仓库地址 https...

  • Golang 扩展包推荐

    系统资源管理 https://github.com/shirou/gopsutil 多进程管理工具 https:/...

  • gopsutil

    psutil是一个跨平台进程和系统监控的Python库,而gopsutil是其Go语言版本的实现。本文介绍了它的基...

  • To Shirou and Saber

    When the sword broke the rocks, They cannot escape their ...

  • 查看硬盘序列号命令

    cmd diskpart list disk select disk 0 detail disk

  • U盘只读模式,指定U盘只读

    diskpart 工具 list disk select disk 1 attributes disk clear...

  • system.disk Metrics from DataDog

    Name system.disk.total KB (unit)The total amount of disk ...

  • DISK

    添加新硬盘后不重启服务器就使用 通过重新扫描SCSI (注: Small Computer System Inte...

  • 2 创建分区并装载

    Disk Info To purchase a cloud disk of 10GB and mount it t...

网友评论

      本文标题:shirou/gopsutil/disk

      本文链接:https://www.haomeiwen.com/subject/denyqrtx.html