uptime.go 858 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package linux
  2. import (
  3. "io/ioutil"
  4. "strconv"
  5. "strings"
  6. "time"
  7. )
  8. type Uptime struct {
  9. Total float64 `json:"total"`
  10. Idle float64 `json:"idle"`
  11. }
  12. func (self *Uptime) GetTotalDuration() time.Duration {
  13. return time.Duration(self.Total) * time.Second
  14. }
  15. func (self *Uptime) GetIdleDuration() time.Duration {
  16. return time.Duration(self.Idle) * time.Second
  17. }
  18. func (self *Uptime) CalculateIdle() float64 {
  19. // XXX
  20. // num2/(num1*N) # N = SMP CPU numbers
  21. return 0
  22. }
  23. func ReadUptime(path string) (*Uptime, error) {
  24. b, err := ioutil.ReadFile(path)
  25. if err != nil {
  26. return nil, err
  27. }
  28. fields := strings.Fields(string(b))
  29. uptime := Uptime{}
  30. if uptime.Total, err = strconv.ParseFloat(fields[0], 64); err != nil {
  31. return nil, err
  32. }
  33. if uptime.Idle, err = strconv.ParseFloat(fields[1], 64); err != nil {
  34. return nil, err
  35. }
  36. return &uptime, nil
  37. }