1
0
Fork 0
goprocfs/uptime.go

34 lines
672 B
Go
Raw Permalink Normal View History

2014-07-06 07:05:32 +00:00
package procfs
2014-07-06 06:59:58 +00:00
import (
"fmt"
"io/ioutil"
)
// Proc contains process information obtained from /proc/uptime
// Its fields are documented using PROC(5) man page
type ProcUptime struct {
Uptime float64 // Uptime of the system in seconds
Idle float64 // Time spent in idle process in seconds
}
2014-07-15 07:35:19 +00:00
func NewProcUptime() (ProcUptime, error) {
p := ProcUptime{}
2014-07-06 13:49:20 +00:00
b, err := ioutil.ReadFile("/proc/uptime")
2014-07-06 06:59:58 +00:00
if err != nil {
2014-07-15 07:35:19 +00:00
return p, err
2014-07-06 06:59:58 +00:00
}
2014-07-06 13:49:20 +00:00
parsed, err := fmt.Sscanf(string(b), "%f %f", &p.Uptime, &p.Idle)
2014-07-06 06:59:58 +00:00
if parsed < 2 {
2014-07-15 07:35:19 +00:00
err := fmt.Errorf("Managed to parse only %d fields out of 2", parsed)
return p, err
2014-07-06 06:59:58 +00:00
}
if err != nil {
2014-07-15 07:35:19 +00:00
return p, err
2014-07-06 06:59:58 +00:00
}
2014-07-15 07:35:19 +00:00
return p, nil
2014-07-06 06:59:58 +00:00
}