server.go 6.7 KB

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