server.go 6.8 KB

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