disk_linux.go 1.4 KB
Newer Older
W
WAKAYAMA Shirou 已提交
1 2 3 4
// +build linux

package gopsutil

5 6 7 8
import (
	"strings"
)

9
const (
L
Lukas Lueg 已提交
10
	SectorSize = 512
11 12 13 14
)

// Get disk partitions.
// should use setmntent(3) but this implement use /etc/mtab file
W
WAKAYAMA Shirou 已提交
15
func DiskPartitions(all bool) ([]DiskPartitionStat, error) {
W
WAKAYAMA Shirou 已提交
16

17
	filename := "/etc/mtab"
W
WAKAYAMA shirou 已提交
18
	lines, err := readLines(filename)
19
	if err != nil {
S
Shirou WAKAYAMA 已提交
20
		return nil, err
21 22
	}

S
Shirou WAKAYAMA 已提交
23 24
	ret := make([]DiskPartitionStat, 0, len(lines))

25
	for _, line := range lines {
26
		fields := strings.Fields(line)
S
Shirou WAKAYAMA 已提交
27
		d := DiskPartitionStat{
28
			Device:	    fields[0],
29 30 31 32 33 34 35
			Mountpoint: fields[1],
			Fstype:     fields[2],
			Opts:       fields[3],
		}
		ret = append(ret, d)
	}

W
WAKAYAMA Shirou 已提交
36 37
	return ret, nil
}
38

W
WAKAYAMA Shirou 已提交
39
func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
40
	filename := "/proc/diskstats"
W
WAKAYAMA shirou 已提交
41
	lines, err := readLines(filename)
42
	if err != nil {
S
Shirou WAKAYAMA 已提交
43
		return nil, err
44
	}
S
Shirou WAKAYAMA 已提交
45
	ret := make(map[string]DiskIOCountersStat, 0)
46 47
	empty := DiskIOCountersStat{}

48 49 50
	for _, line := range lines {
		fields := strings.Fields(line)
		name := fields[2]
51 52 53 54 55 56
		reads := mustParseUint64(fields[3])
		rbytes := mustParseUint64(fields[5])
		rtime := mustParseUint64(fields[6])
		writes := mustParseUint64(fields[7])
		wbytes := mustParseUint64(fields[9])
		wtime := mustParseUint64(fields[10])
57
		d := DiskIOCountersStat{
L
Lukas Lueg 已提交
58 59
			ReadBytes:  rbytes * SectorSize,
			WriteBytes: wbytes * SectorSize,
60 61 62 63 64 65 66
			ReadCount:  reads,
			WriteCount: writes,
			ReadTime:   rtime,
			WriteTime:  wtime,
		}
		if d == empty {
			continue
67
		}
68 69 70
		d.Name = name

		ret[name] = d
71 72 73
	}
	return ret, nil
}