1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-26 13:48:59 +08:00

remove "mustParse" function on darwin.

This commit is contained in:
Shirou WAKAYAMA 2014-09-20 10:17:00 +09:00
parent 2cc4321636
commit a7a157d0f7
3 changed files with 35 additions and 27 deletions

View File

@ -79,6 +79,10 @@ func CPUInfo() ([]CPUInfoStat, error) {
for _, line := range strings.Split(string(out), "\n") {
values := strings.Fields(line)
t, err := strconv.ParseInt(values[1], 10, 32)
if err != nil {
return ret, err
}
if strings.HasPrefix(line, "machdep.cpu.brand_string") {
c.ModelName = strings.Join(values[1:], " ")
} else if strings.HasPrefix(line, "machdep.cpu.family") {
@ -86,12 +90,7 @@ func CPUInfo() ([]CPUInfoStat, error) {
} else if strings.HasPrefix(line, "machdep.cpu.model") {
c.Model = values[1]
} else if strings.HasPrefix(line, "machdep.cpu.stepping") {
t, err := strconv.ParseInt(values[1], 10, 32)
if err != nil {
return ret, err
}
c.Stepping = int32(t)
} else if strings.HasPrefix(line, "machdep.cpu.features") {
for _, v := range values[1:] {
c.Flags = append(c.Flags, strings.ToLower(v))
@ -105,18 +104,9 @@ func CPUInfo() ([]CPUInfoStat, error) {
c.Flags = append(c.Flags, strings.ToLower(v))
}
} else if strings.HasPrefix(line, "machdep.cpu.core_count") {
t, err := strconv.ParseInt(values[1], 10, 32)
if err != nil {
return ret, err
}
c.Cores = t
c.Cores = int32(t)
} else if strings.HasPrefix(line, "machdep.cpu.cache.size") {
t, err := strconv.ParseInt(values[1], 10, 32)
if err != nil {
return ret, err
}
c.CacheSize = t
c.CacheSize = int32(t)
} else if strings.HasPrefix(line, "machdep.cpu.vendor") {
c.VendorID = values[1]
}

View File

@ -60,7 +60,11 @@ func HostInfo() (*HostInfoStat, error) {
if err == nil {
// ex: { sec = 1392261637, usec = 627534 } Thu Feb 13 12:20:37 2014
v := strings.Replace(values[2], ",", "", 1)
ret.Uptime = mustParseUint64(v)
t, err := strconv.ParseUint(v, 10, 64)
if err != nil {
return ret, err
}
ret.Uptime = t
}
return ret, nil

View File

@ -4,6 +4,7 @@ package gopsutil
import (
"os/exec"
"strconv"
"strings"
)
@ -19,6 +20,7 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
for _, line := range lines {
values := strings.Fields(line)
if len(values) < 1 || values[0] == "Name" {
// skip first line
continue
}
base := 1
@ -27,18 +29,30 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
base = 0
}
parsed := make([]uint64, 0, 3)
vv := []string{
values[base+3], // PacketsRecv
values[base+4], // Errin
values[base+5], // Dropin
}
for _, target := range vv {
if target == "-" {
parsed = append(parsed, 0)
continue
}
t, err := strconv.ParseUint(target, 10, 64)
if err != nil {
return nil, err
}
parsed = append(parsed, t)
}
n := NetIOCountersStat{
Name: values[0],
PacketsRecv: mustParseUint64(values[base+3]),
Errin: mustParseUint64(values[base+4]),
Dropin: mustParseUint64(values[base+5]),
/*
BytesRecv: mustParseUint64(values[base+6]),
PacketsSent: mustParseUint64(values[base+7]),
Errout: mustParseUint64(values[base+8]),
BytesSent: mustParseUint64(values[base+9]),
Dropout: mustParseUint64(values[base+11]),
*/
PacketsRecv: parsed[0],
Errin: parsed[1],
Dropin: parsed[2],
}
ret = append(ret, n)
}