提交 90b8a216 编写于 作者: W WAKAYAMA shirou

remove mustParse on frebsd.

上级 a2d4c5dc
......@@ -62,12 +62,20 @@ func CPUInfo() ([]CPUInfoStat, error) {
for _, line := range lines {
if matches := regexp.MustCompile(`CPU:\s+(.+) \(([\d.]+).+\)`).FindStringSubmatch(line); matches != nil {
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 {
c.VendorID = matches[1]
c.Family = matches[3]
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 {
for _, v := range strings.Split(matches[1], ",") {
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 {
// 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 {
// 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
......
......@@ -4,30 +4,75 @@ package gopsutil
import (
"os/exec"
"strconv"
"strings"
)
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")
p := mustParseUint64(pageSize[0])
pageCount, err := doSysctrl("vm.stats.vm.v_page_count")
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")
free, _ := doSysctrl("vm.stats.vm.v_free_count")
active, _ := doSysctrl("vm.stats.vm.v_active_count")
inactive, _ := doSysctrl("vm.stats.vm.v_inactive_count")
cache, _ := doSysctrl("vm.stats.vm.v_cache_count")
buffer, _ := doSysctrl("vfs.bufspace")
wired, _ := doSysctrl("vm.stats.vm.v_wire_count")
parsed := make([]uint64, 0, 7)
vv := []string{
pageCount[0],
free[0],
active[0],
inactive[0],
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{
Total: mustParseUint64(pageCount[0]) * p,
Free: mustParseUint64(free[0]) * p,
Active: mustParseUint64(active[0]) * p,
Inactive: mustParseUint64(inactive[0]) * p,
Cached: mustParseUint64(cache[0]) * p,
Buffers: mustParseUint64(buffer[0]),
Wired: mustParseUint64(wired[0]) * p,
Total: parsed[0] * p,
Free: parsed[1] * p,
Active: parsed[2] * p,
Inactive: parsed[3] * p,
Cached: parsed[4] * p,
Buffers: parsed[5],
Wired: parsed[6] * p,
}
// TODO: platform independent (worked freebsd?)
......@@ -55,12 +100,28 @@ func SwapMemory() (*SwapMemoryStat, error) {
}
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{
Total: mustParseUint64(values[1]),
Used: mustParseUint64(values[2]),
Free: mustParseUint64(values[3]),
UsedPercent: mustParseFloat64(u),
Total: total_v,
Used: used_v,
Free: free_v,
UsedPercent: up_v,
}
}
......
......@@ -10,7 +10,6 @@ func TestVirtual_memory(t *testing.T) {
if err != nil {
t.Errorf("error %v", err)
}
empty := &VirtualMemoryStat{}
if v == empty {
t.Errorf("error %v", v)
......
......@@ -4,6 +4,7 @@ package gopsutil
import (
"os/exec"
"strconv"
"strings"
)
......@@ -27,16 +28,40 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
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{
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],
BytesRecv: parsed[3],
PacketsSent: parsed[4],
Errout: parsed[5],
BytesSent: parsed[6],
Dropout: parsed[7],
}
ret = append(ret, n)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册