mirror of
https://github.com/shirou/gopsutil.git
synced 2025-04-26 13:48:59 +08:00
73 lines
1.4 KiB
Go
73 lines
1.4 KiB
Go
// +build linux
|
|
|
|
package gopsutil
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
SECTOR_SIZE = 512
|
|
)
|
|
|
|
// Get disk partitions.
|
|
// should use setmntent(3) but this implement use /etc/mtab file
|
|
func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
|
|
|
|
filename := "/etc/mtab"
|
|
lines, err := readLines(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ret := make([]DiskPartitionStat, 0, len(lines))
|
|
|
|
for _, line := range lines {
|
|
fields := strings.Fields(line)
|
|
d := DiskPartitionStat{
|
|
Mountpoint: fields[1],
|
|
Fstype: fields[2],
|
|
Opts: fields[3],
|
|
}
|
|
ret = append(ret, d)
|
|
}
|
|
|
|
return ret, nil
|
|
}
|
|
|
|
func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
|
|
filename := "/proc/diskstats"
|
|
lines, err := readLines(filename)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ret := make(map[string]DiskIOCountersStat, 0)
|
|
empty := DiskIOCountersStat{}
|
|
|
|
for _, line := range lines {
|
|
fields := strings.Fields(line)
|
|
name := fields[2]
|
|
reads := mustParseUint64(fields[3])
|
|
rbytes := mustParseUint64(fields[5])
|
|
rtime := mustParseUint64(fields[6])
|
|
writes := mustParseUint64(fields[7])
|
|
wbytes := mustParseUint64(fields[9])
|
|
wtime := mustParseUint64(fields[10])
|
|
d := DiskIOCountersStat{
|
|
ReadBytes: rbytes * SECTOR_SIZE,
|
|
WriteBytes: wbytes * SECTOR_SIZE,
|
|
ReadCount: reads,
|
|
WriteCount: writes,
|
|
ReadTime: rtime,
|
|
WriteTime: wtime,
|
|
}
|
|
if d == empty {
|
|
continue
|
|
}
|
|
d.Name = name
|
|
|
|
ret[name] = d
|
|
}
|
|
return ret, nil
|
|
}
|