server.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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()
  54. log.Debug("clear useless proxy statistics data count %d/%d, cost %v", count, total, time.Since(start))
  55. }
  56. }()
  57. }
  58. func (m *serverMetrics) clearUselessInfo() (int, int) {
  59. count := 0
  60. total := 0
  61. // To check if there are proxies that closed than 7 days and drop 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) > time.Duration(7*24)*time.Hour {
  69. delete(m.info.ProxyStatistics, name)
  70. count++
  71. log.Trace("clear proxy [%s]'s statistics data, lastCloseTime: [%s]", name, data.LastCloseTime.String())
  72. }
  73. }
  74. return count, total
  75. }
  76. func (m *serverMetrics) NewClient() {
  77. m.info.ClientCounts.Inc(1)
  78. }
  79. func (m *serverMetrics) CloseClient() {
  80. m.info.ClientCounts.Dec(1)
  81. }
  82. func (m *serverMetrics) NewProxy(name string, proxyType string) {
  83. m.mu.Lock()
  84. defer m.mu.Unlock()
  85. counter, ok := m.info.ProxyTypeCounts[proxyType]
  86. if !ok {
  87. counter = metric.NewCounter()
  88. }
  89. counter.Inc(1)
  90. m.info.ProxyTypeCounts[proxyType] = counter
  91. proxyStats, ok := m.info.ProxyStatistics[name]
  92. if !(ok && proxyStats.ProxyType == proxyType) {
  93. proxyStats = &ProxyStatistics{
  94. Name: name,
  95. ProxyType: proxyType,
  96. CurConns: metric.NewCounter(),
  97. TrafficIn: metric.NewDateCounter(ReserveDays),
  98. TrafficOut: metric.NewDateCounter(ReserveDays),
  99. }
  100. m.info.ProxyStatistics[name] = proxyStats
  101. }
  102. proxyStats.LastStartTime = time.Now()
  103. }
  104. func (m *serverMetrics) CloseProxy(name string, proxyType string) {
  105. m.mu.Lock()
  106. defer m.mu.Unlock()
  107. if counter, ok := m.info.ProxyTypeCounts[proxyType]; ok {
  108. counter.Dec(1)
  109. }
  110. if proxyStats, ok := m.info.ProxyStatistics[name]; ok {
  111. proxyStats.LastCloseTime = time.Now()
  112. }
  113. }
  114. func (m *serverMetrics) OpenConnection(name string, _ string) {
  115. m.info.CurConns.Inc(1)
  116. m.mu.Lock()
  117. defer m.mu.Unlock()
  118. proxyStats, ok := m.info.ProxyStatistics[name]
  119. if ok {
  120. proxyStats.CurConns.Inc(1)
  121. m.info.ProxyStatistics[name] = proxyStats
  122. }
  123. }
  124. func (m *serverMetrics) CloseConnection(name string, _ string) {
  125. m.info.CurConns.Dec(1)
  126. m.mu.Lock()
  127. defer m.mu.Unlock()
  128. proxyStats, ok := m.info.ProxyStatistics[name]
  129. if ok {
  130. proxyStats.CurConns.Dec(1)
  131. m.info.ProxyStatistics[name] = proxyStats
  132. }
  133. }
  134. func (m *serverMetrics) AddTrafficIn(name string, _ string, trafficBytes int64) {
  135. m.info.TotalTrafficIn.Inc(trafficBytes)
  136. m.mu.Lock()
  137. defer m.mu.Unlock()
  138. proxyStats, ok := m.info.ProxyStatistics[name]
  139. if ok {
  140. proxyStats.TrafficIn.Inc(trafficBytes)
  141. m.info.ProxyStatistics[name] = proxyStats
  142. }
  143. }
  144. func (m *serverMetrics) AddTrafficOut(name string, _ string, trafficBytes int64) {
  145. m.info.TotalTrafficOut.Inc(trafficBytes)
  146. m.mu.Lock()
  147. defer m.mu.Unlock()
  148. proxyStats, ok := m.info.ProxyStatistics[name]
  149. if ok {
  150. proxyStats.TrafficOut.Inc(trafficBytes)
  151. m.info.ProxyStatistics[name] = proxyStats
  152. }
  153. }
  154. // Get stats data api.
  155. func (m *serverMetrics) GetServer() *ServerStats {
  156. m.mu.Lock()
  157. defer m.mu.Unlock()
  158. s := &ServerStats{
  159. TotalTrafficIn: m.info.TotalTrafficIn.TodayCount(),
  160. TotalTrafficOut: m.info.TotalTrafficOut.TodayCount(),
  161. CurConns: int64(m.info.CurConns.Count()),
  162. ClientCounts: int64(m.info.ClientCounts.Count()),
  163. ProxyTypeCounts: make(map[string]int64),
  164. }
  165. for k, v := range m.info.ProxyTypeCounts {
  166. s.ProxyTypeCounts[k] = int64(v.Count())
  167. }
  168. return s
  169. }
  170. func (m *serverMetrics) GetProxiesByType(proxyType string) []*ProxyStats {
  171. res := make([]*ProxyStats, 0)
  172. m.mu.Lock()
  173. defer m.mu.Unlock()
  174. for name, proxyStats := range m.info.ProxyStatistics {
  175. if proxyStats.ProxyType != proxyType {
  176. continue
  177. }
  178. ps := &ProxyStats{
  179. Name: name,
  180. Type: proxyStats.ProxyType,
  181. TodayTrafficIn: proxyStats.TrafficIn.TodayCount(),
  182. TodayTrafficOut: proxyStats.TrafficOut.TodayCount(),
  183. CurConns: int64(proxyStats.CurConns.Count()),
  184. }
  185. if !proxyStats.LastStartTime.IsZero() {
  186. ps.LastStartTime = proxyStats.LastStartTime.Format("01-02 15:04:05")
  187. }
  188. if !proxyStats.LastCloseTime.IsZero() {
  189. ps.LastCloseTime = proxyStats.LastCloseTime.Format("01-02 15:04:05")
  190. }
  191. res = append(res, ps)
  192. }
  193. return res
  194. }
  195. func (m *serverMetrics) GetProxiesByTypeAndName(proxyType string, proxyName string) (res *ProxyStats) {
  196. m.mu.Lock()
  197. defer m.mu.Unlock()
  198. for name, proxyStats := range m.info.ProxyStatistics {
  199. if proxyStats.ProxyType != proxyType {
  200. continue
  201. }
  202. if name != proxyName {
  203. continue
  204. }
  205. res = &ProxyStats{
  206. Name: name,
  207. Type: proxyStats.ProxyType,
  208. TodayTrafficIn: proxyStats.TrafficIn.TodayCount(),
  209. TodayTrafficOut: proxyStats.TrafficOut.TodayCount(),
  210. CurConns: int64(proxyStats.CurConns.Count()),
  211. }
  212. if !proxyStats.LastStartTime.IsZero() {
  213. res.LastStartTime = proxyStats.LastStartTime.Format("01-02 15:04:05")
  214. }
  215. if !proxyStats.LastCloseTime.IsZero() {
  216. res.LastCloseTime = proxyStats.LastCloseTime.Format("01-02 15:04:05")
  217. }
  218. break
  219. }
  220. return
  221. }
  222. func (m *serverMetrics) GetProxyTraffic(name string) (res *ProxyTrafficInfo) {
  223. m.mu.Lock()
  224. defer m.mu.Unlock()
  225. proxyStats, ok := m.info.ProxyStatistics[name]
  226. if ok {
  227. res = &ProxyTrafficInfo{
  228. Name: name,
  229. }
  230. res.TrafficIn = proxyStats.TrafficIn.GetLastDaysCount(ReserveDays)
  231. res.TrafficOut = proxyStats.TrafficOut.GetLastDaysCount(ReserveDays)
  232. }
  233. return
  234. }