analytic.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package api
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/0xJacky/Nginx-UI/server/analytic"
  6. "github.com/shirou/gopsutil/v3/cpu"
  7. "github.com/shirou/gopsutil/v3/disk"
  8. "github.com/shirou/gopsutil/v3/host"
  9. "github.com/shirou/gopsutil/v3/load"
  10. "github.com/shirou/gopsutil/v3/mem"
  11. "github.com/shirou/gopsutil/v3/net"
  12. "math"
  13. "net/http"
  14. "runtime"
  15. "strconv"
  16. "time"
  17. "github.com/dustin/go-humanize"
  18. "github.com/gin-gonic/gin"
  19. "github.com/gorilla/websocket"
  20. )
  21. func Analytic(c *gin.Context) {
  22. var upGrader = websocket.Upgrader{
  23. CheckOrigin: func(r *http.Request) bool {
  24. return true
  25. },
  26. }
  27. // upgrade http to websocket
  28. ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
  29. if err != nil {
  30. return
  31. }
  32. defer ws.Close()
  33. response := make(gin.H)
  34. for {
  35. // read
  36. mt, message, err := ws.ReadMessage()
  37. if err != nil {
  38. break
  39. }
  40. for {
  41. memoryStat, err := mem.VirtualMemory()
  42. if err != nil {
  43. fmt.Println(err)
  44. return
  45. }
  46. response["memory_total"] = humanize.Bytes(memoryStat.Total)
  47. response["memory_used"] = humanize.Bytes(memoryStat.Used)
  48. response["memory_cached"] = humanize.Bytes(memoryStat.Cached)
  49. response["memory_free"] = humanize.Bytes(memoryStat.Free)
  50. response["memory_swap_used"] = humanize.Bytes(memoryStat.SwapTotal - memoryStat.SwapFree)
  51. response["memory_swap_total"] = humanize.Bytes(memoryStat.SwapTotal)
  52. response["memory_swap_cached"] = humanize.Bytes(memoryStat.SwapCached)
  53. response["memory_swap_percent"] = float64(memoryStat.SwapFree) / math.Max(float64(memoryStat.SwapTotal), 1)
  54. response["memory_pressure"], _ = strconv.ParseFloat(fmt.Sprintf("%.2f", memoryStat.UsedPercent), 64)
  55. cpuTimesBefore, _ := cpu.Times(false)
  56. time.Sleep(1000 * time.Millisecond)
  57. cpuTimesAfter, _ := cpu.Times(false)
  58. threadNum := runtime.GOMAXPROCS(0)
  59. cpuUserUsage := (cpuTimesAfter[0].User - cpuTimesBefore[0].User) / (float64(1000*threadNum) / 1000)
  60. cpuSystemUsage := (cpuTimesAfter[0].System - cpuTimesBefore[0].System) / (float64(1000*threadNum) / 1000)
  61. response["cpu_user"], _ = strconv.ParseFloat(fmt.Sprintf("%.2f",
  62. cpuUserUsage*100), 64)
  63. response["cpu_system"], _ = strconv.ParseFloat(fmt.Sprintf("%.2f",
  64. cpuSystemUsage*100), 64)
  65. response["cpu_idle"], _ = strconv.ParseFloat(fmt.Sprintf("%.2f",
  66. (1-cpuUserUsage+cpuSystemUsage)*100), 64)
  67. response["uptime"], _ = host.Uptime()
  68. response["loadavg"], _ = load.Avg()
  69. diskUsage, _ := disk.Usage(".")
  70. response["disk_used"] = humanize.Bytes(diskUsage.Used)
  71. response["disk_total"] = humanize.Bytes(diskUsage.Total)
  72. response["disk_percentage"], _ = strconv.ParseFloat(fmt.Sprintf("%.2f", diskUsage.UsedPercent), 64)
  73. response["diskIO"] = gin.H{
  74. "writes": analytic.DiskWriteRecord[len(analytic.DiskWriteRecord)-1],
  75. "reads": analytic.DiskReadRecord[len(analytic.DiskReadRecord)-1],
  76. }
  77. network, _ := net.IOCounters(false)
  78. if len(network) > 0 {
  79. response["network"] = network[0]
  80. }
  81. m, _ := json.Marshal(response)
  82. message = m
  83. // write
  84. err = ws.WriteMessage(mt, message)
  85. if err != nil {
  86. break
  87. }
  88. time.Sleep(800 * time.Microsecond)
  89. }
  90. }
  91. }
  92. func GetAnalyticInit(c *gin.Context) {
  93. cpuInfo, _ := cpu.Info()
  94. network, _ := net.IOCounters(false)
  95. var _net net.IOCountersStat
  96. if len(network) > 0 {
  97. _net = network[0]
  98. }
  99. hostInfo, _ := host.Info()
  100. c.JSON(http.StatusOK, gin.H{
  101. "host": hostInfo,
  102. "cpu": gin.H{
  103. "info": cpuInfo,
  104. "user": analytic.CpuUserRecord,
  105. "total": analytic.CpuTotalRecord,
  106. },
  107. "network": gin.H{
  108. "init": _net,
  109. "bytesRecv": analytic.NetRecvRecord,
  110. "bytesSent": analytic.NetSentRecord,
  111. },
  112. "diskIO": gin.H{
  113. "writes": analytic.DiskWriteRecord,
  114. "reads": analytic.DiskReadRecord,
  115. },
  116. })
  117. }