fix error handling.

pull/16/head
WAKAYAMA shirou 11 years ago
parent a7a157d0f7
commit 9beacb51c5

@ -32,11 +32,26 @@ func CPUTimes(percpu bool) ([]CPUTimesStat, error) {
return ret, err return ret, err
} }
user, _ := strconv.ParseFloat(cpuTime[CPUser], 32) user, err := strconv.ParseFloat(cpuTime[CPUser], 32)
nice, _ := strconv.ParseFloat(cpuTime[CPNice], 32) if err != nil {
sys, _ := strconv.ParseFloat(cpuTime[CPSys], 32) return ret, err
idle, _ := strconv.ParseFloat(cpuTime[CPIdle], 32) }
intr, _ := strconv.ParseFloat(cpuTime[CPIntr], 32) 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{ c := CPUTimesStat{
User: float32(user / ClocksPerSec), User: float32(user / ClocksPerSec),

@ -106,14 +106,38 @@ func parseStatLine(line string) (*CPUTimesStat, error) {
if cpu == "cpu" { if cpu == "cpu" {
cpu = "cpu-total" cpu = "cpu-total"
} }
user, _ := strconv.ParseFloat(fields[1], 32) user, err := strconv.ParseFloat(fields[1], 32)
nice, _ := strconv.ParseFloat(fields[2], 32) if err != nil {
system, _ := strconv.ParseFloat(fields[3], 32) return nil, err
idle, _ := strconv.ParseFloat(fields[4], 32) }
iowait, _ := strconv.ParseFloat(fields[5], 32) nice, err := strconv.ParseFloat(fields[2], 32)
irq, _ := strconv.ParseFloat(fields[6], 32) if err != nil {
softirq, _ := strconv.ParseFloat(fields[7], 32) return nil, err
stolen, _ := strconv.ParseFloat(fields[8], 32) }
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{ ct := &CPUTimesStat{
CPU: cpu, CPU: cpu,
User: float32(user), User: float32(user),
@ -126,15 +150,24 @@ func parseStatLine(line string) (*CPUTimesStat, error) {
Stolen: float32(stolen), Stolen: float32(stolen),
} }
if len(fields) > 9 { // Linux >= 2.6.11 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) ct.Steal = float32(steal)
} }
if len(fields) > 10 { // Linux >= 2.6.24 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) ct.Guest = float32(guest)
} }
if len(fields) > 11 { // Linux >= 3.2.0 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) ct.GuestNice = float32(guestNice)
} }

Loading…
Cancel
Save