server.go 6.8 KB

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