From 5f8b99aa65e1fe622f2a2c2a2449b79c2d261bb6 Mon Sep 17 00:00:00 2001 From: WAKAYAMA Shirou Date: Fri, 20 Mar 2015 22:48:58 +0900 Subject: [PATCH] windows: GetWmic returns [][]string where string split by "," --- common/common_windows.go | 30 +++++++++++++++++++++++------- cpu/cpu_windows.go | 26 +++++++++++--------------- host/host_windows.go | 10 +++------- 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/common/common_windows.go b/common/common_windows.go index d148e54..4cc0ba9 100644 --- a/common/common_windows.go +++ b/common/common_windows.go @@ -76,18 +76,34 @@ func BytePtrToString(p *uint8) string { } // exec wmic and return lines splited by newline -func GetWmic(target string, query string) ([]string, error) { - ret, err := exec.Command("wmic", target, "get", query, "/format:csv").Output() +func GetWmic(target string, query ...string) ([][]string, error) { + cmd := []string{target} + cmd = append(cmd, query...) + cmd = append(cmd, "/format:csv") + out, err := exec.Command("wmic", cmd...).Output() if err != nil { - return []string{}, err + return [][]string{}, err } - lines := strings.Split(string(ret), "\r\r\n") + lines := strings.Split(string(out), "\r\r\n") if len(lines) <= 2 { - return []string{}, fmt.Errorf("wmic result malformed: [%q]", lines) + return [][]string{}, fmt.Errorf("wmic result malformed: [%q]", lines) } + var ret [][]string + for _, l := range lines[2:] { // skip first two lines + var lr []string + for _, r := range strings.Split(l, ",") { + if r == "" { + continue + } + lr = append(lr, strings.TrimSpace(r)) + } + if len(lr) != 0 { + ret = append(ret, lr) + } + } + + return ret, nil - // skip first two line - return lines[2:], nil } // CounterInfo diff --git a/cpu/cpu_windows.go b/cpu/cpu_windows.go index a19e4ce..c5f8c1d 100644 --- a/cpu/cpu_windows.go +++ b/cpu/cpu_windows.go @@ -4,7 +4,6 @@ package cpu import ( "strconv" - "strings" "syscall" "time" "unsafe" @@ -44,15 +43,11 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) { func CPUInfo() ([]CPUInfoStat, error) { var ret []CPUInfoStat - lines, err := common.GetWmic("cpu", "Family,L2CacheSize,Manufacturer,Name,NumberOfLogicalProcessors,ProcessorId,Stepping") + lines, err := common.GetWmic("cpu", "get", "Family,L2CacheSize,Manufacturer,Name,NumberOfLogicalProcessors,ProcessorId,Stepping") if err != nil { return ret, err } - for i, l := range lines { - t := strings.Split(l, ",") - if len(t) < 2 { - continue - } + for i, t := range lines { cache, err := strconv.Atoi(t[2]) if err != nil { cache = 0 @@ -61,9 +56,12 @@ func CPUInfo() ([]CPUInfoStat, error) { if err != nil { cores = 0 } - stepping, err := strconv.Atoi(t[7]) - if err != nil { - stepping = 0 + stepping := 0 + if len(t) > 7 { + stepping, err = strconv.Atoi(t[6]) + if err != nil { + stepping = 0 + } } cpu := CPUInfoStat{ CPU: int32(i), @@ -84,17 +82,15 @@ func CPUInfo() ([]CPUInfoStat, error) { func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) { ret := []float64{} - lines, err := common.GetWmic("cpu", "loadpercentage") + lines, err := common.GetWmic("cpu", "get", "loadpercentage") if err != nil { return ret, err } for _, l := range lines { - t := strings.Split(l, ",") - - if len(t) < 2 { + if len(l) < 2 { continue } - p, err := strconv.Atoi(t[1]) + p, err := strconv.Atoi(l[1]) if err != nil { p = 0 } diff --git a/host/host_windows.go b/host/host_windows.go index 203d7ee..cfbdd69 100644 --- a/host/host_windows.go +++ b/host/host_windows.go @@ -40,19 +40,15 @@ func HostInfo() (*HostInfoStat, error) { } func BootTime() (uint64, error) { - lines, err := common.GetWmic("os", "LastBootUpTime") + lines, err := common.GetWmic("os", "get", "LastBootUpTime") if err != nil { return 0, err } - if len(lines) == 0 || lines[0] == "" { + if len(lines) == 0 || len(lines[0]) != 2 { return 0, fmt.Errorf("could not get LastBootUpTime") } - l := strings.Split(lines[0], ",") - if len(l) != 2 { - return 0, fmt.Errorf("could not parse LastBootUpTime") - } format := "20060102150405" - t, err := time.Parse(format, strings.Split(l[1], ".")[0]) + t, err := time.Parse(format, strings.Split(lines[0][1], ".")[0]) if err != nil { return 0, err }