1
0

record.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package analytic
  2. import (
  3. "runtime"
  4. "time"
  5. "github.com/shirou/gopsutil/v4/cpu"
  6. "github.com/shirou/gopsutil/v4/disk"
  7. "github.com/uozi-tech/cosy/logger"
  8. )
  9. func getTotalDiskIO() (read, write uint64) {
  10. diskIOCounters, err := disk.IOCounters()
  11. if err != nil {
  12. logger.Error(err)
  13. return
  14. }
  15. for _, v := range diskIOCounters {
  16. write += v.WriteCount
  17. read += v.ReadCount
  18. }
  19. return
  20. }
  21. func recordCpu(now time.Time) {
  22. cpuTimesBefore, err := cpu.Times(false)
  23. if err != nil {
  24. logger.Error(err)
  25. return
  26. }
  27. time.Sleep(1000 * time.Millisecond)
  28. cpuTimesAfter, err := cpu.Times(false)
  29. if err != nil {
  30. logger.Error(err)
  31. return
  32. }
  33. threadNum := runtime.GOMAXPROCS(0)
  34. cpuUserUsage := (cpuTimesAfter[0].User - cpuTimesBefore[0].User) / (float64(1000*threadNum) / 1000)
  35. cpuUserUsage *= 100
  36. cpuSystemUsage := (cpuTimesAfter[0].System - cpuTimesBefore[0].System) / (float64(1000*threadNum) / 1000)
  37. cpuSystemUsage *= 100
  38. u := Usage[float64]{
  39. Time: now,
  40. Usage: cpuUserUsage,
  41. }
  42. CpuUserRecord = append(CpuUserRecord, u)
  43. s := Usage[float64]{
  44. Time: now,
  45. Usage: cpuUserUsage + cpuSystemUsage,
  46. }
  47. CpuTotalRecord = append(CpuTotalRecord, s)
  48. if len(CpuUserRecord) > 100 {
  49. CpuUserRecord = CpuUserRecord[1:]
  50. }
  51. if len(CpuTotalRecord) > 100 {
  52. CpuTotalRecord = CpuTotalRecord[1:]
  53. }
  54. }
  55. func recordNetwork(now time.Time) {
  56. // Get separate statistics for each interface
  57. networkStats, err := GetNetworkStat()
  58. if err != nil {
  59. logger.Error(err)
  60. return
  61. }
  62. LastNetRecv = networkStats.BytesRecv
  63. LastNetSent = networkStats.BytesSent
  64. NetRecvRecord = append(NetRecvRecord, Usage[uint64]{
  65. Time: now,
  66. Usage: networkStats.BytesRecv - LastNetRecv,
  67. })
  68. NetSentRecord = append(NetSentRecord, Usage[uint64]{
  69. Time: now,
  70. Usage: networkStats.BytesSent - LastNetSent,
  71. })
  72. if len(NetRecvRecord) > 100 {
  73. NetRecvRecord = NetRecvRecord[1:]
  74. }
  75. if len(NetSentRecord) > 100 {
  76. NetSentRecord = NetSentRecord[1:]
  77. }
  78. }
  79. func recordDiskIO(now time.Time) {
  80. readCount, writeCount := getTotalDiskIO()
  81. DiskReadRecord = append(DiskReadRecord, Usage[uint64]{
  82. Time: now,
  83. Usage: readCount - LastDiskReads,
  84. })
  85. DiskWriteRecord = append(DiskWriteRecord, Usage[uint64]{
  86. Time: now,
  87. Usage: writeCount - LastDiskWrites,
  88. })
  89. if len(DiskReadRecord) > 100 {
  90. DiskReadRecord = DiskReadRecord[1:]
  91. }
  92. if len(DiskWriteRecord) > 100 {
  93. DiskWriteRecord = DiskWriteRecord[1:]
  94. }
  95. LastDiskWrites = writeCount
  96. LastDiskReads = readCount
  97. }