gupta.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package gupta
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. )
  7. const version = "0.0.1"
  8. func Hello() string {
  9. return fmt.Sprintf("Welcome to gupta version %s", version)
  10. }
  11. func Run(load, cpu, memory bool, partition, networkInterface string) (cpuLoad, cpuUsage, memoryUsage float64) {
  12. cpuLoad = 0.2
  13. cpuUsage = 0.2
  14. memoryUsage = 70.0
  15. return cpuLoad, cpuUsage, memoryUsage
  16. }
  17. type GuptaReport struct {
  18. cpuLoad float64
  19. cpuUsage float64
  20. memoryUsage float64
  21. }
  22. func (g *GuptaReport) GetCPULoad() string {
  23. return fmt.Sprintf("%.2f", g.cpuLoad)
  24. }
  25. // collect all CPU stat from /proc/cpustat
  26. type CPUStat struct {
  27. user uint64
  28. nice uint64
  29. system uint64
  30. idle uint64
  31. iowait uint64
  32. idleNew uint64
  33. iowaitNew uint64
  34. irq uint64
  35. softirq uint64
  36. steal uint64
  37. guest uint64
  38. guest_nice uint64
  39. totalTimeNew uint64
  40. }
  41. // parse the first line of /proc/cpustat without the word cpu
  42. func (c *CPUStat) ReadInfo(rawInfo []string) {
  43. if s, err := strconv.ParseUint(rawInfo[0], 10, 64); err == nil {
  44. c.user = s
  45. }
  46. if s, err := strconv.ParseUint(rawInfo[1], 10, 64); err == nil {
  47. c.nice = s
  48. }
  49. if s, err := strconv.ParseUint(rawInfo[2], 10, 64); err == nil {
  50. c.system = s
  51. }
  52. if s, err := strconv.ParseUint(rawInfo[3], 10, 64); err == nil {
  53. c.idle = s
  54. }
  55. if s, err := strconv.ParseUint(rawInfo[4], 10, 64); err == nil {
  56. c.iowait = s
  57. }
  58. if s, err := strconv.ParseUint(rawInfo[5], 10, 64); err == nil {
  59. c.irq = s
  60. }
  61. if s, err := strconv.ParseUint(rawInfo[6], 10, 64); err == nil {
  62. c.softirq = s
  63. }
  64. if s, err := strconv.ParseUint(rawInfo[7], 10, 64); err == nil {
  65. c.steal = s
  66. }
  67. if s, err := strconv.ParseUint(rawInfo[8], 10, 64); err == nil {
  68. c.guest = s
  69. }
  70. if s, err := strconv.ParseUint(rawInfo[9], 10, 64); err == nil {
  71. c.guest_nice = s
  72. }
  73. }
  74. func (c *CPUStat) ReadInfoNew(rawInfo []string) {
  75. idleNew, _ := strconv.ParseUint(rawInfo[3], 10, 64)
  76. iowaitNew, _ := strconv.ParseUint(rawInfo[4], 10, 64)
  77. c.idleNew = idleNew
  78. c.iowaitNew = iowaitNew
  79. for _, s := range rawInfo {
  80. u, _ := strconv.ParseUint(s, 10, 64)
  81. c.totalTimeNew += u
  82. }
  83. }
  84. func (c *CPUStat) TotalTime() uint64 {
  85. return c.user + c.nice + c.system + c.idle + c.iowait + c.irq + c.softirq + c.steal
  86. }
  87. func (c *CPUStat) TotalTimeNew() uint64 {
  88. return c.totalTimeNew
  89. }
  90. func (c *CPUStat) IdleTime() uint64 {
  91. return c.idle + c.iowait
  92. }
  93. func (c *CPUStat) IdleTimeNew() uint64 {
  94. return c.idleNew + c.iowaitNew
  95. }
  96. func (c *CPUStat) CPUUsage() float64 {
  97. deltaIdleTime := c.IdleTimeNew() - c.IdleTime()
  98. deltaTotalTime := c.TotalTimeNew() - c.TotalTime()
  99. cpuUsage := (1.0 - float64(deltaIdleTime)/float64(deltaTotalTime)) * 100
  100. return cpuUsage
  101. }
  102. func NewCPUStat(procstatline, procstatlineNew string) CPUStat {
  103. cpustat := CPUStat{}
  104. statline := strings.Fields(procstatline)
  105. statlineNew := strings.Fields(procstatlineNew)
  106. cpustat.ReadInfo(statline[1:])
  107. cpustat.ReadInfoNew(statlineNew[1:])
  108. return cpustat
  109. }