stats.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package stats
  15. import (
  16. "time"
  17. "github.com/fatedier/frp/utils/metric"
  18. )
  19. const (
  20. ReserveDays = 7
  21. )
  22. type StatsType int
  23. const (
  24. TypeNewClient StatsType = iota
  25. TypeCloseClient
  26. TypeNewProxy
  27. TypeCloseProxy
  28. TypeOpenConnection
  29. TypeCloseConnection
  30. TypeAddTrafficIn
  31. TypeAddTrafficOut
  32. )
  33. type ServerStats struct {
  34. TotalTrafficIn int64
  35. TotalTrafficOut int64
  36. CurConns int64
  37. ClientCounts int64
  38. ProxyTypeCounts map[string]int64
  39. }
  40. type ProxyStats struct {
  41. Name string
  42. Type string
  43. TodayTrafficIn int64
  44. TodayTrafficOut int64
  45. LastStartTime string
  46. LastCloseTime string
  47. CurConns int64
  48. }
  49. type ProxyTrafficInfo struct {
  50. Name string
  51. TrafficIn []int64
  52. TrafficOut []int64
  53. }
  54. type ProxyStatistics struct {
  55. Name string
  56. ProxyType string
  57. TrafficIn metric.DateCounter
  58. TrafficOut metric.DateCounter
  59. CurConns metric.Counter
  60. LastStartTime time.Time
  61. LastCloseTime time.Time
  62. }
  63. type ServerStatistics struct {
  64. TotalTrafficIn metric.DateCounter
  65. TotalTrafficOut metric.DateCounter
  66. CurConns metric.Counter
  67. // counter for clients
  68. ClientCounts metric.Counter
  69. // counter for proxy types
  70. ProxyTypeCounts map[string]metric.Counter
  71. // statistics for different proxies
  72. // key is proxy name
  73. ProxyStatistics map[string]*ProxyStatistics
  74. }
  75. type Collector interface {
  76. Mark(statsType StatsType, payload interface{})
  77. Run() error
  78. GetServer() *ServerStats
  79. GetProxiesByType(proxyType string) []*ProxyStats
  80. GetProxiesByTypeAndName(proxyType string, proxyName string) *ProxyStats
  81. GetProxyTraffic(name string) *ProxyTrafficInfo
  82. }
  83. type NewClientPayload struct{}
  84. type CloseClientPayload struct{}
  85. type NewProxyPayload struct {
  86. Name string
  87. ProxyType string
  88. }
  89. type CloseProxyPayload struct {
  90. Name string
  91. ProxyType string
  92. }
  93. type OpenConnectionPayload struct {
  94. ProxyName string
  95. }
  96. type CloseConnectionPayload struct {
  97. ProxyName string
  98. }
  99. type AddTrafficInPayload struct {
  100. ProxyName string
  101. TrafficBytes int64
  102. }
  103. type AddTrafficOutPayload struct {
  104. ProxyName string
  105. TrafficBytes int64
  106. }