gupta.go 2.4 KB

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