1
0

snmp.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package kcp
  2. import (
  3. "fmt"
  4. "sync/atomic"
  5. )
  6. // Snmp defines network statistics indicator
  7. type Snmp struct {
  8. BytesSent uint64 // bytes sent from upper level
  9. BytesReceived uint64 // bytes received to upper level
  10. MaxConn uint64 // max number of connections ever reached
  11. ActiveOpens uint64 // accumulated active open connections
  12. PassiveOpens uint64 // accumulated passive open connections
  13. CurrEstab uint64 // current number of established connections
  14. InErrs uint64 // UDP read errors reported from net.PacketConn
  15. InCsumErrors uint64 // checksum errors from CRC32
  16. KCPInErrors uint64 // packet iput errors reported from KCP
  17. InPkts uint64 // incoming packets count
  18. OutPkts uint64 // outgoing packets count
  19. InSegs uint64 // incoming KCP segments
  20. OutSegs uint64 // outgoing KCP segments
  21. InBytes uint64 // UDP bytes received
  22. OutBytes uint64 // UDP bytes sent
  23. RetransSegs uint64 // accmulated retransmited segments
  24. FastRetransSegs uint64 // accmulated fast retransmitted segments
  25. EarlyRetransSegs uint64 // accmulated early retransmitted segments
  26. LostSegs uint64 // number of segs infered as lost
  27. RepeatSegs uint64 // number of segs duplicated
  28. FECRecovered uint64 // correct packets recovered from FEC
  29. FECErrs uint64 // incorrect packets recovered from FEC
  30. FECParityShards uint64 // FEC segments received
  31. FECShortShards uint64 // number of data shards that's not enough for recovery
  32. }
  33. func newSnmp() *Snmp {
  34. return new(Snmp)
  35. }
  36. // Header returns all field names
  37. func (s *Snmp) Header() []string {
  38. return []string{
  39. "BytesSent",
  40. "BytesReceived",
  41. "MaxConn",
  42. "ActiveOpens",
  43. "PassiveOpens",
  44. "CurrEstab",
  45. "InErrs",
  46. "InCsumErrors",
  47. "KCPInErrors",
  48. "InPkts",
  49. "OutPkts",
  50. "InSegs",
  51. "OutSegs",
  52. "InBytes",
  53. "OutBytes",
  54. "RetransSegs",
  55. "FastRetransSegs",
  56. "EarlyRetransSegs",
  57. "LostSegs",
  58. "RepeatSegs",
  59. "FECParityShards",
  60. "FECErrs",
  61. "FECRecovered",
  62. "FECShortShards",
  63. }
  64. }
  65. // ToSlice returns current snmp info as slice
  66. func (s *Snmp) ToSlice() []string {
  67. snmp := s.Copy()
  68. return []string{
  69. fmt.Sprint(snmp.BytesSent),
  70. fmt.Sprint(snmp.BytesReceived),
  71. fmt.Sprint(snmp.MaxConn),
  72. fmt.Sprint(snmp.ActiveOpens),
  73. fmt.Sprint(snmp.PassiveOpens),
  74. fmt.Sprint(snmp.CurrEstab),
  75. fmt.Sprint(snmp.InErrs),
  76. fmt.Sprint(snmp.InCsumErrors),
  77. fmt.Sprint(snmp.KCPInErrors),
  78. fmt.Sprint(snmp.InPkts),
  79. fmt.Sprint(snmp.OutPkts),
  80. fmt.Sprint(snmp.InSegs),
  81. fmt.Sprint(snmp.OutSegs),
  82. fmt.Sprint(snmp.InBytes),
  83. fmt.Sprint(snmp.OutBytes),
  84. fmt.Sprint(snmp.RetransSegs),
  85. fmt.Sprint(snmp.FastRetransSegs),
  86. fmt.Sprint(snmp.EarlyRetransSegs),
  87. fmt.Sprint(snmp.LostSegs),
  88. fmt.Sprint(snmp.RepeatSegs),
  89. fmt.Sprint(snmp.FECParityShards),
  90. fmt.Sprint(snmp.FECErrs),
  91. fmt.Sprint(snmp.FECRecovered),
  92. fmt.Sprint(snmp.FECShortShards),
  93. }
  94. }
  95. // Copy make a copy of current snmp snapshot
  96. func (s *Snmp) Copy() *Snmp {
  97. d := newSnmp()
  98. d.BytesSent = atomic.LoadUint64(&s.BytesSent)
  99. d.BytesReceived = atomic.LoadUint64(&s.BytesReceived)
  100. d.MaxConn = atomic.LoadUint64(&s.MaxConn)
  101. d.ActiveOpens = atomic.LoadUint64(&s.ActiveOpens)
  102. d.PassiveOpens = atomic.LoadUint64(&s.PassiveOpens)
  103. d.CurrEstab = atomic.LoadUint64(&s.CurrEstab)
  104. d.InErrs = atomic.LoadUint64(&s.InErrs)
  105. d.InCsumErrors = atomic.LoadUint64(&s.InCsumErrors)
  106. d.KCPInErrors = atomic.LoadUint64(&s.KCPInErrors)
  107. d.InPkts = atomic.LoadUint64(&s.InPkts)
  108. d.OutPkts = atomic.LoadUint64(&s.OutPkts)
  109. d.InSegs = atomic.LoadUint64(&s.InSegs)
  110. d.OutSegs = atomic.LoadUint64(&s.OutSegs)
  111. d.InBytes = atomic.LoadUint64(&s.InBytes)
  112. d.OutBytes = atomic.LoadUint64(&s.OutBytes)
  113. d.RetransSegs = atomic.LoadUint64(&s.RetransSegs)
  114. d.FastRetransSegs = atomic.LoadUint64(&s.FastRetransSegs)
  115. d.EarlyRetransSegs = atomic.LoadUint64(&s.EarlyRetransSegs)
  116. d.LostSegs = atomic.LoadUint64(&s.LostSegs)
  117. d.RepeatSegs = atomic.LoadUint64(&s.RepeatSegs)
  118. d.FECParityShards = atomic.LoadUint64(&s.FECParityShards)
  119. d.FECErrs = atomic.LoadUint64(&s.FECErrs)
  120. d.FECRecovered = atomic.LoadUint64(&s.FECRecovered)
  121. d.FECShortShards = atomic.LoadUint64(&s.FECShortShards)
  122. return d
  123. }
  124. // Reset values to zero
  125. func (s *Snmp) Reset() {
  126. atomic.StoreUint64(&s.BytesSent, 0)
  127. atomic.StoreUint64(&s.BytesReceived, 0)
  128. atomic.StoreUint64(&s.MaxConn, 0)
  129. atomic.StoreUint64(&s.ActiveOpens, 0)
  130. atomic.StoreUint64(&s.PassiveOpens, 0)
  131. atomic.StoreUint64(&s.CurrEstab, 0)
  132. atomic.StoreUint64(&s.InErrs, 0)
  133. atomic.StoreUint64(&s.InCsumErrors, 0)
  134. atomic.StoreUint64(&s.KCPInErrors, 0)
  135. atomic.StoreUint64(&s.InPkts, 0)
  136. atomic.StoreUint64(&s.OutPkts, 0)
  137. atomic.StoreUint64(&s.InSegs, 0)
  138. atomic.StoreUint64(&s.OutSegs, 0)
  139. atomic.StoreUint64(&s.InBytes, 0)
  140. atomic.StoreUint64(&s.OutBytes, 0)
  141. atomic.StoreUint64(&s.RetransSegs, 0)
  142. atomic.StoreUint64(&s.FastRetransSegs, 0)
  143. atomic.StoreUint64(&s.EarlyRetransSegs, 0)
  144. atomic.StoreUint64(&s.LostSegs, 0)
  145. atomic.StoreUint64(&s.RepeatSegs, 0)
  146. atomic.StoreUint64(&s.FECParityShards, 0)
  147. atomic.StoreUint64(&s.FECErrs, 0)
  148. atomic.StoreUint64(&s.FECRecovered, 0)
  149. atomic.StoreUint64(&s.FECShortShards, 0)
  150. }
  151. // DefaultSnmp is the global KCP connection statistics collector
  152. var DefaultSnmp *Snmp
  153. func init() {
  154. DefaultSnmp = newSnmp()
  155. }