remove mustParse on frebsd.

pull/16/head
WAKAYAMA shirou 11 years ago
parent a2d4c5dcb8
commit 90b8a2160b

@ -62,12 +62,20 @@ func CPUInfo() ([]CPUInfoStat, error) {
for _, line := range lines { for _, line := range lines {
if matches := regexp.MustCompile(`CPU:\s+(.+) \(([\d.]+).+\)`).FindStringSubmatch(line); matches != nil { if matches := regexp.MustCompile(`CPU:\s+(.+) \(([\d.]+).+\)`).FindStringSubmatch(line); matches != nil {
c.ModelName = matches[1] c.ModelName = matches[1]
c.Mhz = mustParseFloat64(matches[2]) t, err := strconv.ParseFloat(matches[2], 64)
if err != nil {
return ret, nil
}
c.Mhz = t
} else if matches := regexp.MustCompile(`Origin = "(.+)" Id = (.+) Family = (.+) Model = (.+) Stepping = (.+)`).FindStringSubmatch(line); matches != nil { } else if matches := regexp.MustCompile(`Origin = "(.+)" Id = (.+) Family = (.+) Model = (.+) Stepping = (.+)`).FindStringSubmatch(line); matches != nil {
c.VendorID = matches[1] c.VendorID = matches[1]
c.Family = matches[3] c.Family = matches[3]
c.Model = matches[4] c.Model = matches[4]
c.Stepping = mustParseInt32(matches[5]) t, err := strconv.ParseInt(matches[5], 10, 32)
if err != nil {
return ret, nil
}
c.Stepping = int32(t)
} else if matches := regexp.MustCompile(`Features=.+<(.+)>`).FindStringSubmatch(line); matches != nil { } else if matches := regexp.MustCompile(`Features=.+<(.+)>`).FindStringSubmatch(line); matches != nil {
for _, v := range strings.Split(matches[1], ",") { for _, v := range strings.Split(matches[1], ",") {
c.Flags = append(c.Flags, strings.ToLower(v)) c.Flags = append(c.Flags, strings.ToLower(v))
@ -78,7 +86,11 @@ func CPUInfo() ([]CPUInfoStat, error) {
} }
} else if matches := regexp.MustCompile(`Logical CPUs per core: (\d+)`).FindStringSubmatch(line); matches != nil { } else if matches := regexp.MustCompile(`Logical CPUs per core: (\d+)`).FindStringSubmatch(line); matches != nil {
// FIXME: no this line? // FIXME: no this line?
c.Cores = mustParseInt32(matches[1]) t, err := strconv.ParseInt(matches[1], 10, 32)
if err != nil {
return ret, nil
}
c.Cores = int32(t)
} }
} }

@ -42,7 +42,11 @@ func HostInfo() (*HostInfoStat, error) {
if err == nil { if err == nil {
// ex: { sec = 1392261637, usec = 627534 } Thu Feb 13 12:20:37 2014 // ex: { sec = 1392261637, usec = 627534 } Thu Feb 13 12:20:37 2014
v := strings.Replace(values[2], ",", "", 1) 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 return ret, nil

@ -4,30 +4,75 @@ package gopsutil
import ( import (
"os/exec" "os/exec"
"strconv"
"strings" "strings"
) )
func VirtualMemory() (*VirtualMemoryStat, error) { func VirtualMemory() (*VirtualMemoryStat, error) {
pageSize, err := doSysctrl("vm.stats.vm.v_page_size")
if err != nil {
return nil, err
}
p, err := strconv.ParseUint(pageSize[0], 10, 64)
if err != nil {
return nil, err
}
pageSize, _ := doSysctrl("vm.stats.vm.v_page_size") pageCount, err := doSysctrl("vm.stats.vm.v_page_count")
p := mustParseUint64(pageSize[0]) if err != nil {
return nil, err
}
free, err := doSysctrl("vm.stats.vm.v_free_count")
if err != nil {
return nil, err
}
active, err := doSysctrl("vm.stats.vm.v_active_count")
if err != nil {
return nil, err
}
inactive, err := doSysctrl("vm.stats.vm.v_inactive_count")
if err != nil {
return nil, err
}
cache, err := doSysctrl("vm.stats.vm.v_cache_count")
if err != nil {
return nil, err
}
buffer, err := doSysctrl("vfs.bufspace")
if err != nil {
return nil, err
}
wired, err := doSysctrl("vm.stats.vm.v_wire_count")
if err != nil {
return nil, err
}
pageCount, _ := doSysctrl("vm.stats.vm.v_page_count") parsed := make([]uint64, 0, 7)
free, _ := doSysctrl("vm.stats.vm.v_free_count") vv := []string{
active, _ := doSysctrl("vm.stats.vm.v_active_count") pageCount[0],
inactive, _ := doSysctrl("vm.stats.vm.v_inactive_count") free[0],
cache, _ := doSysctrl("vm.stats.vm.v_cache_count") active[0],
buffer, _ := doSysctrl("vfs.bufspace") inactive[0],
wired, _ := doSysctrl("vm.stats.vm.v_wire_count") cache[0],
buffer[0],
wired[0],
}
for _, target := range vv {
t, err := strconv.ParseUint(target, 10, 64)
if err != nil {
return nil, err
}
parsed = append(parsed, t)
}
ret := &VirtualMemoryStat{ ret := &VirtualMemoryStat{
Total: mustParseUint64(pageCount[0]) * p, Total: parsed[0] * p,
Free: mustParseUint64(free[0]) * p, Free: parsed[1] * p,
Active: mustParseUint64(active[0]) * p, Active: parsed[2] * p,
Inactive: mustParseUint64(inactive[0]) * p, Inactive: parsed[3] * p,
Cached: mustParseUint64(cache[0]) * p, Cached: parsed[4] * p,
Buffers: mustParseUint64(buffer[0]), Buffers: parsed[5],
Wired: mustParseUint64(wired[0]) * p, Wired: parsed[6] * p,
} }
// TODO: platform independent (worked freebsd?) // TODO: platform independent (worked freebsd?)
@ -55,12 +100,28 @@ func SwapMemory() (*SwapMemoryStat, error) {
} }
u := strings.Replace(values[4], "%", "", 1) u := strings.Replace(values[4], "%", "", 1)
total_v, err := strconv.ParseUint(values[1], 10, 64)
if err != nil {
return nil, err
}
used_v, err := strconv.ParseUint(values[2], 10, 64)
if err != nil {
return nil, err
}
free_v, err := strconv.ParseUint(values[3], 10, 64)
if err != nil {
return nil, err
}
up_v, err := strconv.ParseFloat(u, 64)
if err != nil {
return nil, err
}
ret = &SwapMemoryStat{ ret = &SwapMemoryStat{
Total: mustParseUint64(values[1]), Total: total_v,
Used: mustParseUint64(values[2]), Used: used_v,
Free: mustParseUint64(values[3]), Free: free_v,
UsedPercent: mustParseFloat64(u), UsedPercent: up_v,
} }
} }

@ -10,7 +10,6 @@ func TestVirtual_memory(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("error %v", err) t.Errorf("error %v", err)
} }
empty := &VirtualMemoryStat{} empty := &VirtualMemoryStat{}
if v == empty { if v == empty {
t.Errorf("error %v", v) t.Errorf("error %v", v)

@ -4,6 +4,7 @@ package gopsutil
import ( import (
"os/exec" "os/exec"
"strconv"
"strings" "strings"
) )
@ -27,16 +28,40 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
base = 0 base = 0
} }
parsed := make([]uint64, 0, 8)
vv := []string{
values[base+3], // PacketsRecv
values[base+4], // Errin
values[base+5], // Dropin
values[base+6], // BytesRecvn
values[base+7], // PacketSent
values[base+8], // Errout
values[base+9], // BytesSent
values[base+11], // Dropout
}
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{ n := NetIOCountersStat{
Name: values[0], Name: values[0],
PacketsRecv: mustParseUint64(values[base+3]), PacketsRecv: parsed[0],
Errin: mustParseUint64(values[base+4]), Errin: parsed[1],
Dropin: mustParseUint64(values[base+5]), Dropin: parsed[2],
BytesRecv: mustParseUint64(values[base+6]), BytesRecv: parsed[3],
PacketsSent: mustParseUint64(values[base+7]), PacketsSent: parsed[4],
Errout: mustParseUint64(values[base+8]), Errout: parsed[5],
BytesSent: mustParseUint64(values[base+9]), BytesSent: parsed[6],
Dropout: mustParseUint64(values[base+11]), Dropout: parsed[7],
} }
ret = append(ret, n) ret = append(ret, n)
} }

Loading…
Cancel
Save