server.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright 2019 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 mem
  15. import (
  16. "sync"
  17. "time"
  18. "github.com/fatedier/frp/pkg/util/log"
  19. "github.com/fatedier/frp/pkg/util/metric"
  20. server "github.com/fatedier/frp/server/metrics"
  21. )
  22. var (
  23. sm = newServerMetrics()
  24. ServerMetrics server.ServerMetrics
  25. StatsCollector Collector
  26. )
  27. func init() {
  28. ServerMetrics = sm
  29. StatsCollector = sm
  30. sm.run()
  31. }
  32. type serverMetrics struct {
  33. info *ServerStatistics
  34. mu sync.Mutex
  35. }
  36. func newServerMetrics() *serverMetrics {
  37. return &serverMetrics{
  38. info: &ServerStatistics{
  39. TotalTrafficIn: metric.NewDateCounter(ReserveDays),
  40. TotalTrafficOut: metric.NewDateCounter(ReserveDays),
  41. CurConns: metric.NewCounter(),
  42. ClientCounts: metric.NewCounter(),
  43. ProxyTypeCounts: make(map[string]metric.Counter),
  44. ProxyStatistics: make(map[string]*ProxyStatistics),
  45. },
  46. }
  47. }
  48. func (m *serverMetrics) run() {
  49. go func() {
  50. for {
  51. time.Sleep(12 * time.Hour)
  52. start := time.Now()
  53. count, total := m.clearUselessInfo(time.Duration(7*24) * time.Hour)
  54. log.Debugf("clear useless proxy statistics data count %d/%d, cost %v", count, total, time.Since(start))
  55. }
  56. }()
  57. }
  58. func (m *serverMetrics) clearUselessInfo(continuousOfflineDuration time.Duration) (int, int) {
  59. count := 0
  60. total := 0
  61. // To check if there are any proxies that have been closed for more than continuousOfflineDuration and remove them.
  62. m.mu.Lock()
  63. defer m.mu.Unlock()
  64. total = len(m.info.ProxyStatistics)
  65. for name, data := range m.info.ProxyStatistics {
  66. if !data.LastCloseTime.IsZero() &&
  67. data.LastStartTime.Before(data.LastCloseTime) &&
  68. time.Since(data.LastCloseTime) > continuousOfflineDuration {
  69. delete(m.info.ProxyStatistics, name)
  70. count++
  71. log.Tracef("clear proxy [%s]'s statistics data, lastCloseTime: [%s]", name, data.LastCloseTime.String())
  72. }
  73. }
  74. return count, total
  75. }
  76. func (m *serverMetrics) ClearOfflineProxies() (int, int) {
  77. return m.clearUselessInfo(0)
  78. }
  79. func (m *serverMetrics) NewClient() {
  80. m.info.ClientCounts.Inc(1)
  81. }
  82. func (m *serverMetrics) CloseClient() {
  83. m.info.ClientCounts.Dec(1)
  84. }
  85. func (m *serverMetrics) NewProxy(name string, proxyType string) {
  86. m.mu.Lock()
  87. defer m.mu.Unlock()
  88. counter, ok := m.info.ProxyTypeCounts[proxyType]
  89. if !ok {
  90. counter = metric.NewCounter()
  91. }
  92. counter.Inc(1)
  93. m.info.ProxyTypeCounts[proxyType] = counter
  94. proxyStats, ok := m.info.ProxyStatistics[name]
  95. if !(ok && proxyStats.ProxyType == proxyType) {
  96. proxyStats = &ProxyStatistics{
  97. Name: name,
  98. ProxyType: proxyType,
  99. CurConns: metric.NewCounter(),
  100. TrafficIn: metric.NewDateCounter(ReserveDays),
  101. TrafficOut: metric.NewDateCounter(ReserveDays),
  102. }
  103. m.info.ProxyStatistics[name] = proxyStats
  104. }
  105. proxyStats.LastStartTime = time.Now()
  106. }
  107. func (m *serverMetrics) CloseProxy(name string, proxyType string) {
  108. m.mu.Lock()
  109. defer m.mu.Unlock()
  110. if counter, ok := m.info.ProxyTypeCounts[proxyType]; ok {
  111. counter.Dec(1)
  112. }
  113. if proxyStats, ok := m.info.ProxyStatistics[name]; ok {
  114. proxyStats.LastCloseTime = time.Now()
  115. }
  116. }
  117. func (m *serverMetrics) OpenConnection(name string, _ string) {
  118. m.info.CurConns.Inc(1)
  119. m.mu.Lock()
  120. defer m.mu.Unlock()
  121. proxyStats, ok := m.info.ProxyStatistics[name]
  122. if ok {
  123. proxyStats.CurConns.Inc(1)
  124. m.info.ProxyStatistics[name] = proxyStats
  125. }
  126. }
  127. func (m *serverMetrics) CloseConnection(name string, _ string) {
  128. m.info.CurConns.Dec(1)
  129. m.mu.Lock()
  130. defer m.mu.Unlock()
  131. proxyStats, ok := m.info.ProxyStatistics[name]
  132. if ok {
  133. proxyStats.CurConns.Dec(1)
  134. m.info.ProxyStatistics[name] = proxyStats
  135. }
  136. }
  137. func (m *serverMetrics) AddTrafficIn(name string, _ string, trafficBytes int64) {
  138. m.info.TotalTrafficIn.Inc(trafficBytes)
  139. m.mu.Lock()
  140. defer m.mu.Unlock()
  141. proxyStats, ok := m.info.ProxyStatistics[name]
  142. if ok {
  143. proxyStats.TrafficIn.Inc(trafficBytes)
  144. m.info.ProxyStatistics[name] = proxyStats
  145. }
  146. }
  147. func (m *serverMetrics) AddTrafficOut(name string, _ string, trafficBytes int64) {
  148. m.info.TotalTrafficOut.Inc(trafficBytes)
  149. m.mu.Lock()
  150. defer m.mu.Unlock()
  151. proxyStats, ok := m.info.ProxyStatistics[name]
  152. if ok {
  153. proxyStats.TrafficOut.Inc(trafficBytes)
  154. m.info.ProxyStatistics[name] = proxyStats
  155. }
  156. }
  157. // Get stats data api.
  158. func (m *serverMetrics) GetServer() *ServerStats {
  159. m.mu.Lock()
  160. defer m.mu.Unlock()
  161. s := &ServerStats{
  162. TotalTrafficIn: m.info.TotalTrafficIn.TodayCount(),
  163. TotalTrafficOut: m.info.TotalTrafficOut.TodayCount(),
  164. CurConns: int64(m.info.CurConns.Count()),
  165. ClientCounts: int64(m.info.ClientCounts.Count()),
  166. ProxyTypeCounts: make(map[string]int64),
  167. }
  168. for k, v := range m.info.ProxyTypeCounts {
  169. s.ProxyTypeCounts[k] = int64(v.Count())
  170. }
  171. return s
  172. }
  173. func (m *serverMetrics) GetProxiesByType(proxyType string) []*ProxyStats {
  174. res := make([]*ProxyStats, 0)
  175. m.mu.Lock()
  176. defer m.mu.Unlock()
  177. for name, proxyStats := range m.info.ProxyStatistics {
  178. if proxyStats.ProxyType != proxyType {
  179. continue
  180. }
  181. ps := &ProxyStats{
  182. Name: name,
  183. Type: proxyStats.ProxyType,
  184. TodayTrafficIn: proxyStats.TrafficIn.TodayCount(),
  185. TodayTrafficOut: proxyStats.TrafficOut.TodayCount(),
  186. CurConns: int64(proxyStats.CurConns.Count()),
  187. }
  188. if !proxyStats.LastStartTime.IsZero() {
  189. ps.LastStartTime = proxyStats.LastStartTime.Format("01-02 15:04:05")
  190. }
  191. if !proxyStats.LastCloseTime.IsZero() {
  192. ps.LastCloseTime = proxyStats.LastCloseTime.Format("01-02 15:04:05")
  193. }
  194. res = append(res, ps)
  195. }
  196. return res
  197. }
  198. func (m *serverMetrics) GetProxiesByTypeAndName(proxyType string, proxyName string) (res *ProxyStats) {
  199. m.mu.Lock()
  200. defer m.mu.Unlock()
  201. for name, proxyStats := range m.info.ProxyStatistics {
  202. if proxyStats.ProxyType != proxyType {
  203. continue
  204. }
  205. if name != proxyName {
  206. continue
  207. }
  208. res = &ProxyStats{
  209. Name: name,
  210. Type: proxyStats.ProxyType,
  211. TodayTrafficIn: proxyStats.TrafficIn.TodayCount(),
  212. TodayTrafficOut: proxyStats.TrafficOut.TodayCount(),
  213. CurConns: int64(proxyStats.CurConns.Count()),
  214. }
  215. if !proxyStats.LastStartTime.IsZero() {
  216. res.LastStartTime = proxyStats.LastStartTime.Format("01-02 15:04:05")
  217. }
  218. if !proxyStats.LastCloseTime.IsZero() {
  219. res.LastCloseTime = proxyStats.LastCloseTime.Format("01-02 15:04:05")
  220. }
  221. break
  222. }
  223. return
  224. }
  225. func (m *serverMetrics) GetProxyTraffic(name string) (res *ProxyTrafficInfo) {
  226. m.mu.Lock()
  227. defer m.mu.Unlock()
  228. proxyStats, ok := m.info.ProxyStatistics[name]
  229. if ok {
  230. res = &ProxyTrafficInfo{
  231. Name: name,
  232. }
  233. res.TrafficIn = proxyStats.TrafficIn.GetLastDaysCount(ReserveDays)
  234. res.TrafficOut = proxyStats.TrafficOut.GetLastDaysCount(ReserveDays)
  235. }
  236. return
  237. }