analytic.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package api
  2. import (
  3. "fmt"
  4. analytic2 "github.com/0xJacky/Nginx-UI/internal/analytic"
  5. "github.com/0xJacky/Nginx-UI/internal/logger"
  6. "github.com/shirou/gopsutil/v3/cpu"
  7. "github.com/shirou/gopsutil/v3/host"
  8. "github.com/shirou/gopsutil/v3/load"
  9. "github.com/shirou/gopsutil/v3/net"
  10. "github.com/spf13/cast"
  11. "net/http"
  12. "runtime"
  13. "time"
  14. "github.com/gin-gonic/gin"
  15. "github.com/gorilla/websocket"
  16. )
  17. type CPUStat struct {
  18. User float64 `json:"user"`
  19. System float64 `json:"system"`
  20. Idle float64 `json:"idle"`
  21. Total float64 `json:"total"`
  22. }
  23. type Stat struct {
  24. Uptime uint64 `json:"uptime"`
  25. LoadAvg *load.AvgStat `json:"loadavg"`
  26. CPU CPUStat `json:"cpu"`
  27. Memory analytic2.MemStat `json:"memory"`
  28. Disk analytic2.DiskStat `json:"disk"`
  29. Network net.IOCountersStat `json:"network"`
  30. }
  31. func Analytic(c *gin.Context) {
  32. var upGrader = websocket.Upgrader{
  33. CheckOrigin: func(r *http.Request) bool {
  34. return true
  35. },
  36. }
  37. // upgrade http to websocket
  38. ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
  39. if err != nil {
  40. logger.Error(err)
  41. return
  42. }
  43. defer ws.Close()
  44. var stat Stat
  45. for {
  46. stat.Memory, err = analytic2.GetMemoryStat()
  47. if err != nil {
  48. logger.Error(err)
  49. return
  50. }
  51. cpuTimesBefore, _ := cpu.Times(false)
  52. time.Sleep(1000 * time.Millisecond)
  53. cpuTimesAfter, _ := cpu.Times(false)
  54. threadNum := runtime.GOMAXPROCS(0)
  55. cpuUserUsage := (cpuTimesAfter[0].User - cpuTimesBefore[0].User) / (float64(1000*threadNum) / 1000)
  56. cpuSystemUsage := (cpuTimesAfter[0].System - cpuTimesBefore[0].System) / (float64(1000*threadNum) / 1000)
  57. stat.CPU = CPUStat{
  58. User: cast.ToFloat64(fmt.Sprintf("%.2f", cpuUserUsage*100)),
  59. System: cast.ToFloat64(fmt.Sprintf("%.2f", cpuSystemUsage*100)),
  60. Idle: cast.ToFloat64(fmt.Sprintf("%.2f", (1-cpuUserUsage-cpuSystemUsage)*100)),
  61. Total: cast.ToFloat64(fmt.Sprintf("%.2f", (cpuUserUsage+cpuSystemUsage)*100)),
  62. }
  63. stat.Uptime, _ = host.Uptime()
  64. stat.LoadAvg, _ = load.Avg()
  65. stat.Disk, err = analytic2.GetDiskStat()
  66. if err != nil {
  67. logger.Error(err)
  68. return
  69. }
  70. network, _ := net.IOCounters(false)
  71. if len(network) > 0 {
  72. stat.Network = network[0]
  73. }
  74. // write
  75. err = ws.WriteJSON(stat)
  76. if err != nil || websocket.IsUnexpectedCloseError(err,
  77. websocket.CloseGoingAway,
  78. websocket.CloseNoStatusReceived,
  79. websocket.CloseNormalClosure) {
  80. logger.Error(err)
  81. break
  82. }
  83. time.Sleep(800 * time.Microsecond)
  84. }
  85. }
  86. func GetAnalyticInit(c *gin.Context) {
  87. cpuInfo, _ := cpu.Info()
  88. network, _ := net.IOCounters(false)
  89. memory, err := analytic2.GetMemoryStat()
  90. if err != nil {
  91. logger.Error(err)
  92. return
  93. }
  94. diskStat, err := analytic2.GetDiskStat()
  95. if err != nil {
  96. logger.Error(err)
  97. return
  98. }
  99. var _net net.IOCountersStat
  100. if len(network) > 0 {
  101. _net = network[0]
  102. }
  103. hostInfo, _ := host.Info()
  104. switch hostInfo.Platform {
  105. case "ubuntu":
  106. hostInfo.Platform = "Ubuntu"
  107. case "centos":
  108. hostInfo.Platform = "CentOS"
  109. }
  110. loadAvg, _ := load.Avg()
  111. c.JSON(http.StatusOK, gin.H{
  112. "host": hostInfo,
  113. "cpu": gin.H{
  114. "info": cpuInfo,
  115. "user": analytic2.CpuUserRecord,
  116. "total": analytic2.CpuTotalRecord,
  117. },
  118. "network": gin.H{
  119. "init": _net,
  120. "bytesRecv": analytic2.NetRecvRecord,
  121. "bytesSent": analytic2.NetSentRecord,
  122. },
  123. "disk_io": gin.H{
  124. "writes": analytic2.DiskWriteRecord,
  125. "reads": analytic2.DiskReadRecord,
  126. },
  127. "memory": memory,
  128. "disk": diskStat,
  129. "loadavg": loadAvg,
  130. })
  131. }
  132. func GetNodeStat(c *gin.Context) {
  133. var upGrader = websocket.Upgrader{
  134. CheckOrigin: func(r *http.Request) bool {
  135. return true
  136. },
  137. }
  138. // upgrade http to websocket
  139. ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
  140. if err != nil {
  141. logger.Error(err)
  142. return
  143. }
  144. defer ws.Close()
  145. for {
  146. // write
  147. err = ws.WriteJSON(analytic2.GetNodeStat())
  148. if err != nil || websocket.IsUnexpectedCloseError(err,
  149. websocket.CloseGoingAway,
  150. websocket.CloseNoStatusReceived,
  151. websocket.CloseNormalClosure) {
  152. logger.Error(err)
  153. break
  154. }
  155. time.Sleep(10 * time.Second)
  156. }
  157. }
  158. func GetNodesAnalytic(c *gin.Context) {
  159. var upGrader = websocket.Upgrader{
  160. CheckOrigin: func(r *http.Request) bool {
  161. return true
  162. },
  163. }
  164. // upgrade http to websocket
  165. ws, err := upGrader.Upgrade(c.Writer, c.Request, nil)
  166. if err != nil {
  167. logger.Error(err)
  168. return
  169. }
  170. defer ws.Close()
  171. for {
  172. // write
  173. err = ws.WriteJSON(analytic2.NodeMap)
  174. if err != nil || websocket.IsUnexpectedCloseError(err,
  175. websocket.CloseGoingAway,
  176. websocket.CloseNoStatusReceived,
  177. websocket.CloseNormalClosure) {
  178. logger.Error(err)
  179. break
  180. }
  181. time.Sleep(10 * time.Second)
  182. }
  183. }