Remove sprintf where it's not required
This commit is contained in:
parent
07579de372
commit
6d8e80e5c2
|
@ -26,15 +26,14 @@ type ProcLoadavg struct {
|
|||
}
|
||||
|
||||
func NewProcLoadavg() ProcLoadavg {
|
||||
file := fmt.Sprintf("/proc/loadavg")
|
||||
bytes, err := ioutil.ReadFile(file)
|
||||
b, err := ioutil.ReadFile("/proc/loadavg")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
p := ProcLoadavg{}
|
||||
|
||||
parsed, err := fmt.Sscanf(string(bytes), "%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.TotalEntities, &p.LastPid)
|
||||
if parsed < 6 {
|
||||
|
|
|
@ -212,14 +212,14 @@ type ProcPidStat struct {
|
|||
|
||||
func NewProcPidStat(pid int) ProcPidStat {
|
||||
file := fmt.Sprintf("/proc/%d/stat", pid)
|
||||
bytes, err := ioutil.ReadFile(file)
|
||||
b, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
p := ProcPidStat{}
|
||||
|
||||
parsed, err := fmt.Sscanf(string(bytes),
|
||||
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",
|
||||
&p.Pid, &p.Comm, &p.State, &p.Ppid, &p.Pgrp, &p.Session, &p.TtyNr,
|
||||
|
|
|
@ -19,14 +19,14 @@ type ProcPidStatm struct {
|
|||
|
||||
func NewProcPidStatm(pid int) ProcPidStatm {
|
||||
file := fmt.Sprintf("/proc/%d/statm", pid)
|
||||
bytes, err := ioutil.ReadFile(file)
|
||||
b, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
p := ProcPidStatm{}
|
||||
|
||||
parsed, err := fmt.Sscanf(string(bytes), "%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)
|
||||
|
||||
if parsed < 7 {
|
||||
|
|
5
stat.go
5
stat.go
|
@ -30,15 +30,14 @@ type ProcStat struct {
|
|||
}
|
||||
|
||||
func NewProcStat() ProcStat {
|
||||
file := fmt.Sprintf("/proc/stat")
|
||||
bytes, err := ioutil.ReadFile(file)
|
||||
b, err := ioutil.ReadFile("/proc/stat")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
p := ProcStat{}
|
||||
|
||||
parsed, err := fmt.Sscanf(string(bytes), "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.Irq, &p.Cpu.Softirq, &p.Cpu.Steal, &p.Cpu.Guest,
|
||||
&p.Cpu.GuestNice)
|
||||
|
|
|
@ -13,15 +13,14 @@ type ProcUptime struct {
|
|||
}
|
||||
|
||||
func NewProcUptime() ProcUptime {
|
||||
file := fmt.Sprintf("/proc/uptime")
|
||||
bytes, err := ioutil.ReadFile(file)
|
||||
b, err := ioutil.ReadFile("/proc/uptime")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
p := ProcUptime{}
|
||||
|
||||
parsed, err := fmt.Sscanf(string(bytes), "%f %f", &p.Uptime, &p.Idle)
|
||||
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")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue