systemstat_linux.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright (c) 2013 Phillip Bond
  2. // Licensed under the MIT License
  3. // see file LICENSE
  4. // +build linux
  5. package systemstat
  6. import (
  7. "bufio"
  8. "bytes"
  9. "io"
  10. "io/ioutil"
  11. "strconv"
  12. "strings"
  13. "syscall"
  14. "time"
  15. )
  16. func getUptime(procfile string) (uptime UptimeSample) {
  17. // read in whole uptime file with cpu usage information ;"/proc/uptime"
  18. contents, err := ioutil.ReadFile(procfile)
  19. uptime.Time = time.Now()
  20. if err != nil {
  21. return
  22. }
  23. reader := bufio.NewReader(bytes.NewBuffer(contents))
  24. line, _, err := reader.ReadLine()
  25. fields := strings.Fields(string(line))
  26. val, numerr := strconv.ParseFloat(fields[0], 64)
  27. if numerr != nil {
  28. return
  29. }
  30. uptime.Uptime = val
  31. return
  32. }
  33. func getLoadAvgSample(procfile string) (samp LoadAvgSample) {
  34. // read in whole loadavg file with cpu usage information ;"/proc/loadavg"
  35. contents, err := ioutil.ReadFile(procfile)
  36. samp.Time = time.Now()
  37. if err != nil {
  38. return
  39. }
  40. reader := bufio.NewReader(bytes.NewBuffer(contents))
  41. line, _, err := reader.ReadLine()
  42. fields := strings.Fields(string(line))
  43. for i := 0; i < 3; i++ {
  44. val, numerr := strconv.ParseFloat(fields[i], 64)
  45. if numerr != nil {
  46. return
  47. }
  48. switch i {
  49. case 0:
  50. samp.One = val
  51. case 1:
  52. samp.Five = val
  53. case 2:
  54. samp.Fifteen = val
  55. }
  56. }
  57. return
  58. }
  59. func getMemSample(procfile string) (samp MemSample) {
  60. want := map[string]bool{
  61. "Buffers:": true,
  62. "Cached:": true,
  63. "MemTotal:": true,
  64. "MemFree:": true,
  65. "MemUsed:": true,
  66. "SwapTotal:": true,
  67. "SwapFree:": true,
  68. "SwapUsed:": true}
  69. // read in whole meminfo file with cpu usage information ;"/proc/meminfo"
  70. contents, err := ioutil.ReadFile(procfile)
  71. samp.Time = time.Now()
  72. if err != nil {
  73. return
  74. }
  75. reader := bufio.NewReader(bytes.NewBuffer(contents))
  76. for {
  77. line, _, err := reader.ReadLine()
  78. if err == io.EOF {
  79. break
  80. }
  81. fields := strings.Fields(string(line))
  82. fieldName := fields[0]
  83. _, ok := want[fieldName]
  84. if ok && len(fields) == 3 {
  85. val, numerr := strconv.ParseUint(fields[1], 10, 64)
  86. if numerr != nil {
  87. return
  88. }
  89. switch fieldName {
  90. case "Buffers:":
  91. samp.Buffers = val
  92. case "Cached:":
  93. samp.Cached = val
  94. case "MemTotal:":
  95. samp.MemTotal = val
  96. case "MemFree:":
  97. samp.MemFree = val
  98. case "SwapTotal:":
  99. samp.SwapTotal = val
  100. case "SwapFree:":
  101. samp.SwapFree = val
  102. }
  103. }
  104. }
  105. samp.MemUsed = samp.MemTotal - samp.MemFree
  106. samp.SwapUsed = samp.SwapTotal - samp.SwapFree
  107. return
  108. }
  109. func getProcCPUSample() (s ProcCPUSample) {
  110. var processInfo syscall.Rusage
  111. syscall.Getrusage(syscall.RUSAGE_SELF, &processInfo)
  112. s.Time = time.Now()
  113. s.ProcMemUsedK = int64(processInfo.Maxrss)
  114. s.User = float64(processInfo.Utime.Usec)/1000000 + float64(processInfo.Utime.Sec)
  115. s.System = float64(processInfo.Stime.Usec)/1000000 + float64(processInfo.Stime.Sec)
  116. s.Total = s.User + s.System
  117. return
  118. }
  119. func getCPUSample(procfile string) (samp CPUSample) {
  120. // read in whole proc file with cpu usage information ; "/proc/stat"
  121. contents, err := ioutil.ReadFile(procfile)
  122. samp.Time = time.Now()
  123. if err != nil {
  124. return
  125. }
  126. reader := bufio.NewReader(bytes.NewBuffer(contents))
  127. for {
  128. line, _, err := reader.ReadLine()
  129. if err == io.EOF {
  130. break
  131. }
  132. fields := strings.Fields(string(line))
  133. if len(fields) > 0 {
  134. fieldName := fields[0]
  135. if fieldName == "cpu" {
  136. parseCPUFields(fields, &samp)
  137. }
  138. }
  139. }
  140. return
  141. }