summary.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package engine
  2. import (
  3. "encoding/json"
  4. "sync"
  5. "time"
  6. "github.com/shirou/gopsutil/v3/cpu"
  7. "github.com/shirou/gopsutil/v3/disk"
  8. "github.com/shirou/gopsutil/v3/mem"
  9. "github.com/shirou/gopsutil/v3/net"
  10. "m7s.live/engine/v4/util"
  11. )
  12. var (
  13. summary SummaryUtil
  14. lastSummary Summary
  15. children util.Map[string, *Summary]
  16. collectLock sync.RWMutex
  17. )
  18. // ServerSummary 系统摘要定义
  19. type Summary struct {
  20. Address string
  21. Memory struct {
  22. Total uint64
  23. Free uint64
  24. Used uint64
  25. Usage float64
  26. }
  27. CPUUsage float64
  28. HardDisk struct {
  29. Total uint64
  30. Free uint64
  31. Used uint64
  32. Usage float64
  33. }
  34. NetWork []NetWorkInfo
  35. Streams []StreamSummay
  36. ts time.Time //上次更新时间
  37. }
  38. // NetWorkInfo 网速信息
  39. type NetWorkInfo struct {
  40. Name string
  41. Receive uint64
  42. Sent uint64
  43. ReceiveSpeed uint64
  44. SentSpeed uint64
  45. }
  46. type SummaryUtil Summary
  47. // Report 上报数据
  48. func (s *Summary) Report(slave *Summary) {
  49. children.Set(slave.Address, slave)
  50. }
  51. func (s *SummaryUtil) MarshalJSON() ([]byte, error) {
  52. return json.Marshal(s.collect())
  53. }
  54. func (s *SummaryUtil) MarshalYAML() (any, error) {
  55. return s.collect(), nil
  56. }
  57. func (s *SummaryUtil) collect() *Summary {
  58. if collectLock.TryLock() {
  59. defer collectLock.Unlock()
  60. } else {
  61. collectLock.RLock()
  62. defer collectLock.RUnlock()
  63. return &lastSummary
  64. }
  65. dur := time.Since(s.ts)
  66. if dur < time.Second {
  67. return &lastSummary
  68. }
  69. s.ts = time.Now()
  70. v, _ := mem.VirtualMemory()
  71. d, _ := disk.Usage("/")
  72. nv, _ := net.IOCounters(true)
  73. s.Memory.Total = v.Total >> 20
  74. s.Memory.Free = v.Available >> 20
  75. s.Memory.Used = v.Used >> 20
  76. s.Memory.Usage = v.UsedPercent
  77. if cc, _ := cpu.Percent(time.Second, false); len(cc) > 0 {
  78. s.CPUUsage = cc[0]
  79. }
  80. s.HardDisk.Free = d.Free >> 30
  81. s.HardDisk.Total = d.Total >> 30
  82. s.HardDisk.Used = d.Used >> 30
  83. s.HardDisk.Usage = d.UsedPercent
  84. netWorks := []NetWorkInfo{}
  85. for i, n := range nv {
  86. info := NetWorkInfo{
  87. Name: n.Name,
  88. Receive: n.BytesRecv,
  89. Sent: n.BytesSent,
  90. }
  91. if len(lastSummary.NetWork) > i {
  92. info.ReceiveSpeed = (n.BytesRecv - lastSummary.NetWork[i].Receive) / uint64(dur.Seconds())
  93. info.SentSpeed = (n.BytesSent - lastSummary.NetWork[i].Sent) / uint64(dur.Seconds())
  94. }
  95. netWorks = append(netWorks, info)
  96. }
  97. s.NetWork = netWorks
  98. s.Streams = util.MapList(&Streams, func(name string, ss *Stream) StreamSummay {
  99. return ss.Summary()
  100. })
  101. lastSummary = Summary(*s)
  102. return &lastSummary
  103. }