diff --git a/README.rst b/README.rst index bc4614f..9848aec 100644 --- a/README.rst +++ b/README.rst @@ -117,6 +117,10 @@ Several methods have been added which are not present in psutil, but will provid - VirtualizationSystem (ex: "LXC") - VirtualizationRole (ex: "guest"/"host") +- IOCounters + + - Label (linux only) The registered [device mapper name](https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-block-dm) + - cpu/CPUInfo() (linux, freebsd) - CPU (ex: 0, 1, ...) diff --git a/disk/disk.go b/disk/disk.go index 1c31047..38d8a8f 100644 --- a/disk/disk.go +++ b/disk/disk.go @@ -42,6 +42,7 @@ type IOCountersStat struct { WeightedIO uint64 `json:"weightedIO"` Name string `json:"name"` SerialNumber string `json:"serialNumber"` + Label string `json:"label"` } func (d UsageStat) String() string { diff --git a/disk/disk_linux.go b/disk/disk_linux.go index 46904e3..8ca7ab6 100644 --- a/disk/disk_linux.go +++ b/disk/disk_linux.go @@ -5,6 +5,7 @@ package disk import ( "context" "fmt" + "io/ioutil" "os/exec" "path/filepath" "strconv" @@ -370,6 +371,8 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC d.Name = name d.SerialNumber = GetDiskSerialNumber(name) + d.Label = GetLabel(name) + ret[name] = d } return ret, nil @@ -406,6 +409,26 @@ func GetDiskSerialNumberWithContext(ctx context.Context, name string) string { return "" } +// GetLabel returns label of given device or empty string on error. +// Name of device is expected, eg. /dev/sda +// Supports label based on devicemapper name +// See https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-block-dm +func GetLabel(name string) string { + // Try label based on devicemapper name + dmname_filename := common.HostSys(fmt.Sprintf("block/%s/dm/name", name)) + + if !common.PathExists(dmname_filename) { + return "" + } + + dmname, err := ioutil.ReadFile(dmname_filename) + if err != nil { + return "" + } else { + return dmname + } +} + func getFsType(stat unix.Statfs_t) string { t := int64(stat.Type) ret, ok := fsTypeMap[t]