Skip to content

Commit

Permalink
windows: GetWmic returns [][]string where string split by ","
Browse files Browse the repository at this point in the history
  • Loading branch information
shirou committed Mar 20, 2015
1 parent 1f5fdd1 commit 5f8b99a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
30 changes: 23 additions & 7 deletions common/common_windows.go
Expand Up @@ -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
Expand Down
26 changes: 11 additions & 15 deletions cpu/cpu_windows.go
Expand Up @@ -4,7 +4,6 @@ package cpu

import (
"strconv"
"strings"
"syscall"
"time"
"unsafe"
Expand Down Expand Up @@ -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
Expand All @@ -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),
Expand All @@ -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
}
Expand Down
10 changes: 3 additions & 7 deletions host/host_windows.go
Expand Up @@ -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
}
Expand Down

0 comments on commit 5f8b99a

Please sign in to comment.