From 9beacb51c5f26abad48a6627946343f00bafb04d Mon Sep 17 00:00:00 2001 From: WAKAYAMA shirou Date: Sat, 20 Sep 2014 10:22:41 +0900 Subject: [PATCH] fix error handling. --- cpu_freebsd.go | 25 ++++++++++++++++++----- cpu_linux.go | 55 ++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 64 insertions(+), 16 deletions(-) diff --git a/cpu_freebsd.go b/cpu_freebsd.go index 4657702..ecf9304 100644 --- a/cpu_freebsd.go +++ b/cpu_freebsd.go @@ -32,11 +32,26 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) { return ret, err } - user, _ := strconv.ParseFloat(cpuTime[CPUser], 32) - nice, _ := strconv.ParseFloat(cpuTime[CPNice], 32) - sys, _ := strconv.ParseFloat(cpuTime[CPSys], 32) - idle, _ := strconv.ParseFloat(cpuTime[CPIdle], 32) - intr, _ := strconv.ParseFloat(cpuTime[CPIntr], 32) + user, err := strconv.ParseFloat(cpuTime[CPUser], 32) + if err != nil { + return ret, err + } + nice, err := strconv.ParseFloat(cpuTime[CPNice], 32) + if err != nil { + return ret, err + } + sys, err := strconv.ParseFloat(cpuTime[CPSys], 32) + if err != nil { + return ret, err + } + idle, err := strconv.ParseFloat(cpuTime[CPIdle], 32) + if err != nil { + return ret, err + } + intr, err := strconv.ParseFloat(cpuTime[CPIntr], 32) + if err != nil { + return ret, err + } c := CPUTimesStat{ User: float32(user / ClocksPerSec), diff --git a/cpu_linux.go b/cpu_linux.go index ada85e6..36c4490 100644 --- a/cpu_linux.go +++ b/cpu_linux.go @@ -106,14 +106,38 @@ func parseStatLine(line string) (*CPUTimesStat, error) { if cpu == "cpu" { cpu = "cpu-total" } - user, _ := strconv.ParseFloat(fields[1], 32) - nice, _ := strconv.ParseFloat(fields[2], 32) - system, _ := strconv.ParseFloat(fields[3], 32) - idle, _ := strconv.ParseFloat(fields[4], 32) - iowait, _ := strconv.ParseFloat(fields[5], 32) - irq, _ := strconv.ParseFloat(fields[6], 32) - softirq, _ := strconv.ParseFloat(fields[7], 32) - stolen, _ := strconv.ParseFloat(fields[8], 32) + user, err := strconv.ParseFloat(fields[1], 32) + if err != nil { + return nil, err + } + nice, err := strconv.ParseFloat(fields[2], 32) + if err != nil { + return nil, err + } + system, err := strconv.ParseFloat(fields[3], 32) + if err != nil { + return nil, err + } + idle, err := strconv.ParseFloat(fields[4], 32) + if err != nil { + return nil, err + } + iowait, err := strconv.ParseFloat(fields[5], 32) + if err != nil { + return nil, err + } + irq, err := strconv.ParseFloat(fields[6], 32) + if err != nil { + return nil, err + } + softirq, err := strconv.ParseFloat(fields[7], 32) + if err != nil { + return nil, err + } + stolen, err := strconv.ParseFloat(fields[8], 32) + if err != nil { + return nil, err + } ct := &CPUTimesStat{ CPU: cpu, User: float32(user), @@ -126,15 +150,24 @@ func parseStatLine(line string) (*CPUTimesStat, error) { Stolen: float32(stolen), } if len(fields) > 9 { // Linux >= 2.6.11 - steal, _ := strconv.ParseFloat(fields[9], 32) + steal, err := strconv.ParseFloat(fields[9], 32) + if err != nil { + return nil, err + } ct.Steal = float32(steal) } if len(fields) > 10 { // Linux >= 2.6.24 - guest, _ := strconv.ParseFloat(fields[10], 32) + guest, err := strconv.ParseFloat(fields[10], 32) + if err != nil { + return nil, err + } ct.Guest = float32(guest) } if len(fields) > 11 { // Linux >= 3.2.0 - guestNice, _ := strconv.ParseFloat(fields[11], 32) + guestNice, err := strconv.ParseFloat(fields[11], 32) + if err != nil { + return nil, err + } ct.GuestNice = float32(guestNice) }