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

121 lines
2.5 KiB
Go
Raw Normal View History

2014-04-23 10:30:39 +09:00
// +build linux
package gopsutil
import (
2014-09-22 16:35:47 +09:00
"fmt"
"os/exec"
"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{
2014-09-20 00:21:26 +09:00
ReadBytes: rbytes * SectorSize,
WriteBytes: wbytes * SectorSize,
ReadCount: reads,
WriteCount: writes,
ReadTime: rtime,
WriteTime: wtime,
IoTime: iotime,
}
if d == empty {
continue
2014-04-29 14:59:22 +09:00
}
d.Name = name
2014-09-22 16:35:47 +09:00
d.SerialNumber = GetDiskSerialNumber(name)
ret[name] = d
2014-04-29 14:59:22 +09:00
}
return ret, nil
}
2014-09-22 16:35:47 +09:00
func GetDiskSerialNumber(name string) (string, error) {
n := fmt.Sprintf("--name=%s", name)
out, err := exec.Command("/sbin/udevadm", "info", "--query=property", n).Output()
// does not return error, just an empty string
if err != nil {
return "", nil
}
lines := strings.Split(string(out), "\n")
for _, line := range lines {
values := strings.Split(line, "=")
if len(values) < 2 || values[0] != "ID_SERIAL" {
// only get ID_SERIAL, not ID_SERIAL_SHORT
continue
}
return values[1], nil
}
return "", nil
}