Return errors instead of panicing
This commit is contained in:
parent
6d8e80e5c2
commit
41afa60dce
15
loadavg.go
15
loadavg.go
|
@ -25,23 +25,24 @@ type ProcLoadavg struct {
|
|||
LastPid uint
|
||||
}
|
||||
|
||||
func NewProcLoadavg() ProcLoadavg {
|
||||
func NewProcLoadavg() (ProcLoadavg, error) {
|
||||
p := ProcLoadavg{}
|
||||
|
||||
b, err := ioutil.ReadFile("/proc/loadavg")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return p, err
|
||||
}
|
||||
|
||||
p := ProcLoadavg{}
|
||||
|
||||
parsed, err := fmt.Sscanf(string(b), "%f %f %f %d/%d %d",
|
||||
&p.Avg1Min, &p.Avg5Min, &p.Avg15Min, &p.RunnableEntities,
|
||||
&p.TotalEntities, &p.LastPid)
|
||||
if parsed < 6 {
|
||||
fmt.Println("Managed to parse only", parsed, "fields out of 6")
|
||||
err := fmt.Errorf("Managed to parse only %d fields out of 6", parsed)
|
||||
return p, err
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return p, err
|
||||
}
|
||||
|
||||
return p
|
||||
return p, nil
|
||||
}
|
||||
|
|
15
pid_stat.go
15
pid_stat.go
|
@ -210,15 +210,15 @@ type ProcPidStat struct {
|
|||
CguestTime int32
|
||||
}
|
||||
|
||||
func NewProcPidStat(pid int) ProcPidStat {
|
||||
func NewProcPidStat(pid int) (ProcPidStat, error) {
|
||||
p := ProcPidStat{}
|
||||
|
||||
file := fmt.Sprintf("/proc/%d/stat", pid)
|
||||
b, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return p, err
|
||||
}
|
||||
|
||||
p := ProcPidStat{}
|
||||
|
||||
parsed, err := fmt.Sscanf(string(b),
|
||||
"%d %s %s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d "+
|
||||
"%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
|
||||
|
@ -231,11 +231,12 @@ func NewProcPidStat(pid int) ProcPidStat {
|
|||
&p.Nswap, &p.Cnswap, &p.ExitSignal, &p.Processor, &p.RtPriority,
|
||||
&p.Policy, &p.DelayacctBlkioTicks, &p.GuestTime, &p.CguestTime)
|
||||
if parsed < 44 {
|
||||
fmt.Println("Managed to parse only", parsed, "fields out of 44")
|
||||
err := fmt.Errorf("Managed to parse only %d fields out of 44", parsed)
|
||||
return p, err
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return p, err
|
||||
}
|
||||
|
||||
return p
|
||||
return p, nil
|
||||
}
|
||||
|
|
15
pid_statm.go
15
pid_statm.go
|
@ -17,24 +17,25 @@ type ProcPidStatm struct {
|
|||
Dt uint64 // Dirty pages (unused in Linux 2.6)
|
||||
}
|
||||
|
||||
func NewProcPidStatm(pid int) ProcPidStatm {
|
||||
func NewProcPidStatm(pid int) (ProcPidStatm, error) {
|
||||
p := ProcPidStatm{}
|
||||
|
||||
file := fmt.Sprintf("/proc/%d/statm", pid)
|
||||
b, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return p, err
|
||||
}
|
||||
|
||||
p := ProcPidStatm{}
|
||||
|
||||
parsed, err := fmt.Sscanf(string(b), "%d %d %d %d %d %d %d",
|
||||
&p.Size, &p.Resident, &p.Share, &p.Text, &p.Lib, &p.Data, &p.Dt)
|
||||
|
||||
if parsed < 7 {
|
||||
fmt.Println("Managed to parse only", parsed, "fields out of 7")
|
||||
err := fmt.Errorf("Managed to parse only %d fields out of 7", parsed)
|
||||
return p, err
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return p, err
|
||||
}
|
||||
|
||||
return p
|
||||
return p, nil
|
||||
}
|
||||
|
|
15
stat.go
15
stat.go
|
@ -29,24 +29,25 @@ type ProcStat struct {
|
|||
}
|
||||
}
|
||||
|
||||
func NewProcStat() ProcStat {
|
||||
func NewProcStat() (ProcStat, error) {
|
||||
p := ProcStat{}
|
||||
|
||||
b, err := ioutil.ReadFile("/proc/stat")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return p, err
|
||||
}
|
||||
|
||||
p := ProcStat{}
|
||||
|
||||
parsed, err := fmt.Sscanf(string(b), "cpu %d %d %d %d %d %d %d %d %d %d",
|
||||
&p.Cpu.User, &p.Cpu.Nice, &p.Cpu.System, &p.Cpu.Idle, &p.Cpu.Iowait,
|
||||
&p.Cpu.Irq, &p.Cpu.Softirq, &p.Cpu.Steal, &p.Cpu.Guest,
|
||||
&p.Cpu.GuestNice)
|
||||
if parsed < 10 {
|
||||
fmt.Println("Managed to parse only", parsed, "fields out of 10")
|
||||
err := fmt.Errorf("Managed to parse only %d fields out of 10", parsed)
|
||||
return p, err
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return p, err
|
||||
}
|
||||
|
||||
return p
|
||||
return p, nil
|
||||
}
|
||||
|
|
15
uptime.go
15
uptime.go
|
@ -12,21 +12,22 @@ type ProcUptime struct {
|
|||
Idle float64 // Time spent in idle process in seconds
|
||||
}
|
||||
|
||||
func NewProcUptime() ProcUptime {
|
||||
func NewProcUptime() (ProcUptime, error) {
|
||||
p := ProcUptime{}
|
||||
|
||||
b, err := ioutil.ReadFile("/proc/uptime")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return p, err
|
||||
}
|
||||
|
||||
p := ProcUptime{}
|
||||
|
||||
parsed, err := fmt.Sscanf(string(b), "%f %f", &p.Uptime, &p.Idle)
|
||||
if parsed < 2 {
|
||||
fmt.Println("Managed to parse only", parsed, "fields out of 2")
|
||||
err := fmt.Errorf("Managed to parse only %d fields out of 2", parsed)
|
||||
return p, err
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return p, err
|
||||
}
|
||||
|
||||
return p
|
||||
return p, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue