windows: GetWmic returns [][]string where string split by ","

pull/45/head
WAKAYAMA Shirou 10 years ago
parent 1f5fdd1fba
commit 5f8b99aa65

@ -76,18 +76,34 @@ func BytePtrToString(p *uint8) string {
} }
// exec wmic and return lines splited by newline // exec wmic and return lines splited by newline
func GetWmic(target string, query string) ([]string, error) { func GetWmic(target string, query ...string) ([][]string, error) {
ret, err := exec.Command("wmic", target, "get", query, "/format:csv").Output() cmd := []string{target}
cmd = append(cmd, query...)
cmd = append(cmd, "/format:csv")
out, err := exec.Command("wmic", cmd...).Output()
if err != nil { 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 { 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 // CounterInfo

@ -4,7 +4,6 @@ package cpu
import ( import (
"strconv" "strconv"
"strings"
"syscall" "syscall"
"time" "time"
"unsafe" "unsafe"
@ -44,15 +43,11 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
func CPUInfo() ([]CPUInfoStat, error) { func CPUInfo() ([]CPUInfoStat, error) {
var ret []CPUInfoStat 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 { if err != nil {
return ret, err return ret, err
} }
for i, l := range lines { for i, t := range lines {
t := strings.Split(l, ",")
if len(t) < 2 {
continue
}
cache, err := strconv.Atoi(t[2]) cache, err := strconv.Atoi(t[2])
if err != nil { if err != nil {
cache = 0 cache = 0
@ -61,10 +56,13 @@ func CPUInfo() ([]CPUInfoStat, error) {
if err != nil { if err != nil {
cores = 0 cores = 0
} }
stepping, err := strconv.Atoi(t[7]) stepping := 0
if len(t) > 7 {
stepping, err = strconv.Atoi(t[6])
if err != nil { if err != nil {
stepping = 0 stepping = 0
} }
}
cpu := CPUInfoStat{ cpu := CPUInfoStat{
CPU: int32(i), CPU: int32(i),
Family: t[1], Family: t[1],
@ -84,17 +82,15 @@ func CPUInfo() ([]CPUInfoStat, error) {
func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) { func CPUPercent(interval time.Duration, percpu bool) ([]float64, error) {
ret := []float64{} ret := []float64{}
lines, err := common.GetWmic("cpu", "loadpercentage") lines, err := common.GetWmic("cpu", "get", "loadpercentage")
if err != nil { if err != nil {
return ret, err return ret, err
} }
for _, l := range lines { for _, l := range lines {
t := strings.Split(l, ",") if len(l) < 2 {
if len(t) < 2 {
continue continue
} }
p, err := strconv.Atoi(t[1]) p, err := strconv.Atoi(l[1])
if err != nil { if err != nil {
p = 0 p = 0
} }

@ -40,19 +40,15 @@ func HostInfo() (*HostInfoStat, error) {
} }
func BootTime() (uint64, error) { func BootTime() (uint64, error) {
lines, err := common.GetWmic("os", "LastBootUpTime") lines, err := common.GetWmic("os", "get", "LastBootUpTime")
if err != nil { if err != nil {
return 0, err 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") 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" 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 { if err != nil {
return 0, err return 0, err
} }

Loading…
Cancel
Save