add SerialNumber to DiskIOCounterStat.

pull/17/head
Shirou WAKAYAMA 11 years ago
parent 6f9abdb869
commit 9dbd4898b2

@ -24,14 +24,15 @@ type DiskPartitionStat struct {
} }
type DiskIOCountersStat struct { type DiskIOCountersStat struct {
ReadCount uint64 `json:"readCount"` ReadCount uint64 `json:"readCount"`
WriteCount uint64 `json:"writeCount"` WriteCount uint64 `json:"writeCount"`
ReadBytes uint64 `json:"readBytes"` ReadBytes uint64 `json:"readBytes"`
WriteBytes uint64 `json:"writeBytes"` WriteBytes uint64 `json:"writeBytes"`
ReadTime uint64 `json:"readTime"` ReadTime uint64 `json:"readTime"`
WriteTime uint64 `json:"writeTime"` WriteTime uint64 `json:"writeTime"`
Name string `json:"name"` Name string `json:"name"`
IoTime uint64 `json:"ioTime"` IoTime uint64 `json:"ioTime"`
SerialNumber string `json:"serialNumber"`
} }
func (d DiskUsageStat) String() string { func (d DiskUsageStat) String() string {

@ -3,6 +3,8 @@
package gopsutil package gopsutil
import ( import (
"fmt"
"os/exec"
"strconv" "strconv"
"strings" "strings"
) )
@ -91,7 +93,28 @@ func DiskIOCounters() (map[string]DiskIOCountersStat, error) {
} }
d.Name = name d.Name = name
d.SerialNumber = GetDiskSerialNumber(name)
ret[name] = d ret[name] = d
} }
return ret, nil return ret, nil
} }
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
}

@ -79,13 +79,14 @@ func TestDiskPartitionStat_String(t *testing.T) {
func TestDiskIOCountersStat_String(t *testing.T) { func TestDiskIOCountersStat_String(t *testing.T) {
v := DiskIOCountersStat{ v := DiskIOCountersStat{
Name: "sd01", Name: "sd01",
ReadCount: 100, ReadCount: 100,
WriteCount: 200, WriteCount: 200,
ReadBytes: 300, ReadBytes: 300,
WriteBytes: 400, WriteBytes: 400,
SerialNumber: "SERIAL",
} }
e := `{"readCount":100,"writeCount":200,"readBytes":300,"writeBytes":400,"readTime":0,"writeTime":0,"name":"sd01","ioTime":0}` e := `{"readCount":100,"writeCount":200,"readBytes":300,"writeBytes":400,"readTime":0,"writeTime":0,"name":"sd01","ioTime":0,"serialNumber":"SERIAL"}`
if e != fmt.Sprintf("%v", v) { if e != fmt.Sprintf("%v", v) {
t.Errorf("DiskUsageStat string is invalid: %v", v) t.Errorf("DiskUsageStat string is invalid: %v", v)
} }

Loading…
Cancel
Save