1
0
Fork 0

Return errors instead of panicing

This commit is contained in:
Gregory Eremin 2014-07-15 14:35:19 +07:00
parent 6d8e80e5c2
commit 41afa60dce
No known key found for this signature in database
GPG Key ID: 5EFA427EEC26E86C
5 changed files with 40 additions and 35 deletions

View File

@ -25,23 +25,24 @@ type ProcLoadavg struct {
LastPid uint LastPid uint
} }
func NewProcLoadavg() ProcLoadavg { func NewProcLoadavg() (ProcLoadavg, error) {
p := ProcLoadavg{}
b, err := ioutil.ReadFile("/proc/loadavg") b, err := ioutil.ReadFile("/proc/loadavg")
if err != nil { if err != nil {
panic(err) return p, err
} }
p := ProcLoadavg{}
parsed, err := fmt.Sscanf(string(b), "%f %f %f %d/%d %d", parsed, err := fmt.Sscanf(string(b), "%f %f %f %d/%d %d",
&p.Avg1Min, &p.Avg5Min, &p.Avg15Min, &p.RunnableEntities, &p.Avg1Min, &p.Avg5Min, &p.Avg15Min, &p.RunnableEntities,
&p.TotalEntities, &p.LastPid) &p.TotalEntities, &p.LastPid)
if parsed < 6 { 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 { if err != nil {
panic(err) return p, err
} }
return p return p, nil
} }

View File

@ -210,15 +210,15 @@ type ProcPidStat struct {
CguestTime int32 CguestTime int32
} }
func NewProcPidStat(pid int) ProcPidStat { func NewProcPidStat(pid int) (ProcPidStat, error) {
p := ProcPidStat{}
file := fmt.Sprintf("/proc/%d/stat", pid) file := fmt.Sprintf("/proc/%d/stat", pid)
b, err := ioutil.ReadFile(file) b, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
panic(err) return p, err
} }
p := ProcPidStat{}
parsed, err := fmt.Sscanf(string(b), 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 %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", "%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.Nswap, &p.Cnswap, &p.ExitSignal, &p.Processor, &p.RtPriority,
&p.Policy, &p.DelayacctBlkioTicks, &p.GuestTime, &p.CguestTime) &p.Policy, &p.DelayacctBlkioTicks, &p.GuestTime, &p.CguestTime)
if parsed < 44 { 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 { if err != nil {
panic(err) return p, err
} }
return p return p, nil
} }

View File

@ -17,24 +17,25 @@ type ProcPidStatm struct {
Dt uint64 // Dirty pages (unused in Linux 2.6) 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) file := fmt.Sprintf("/proc/%d/statm", pid)
b, err := ioutil.ReadFile(file) b, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
panic(err) return p, err
} }
p := ProcPidStatm{}
parsed, err := fmt.Sscanf(string(b), "%d %d %d %d %d %d %d", 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) &p.Size, &p.Resident, &p.Share, &p.Text, &p.Lib, &p.Data, &p.Dt)
if parsed < 7 { 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 { if err != nil {
panic(err) return p, err
} }
return p return p, nil
} }

15
stat.go
View File

@ -29,24 +29,25 @@ type ProcStat struct {
} }
} }
func NewProcStat() ProcStat { func NewProcStat() (ProcStat, error) {
p := ProcStat{}
b, err := ioutil.ReadFile("/proc/stat") b, err := ioutil.ReadFile("/proc/stat")
if err != nil { 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", 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.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.Irq, &p.Cpu.Softirq, &p.Cpu.Steal, &p.Cpu.Guest,
&p.Cpu.GuestNice) &p.Cpu.GuestNice)
if parsed < 10 { 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 { if err != nil {
panic(err) return p, err
} }
return p return p, nil
} }

View File

@ -12,21 +12,22 @@ type ProcUptime struct {
Idle float64 // Time spent in idle process in seconds Idle float64 // Time spent in idle process in seconds
} }
func NewProcUptime() ProcUptime { func NewProcUptime() (ProcUptime, error) {
p := ProcUptime{}
b, err := ioutil.ReadFile("/proc/uptime") b, err := ioutil.ReadFile("/proc/uptime")
if err != nil { if err != nil {
panic(err) return p, err
} }
p := ProcUptime{}
parsed, err := fmt.Sscanf(string(b), "%f %f", &p.Uptime, &p.Idle) parsed, err := fmt.Sscanf(string(b), "%f %f", &p.Uptime, &p.Idle)
if parsed < 2 { 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 { if err != nil {
panic(err) return p, err
} }
return p return p, nil
} }