1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-26 13:48:59 +08:00
shirou_gopsutil/disk_linux.go

98 lines
2.0 KiB
Go
Raw Normal View History

2014-04-23 10:30:39 +09:00
// +build linux
package gopsutil
import (
"strconv"
"strings"
)
2014-04-29 14:59:22 +09:00
const (
2014-08-15 20:05:26 +02:00
SectorSize = 512
)
// Get disk partitions.
// should use setmntent(3) but this implement use /etc/mtab file
2014-04-30 15:32:05 +09:00
func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
2014-04-23 10:30:39 +09:00
filename := "/etc/mtab"
2014-04-30 16:22:54 +09:00
lines, err := readLines(filename)
2014-04-29 14:59:22 +09:00
if err != nil {
2014-05-01 12:47:43 +09:00
return nil, err
}
2014-05-01 12:47:43 +09:00
ret := make([]DiskPartitionStat, 0, len(lines))
2014-04-29 14:59:22 +09:00
for _, line := range lines {
fields := strings.Fields(line)
2014-04-30 16:16:07 +09:00
d := DiskPartitionStat{
Device: fields[0],
Mountpoint: fields[1],
Fstype: fields[2],
Opts: fields[3],
}
ret = append(ret, d)
}
2014-04-23 10:30:39 +09:00
return ret, nil
}
2014-04-29 14:59:22 +09:00
2014-04-30 15:32:05 +09:00
func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
filename := "/proc/diskstats"
2014-04-30 16:22:54 +09:00
lines, err := readLines(filename)
2014-04-29 14:59:22 +09:00
if err != nil {
2014-05-01 12:47:43 +09:00
return nil, err
2014-04-29 14:59:22 +09:00
}
2014-05-01 12:47:43 +09:00
ret := make(map[string]DiskIOCountersStat, 0)
empty := DiskIOCountersStat{}
2014-04-29 14:59:22 +09:00
for _, line := range lines {
fields := strings.Fields(line)
name := fields[2]
reads, err := strconv.ParseUint((fields[3]), 10, 64)
if err != nil {
return ret, err
}
rbytes, err := strconv.ParseUint((fields[5]), 10, 64)
if err != nil {
return ret, err
}
rtime, err := strconv.ParseUint((fields[6]), 10, 64)
if err != nil {
return ret, err
}
writes, err := strconv.ParseUint((fields[7]), 10, 64)
if err != nil {
return ret, err
}
wbytes, err := strconv.ParseUint((fields[9]), 10, 64)
if err != nil {
return ret, err
}
wtime, err := strconv.ParseUint((fields[10]), 10, 64)
if err != nil {
return ret, err
}
iotime, err := strconv.ParseUint((fields[12]), 10, 64)
if err != nil {
return ret, err
}
d := DiskIOCountersStat{
ReadBytes: uint64(rbytes) * SectorSize,
WriteBytes: uint64(wbytes) * SectorSize,
ReadCount: uint64(reads),
WriteCount: uint64(writes),
ReadTime: uint64(rtime),
WriteTime: uint64(wtime),
IoTime: uint64(iotime),
}
if d == empty {
continue
2014-04-29 14:59:22 +09:00
}
d.Name = name
ret[name] = d
2014-04-29 14:59:22 +09:00
}
return ret, nil
}