gupta.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package gupta
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. const version = "0.0.1"
  10. func Hello() string {
  11. return fmt.Sprintf("Welcome to gupta version %s", version)
  12. }
  13. func Run(load, cpu, memory bool, partition, networkInterface string) (cpuLoad, cpuUsage, memoryUsage float64) {
  14. cpuLoad = 0.2
  15. cpuUsage = 0.2
  16. memoryUsage = 70.0
  17. return cpuLoad, cpuUsage, memoryUsage
  18. }
  19. type GuptaReport struct {
  20. LoadAvarage float64
  21. CPUUsage *CPUStat `json:"cpu usage, omitempty"`
  22. MemoryUsage *Memory `json:"memory,omitempty"`
  23. }
  24. // usually you'd want a g GuptaReport reciever here ...
  25. // but than you could now check for the object properties ...
  26. func (g *GuptaReport) MarshalJSON() ([]byte, error) {
  27. t := time.Now()
  28. report := []byte(fmt.Sprintf("{\"timestamp\": \"%d\", \"metrics\": [",
  29. t.Unix()))
  30. if g.CPUUsage != nil {
  31. usage, _ := json.Marshal(g.CPUUsage)
  32. report = append(report, usage...)
  33. }
  34. if g.MemoryUsage != nil {
  35. if g.CPUUsage != nil {
  36. report = append(report, []byte(",")...)
  37. }
  38. usage, _ := json.Marshal(g.MemoryUsage)
  39. report = append(report, usage...)
  40. }
  41. report = append(report, []byte("]}")...)
  42. return report, nil
  43. }
  44. // collect all CPU stat from /proc/cpustat
  45. type CPUStat struct {
  46. totalTime float64
  47. idleTime float64
  48. totalTimeNew float64
  49. idleTimeNew float64
  50. }
  51. func (c *CPUStat) ReadInfoNew(statline string) {
  52. rawInfo := strings.Fields(statline)
  53. idleNew, _ := strconv.ParseFloat(rawInfo[4], 10)
  54. iowaitNew, _ := strconv.ParseFloat(rawInfo[5], 10)
  55. c.idleTimeNew = idleNew + iowaitNew
  56. for _, s := range rawInfo {
  57. u, _ := strconv.ParseFloat(s, 10)
  58. c.totalTimeNew += u
  59. }
  60. }
  61. func (c *CPUStat) ReadInfo(statline string) {
  62. rawInfo := strings.Fields(statline)
  63. for _, s := range rawInfo[1:] {
  64. u, _ := strconv.ParseFloat(s, 10)
  65. c.totalTime += u
  66. }
  67. idle, _ := strconv.ParseFloat(rawInfo[4], 10)
  68. iowait, _ := strconv.ParseFloat(rawInfo[5], 10)
  69. c.idleTime = idle + iowait
  70. }
  71. func (c *CPUStat) CPUUsage() float64 {
  72. deltaIdleTime := c.idleTimeNew - c.idleTime
  73. deltaTotalTime := c.totalTimeNew - c.totalTime
  74. cpuUsage := (1.0 - float64(deltaIdleTime)/float64(deltaTotalTime)) * 100
  75. return cpuUsage
  76. }
  77. func NewCPUStat(procstatline, procstatlineNew string) CPUStat {
  78. cpustat := CPUStat{}
  79. cpustat.ReadInfo(procstatline)
  80. cpustat.ReadInfoNew(procstatlineNew)
  81. return cpustat
  82. }
  83. func (c CPUStat) MarshalJSON() ([]byte, error) {
  84. jsonData := []byte(
  85. fmt.Sprintf("{\"cpu usage\": \"%.2f%%\"}", c.CPUUsage()))
  86. return jsonData, nil
  87. }