stat.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package linux
  2. import (
  3. "io/ioutil"
  4. "strconv"
  5. "strings"
  6. "time"
  7. )
  8. type Stat struct {
  9. CPUStatAll CPUStat `json:"cpu_all"`
  10. CPUStats []CPUStat `json:"cpus"`
  11. Interrupts uint64 `json:"intr"`
  12. ContextSwitches uint64 `json:"ctxt"`
  13. BootTime time.Time `json:"btime"`
  14. Processes uint64 `json:"processes"`
  15. ProcsRunning uint64 `json:"procs_running"`
  16. ProcsBlocked uint64 `json:"procs_blocked"`
  17. }
  18. type CPUStat struct {
  19. Id string `json:"id"`
  20. User uint64 `json:"user"`
  21. Nice uint64 `json:"nice"`
  22. System uint64 `json:"system"`
  23. Idle uint64 `json:"idle"`
  24. IOWait uint64 `json:"iowait"`
  25. IRQ uint64 `json:"irq"`
  26. SoftIRQ uint64 `json:"softirq"`
  27. Steal uint64 `json:"steal"`
  28. Guest uint64 `json:"guest"`
  29. GuestNice uint64 `json:"guest_nice"`
  30. }
  31. func createCPUStat(fields []string) *CPUStat {
  32. s := CPUStat{}
  33. s.Id = fields[0]
  34. for i := 1; i < len(fields); i++ {
  35. v, _ := strconv.ParseUint(fields[i], 10, 64)
  36. switch i {
  37. case 1:
  38. s.User = v
  39. case 2:
  40. s.Nice = v
  41. case 3:
  42. s.System = v
  43. case 4:
  44. s.Idle = v
  45. case 5:
  46. s.IOWait = v
  47. case 6:
  48. s.IRQ = v
  49. case 7:
  50. s.SoftIRQ = v
  51. case 8:
  52. s.Steal = v
  53. case 9:
  54. s.Guest = v
  55. case 10:
  56. s.GuestNice = v
  57. }
  58. }
  59. return &s
  60. }
  61. func ReadStat(path string) (*Stat, error) {
  62. b, err := ioutil.ReadFile(path)
  63. if err != nil {
  64. return nil, err
  65. }
  66. content := string(b)
  67. lines := strings.Split(content, "\n")
  68. var stat Stat = Stat{}
  69. for i, line := range lines {
  70. fields := strings.Fields(line)
  71. if len(fields) == 0 {
  72. continue
  73. }
  74. if fields[0][:3] == "cpu" {
  75. if cpuStat := createCPUStat(fields); cpuStat != nil {
  76. if i == 0 {
  77. stat.CPUStatAll = *cpuStat
  78. } else {
  79. stat.CPUStats = append(stat.CPUStats, *cpuStat)
  80. }
  81. }
  82. } else if fields[0] == "intr" {
  83. stat.Interrupts, _ = strconv.ParseUint(fields[1], 10, 64)
  84. } else if fields[0] == "ctxt" {
  85. stat.ContextSwitches, _ = strconv.ParseUint(fields[1], 10, 64)
  86. } else if fields[0] == "btime" {
  87. seconds, _ := strconv.ParseInt(fields[1], 10, 64)
  88. stat.BootTime = time.Unix(seconds, 0)
  89. } else if fields[0] == "processes" {
  90. stat.Processes, _ = strconv.ParseUint(fields[1], 10, 64)
  91. } else if fields[0] == "procs_running" {
  92. stat.ProcsRunning, _ = strconv.ParseUint(fields[1], 10, 64)
  93. } else if fields[0] == "procs_blocked" {
  94. stat.ProcsBlocked, _ = strconv.ParseUint(fields[1], 10, 64)
  95. }
  96. }
  97. return &stat, nil
  98. }