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

94 lines
2.0 KiB
Go
Raw Normal View History

2014-04-23 10:30:39 +09:00
// +build linux
package gopsutil
import (
"strings"
2014-04-29 14:59:22 +09:00
"unicode"
)
2014-04-29 14:59:22 +09:00
const (
SECTOR_SIZE = 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-30 16:16:07 +09:00
var ret []DiskPartitionStat
2014-04-23 10:30:39 +09:00
filename := "/etc/mtab"
lines, err := ReadLines(filename)
2014-04-29 14:59:22 +09:00
if err != nil {
return ret, err
}
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{
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) {
2014-04-30 16:16:07 +09:00
ret := make(map[string]DiskIOCountersStat, 0)
2014-04-29 14:59:22 +09:00
// determine partitions we want to look for
filename := "/proc/partitions"
lines, err := ReadLines(filename)
if err != nil {
return ret, err
}
2014-04-30 16:16:07 +09:00
var partitions []string
2014-04-29 14:59:22 +09:00
for _, line := range lines[2:] {
fields := strings.Fields(line)
name := []rune(fields[3])
if unicode.IsDigit(name[len(name)-1]) {
partitions = append(partitions, fields[3])
} else {
// http://code.google.com/p/psutil/issues/detail?id=338
lenpart := len(partitions)
if lenpart == 0 || strings.HasPrefix(partitions[lenpart-1], fields[3]) {
partitions = append(partitions, fields[3])
}
}
}
filename = "/proc/diskstats"
lines, err = ReadLines(filename)
if err != nil {
return ret, err
}
for _, line := range lines {
fields := strings.Fields(line)
name := fields[2]
reads := parseUint64(fields[3])
rbytes := parseUint64(fields[5])
rtime := parseUint64(fields[6])
writes := parseUint64(fields[7])
wbytes := parseUint64(fields[9])
wtime := parseUint64(fields[10])
if stringContains(partitions, name) {
2014-04-30 16:16:07 +09:00
d := DiskIOCountersStat{
2014-04-29 14:59:22 +09:00
Name: name,
ReadBytes: rbytes * SECTOR_SIZE,
WriteBytes: wbytes * SECTOR_SIZE,
ReadCount: reads,
WriteCount: writes,
ReadTime: rtime,
WriteTime: wtime,
}
ret[name] = d
}
}
return ret, nil
}