internal.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 stats
  15. import (
  16. "sync"
  17. "time"
  18. "github.com/fatedier/frp/utils/log"
  19. "github.com/fatedier/frp/utils/metric"
  20. )
  21. type internalCollector struct {
  22. enable bool
  23. info *ServerStatistics
  24. mu sync.Mutex
  25. }
  26. func NewInternalCollector(enable bool) Collector {
  27. return &internalCollector{
  28. enable: enable,
  29. info: &ServerStatistics{
  30. TotalTrafficIn: metric.NewDateCounter(ReserveDays),
  31. TotalTrafficOut: metric.NewDateCounter(ReserveDays),
  32. CurConns: metric.NewCounter(),
  33. ClientCounts: metric.NewCounter(),
  34. ProxyTypeCounts: make(map[string]metric.Counter),
  35. ProxyStatistics: make(map[string]*ProxyStatistics),
  36. },
  37. }
  38. }
  39. func (collector *internalCollector) Run() error {
  40. go func() {
  41. for {
  42. time.Sleep(12 * time.Hour)
  43. log.Debug("start to clear useless proxy statistics data...")
  44. collector.ClearUselessInfo()
  45. log.Debug("finish to clear useless proxy statistics data")
  46. }
  47. }()
  48. return nil
  49. }
  50. func (collector *internalCollector) ClearUselessInfo() {
  51. // To check if there are proxies that closed than 7 days and drop them.
  52. collector.mu.Lock()
  53. defer collector.mu.Unlock()
  54. for name, data := range collector.info.ProxyStatistics {
  55. if !data.LastCloseTime.IsZero() && time.Since(data.LastCloseTime) > time.Duration(7*24)*time.Hour {
  56. delete(collector.info.ProxyStatistics, name)
  57. log.Trace("clear proxy [%s]'s statistics data, lastCloseTime: [%s]", name, data.LastCloseTime.String())
  58. }
  59. }
  60. }
  61. func (collector *internalCollector) Mark(statsType StatsType, payload interface{}) {
  62. if !collector.enable {
  63. return
  64. }
  65. switch v := payload.(type) {
  66. case *NewClientPayload:
  67. collector.newClient(v)
  68. case *CloseClientPayload:
  69. collector.closeClient(v)
  70. case *OpenConnectionPayload:
  71. collector.openConnection(v)
  72. case *CloseConnectionPayload:
  73. collector.closeConnection(v)
  74. case *AddTrafficInPayload:
  75. collector.addTrafficIn(v)
  76. case *AddTrafficOutPayload:
  77. collector.addTrafficOut(v)
  78. }
  79. }
  80. func (collector *internalCollector) newClient(payload *NewClientPayload) {
  81. collector.info.ClientCounts.Inc(1)
  82. }
  83. func (collector *internalCollector) closeClient(payload *CloseClientPayload) {
  84. collector.info.ClientCounts.Dec(1)
  85. }
  86. func (collector *internalCollector) newProxy(payload *NewProxyPayload) {
  87. collector.mu.Lock()
  88. defer collector.mu.Unlock()
  89. counter, ok := collector.info.ProxyTypeCounts[payload.ProxyType]
  90. if !ok {
  91. counter = metric.NewCounter()
  92. }
  93. counter.Inc(1)
  94. collector.info.ProxyTypeCounts[payload.ProxyType] = counter
  95. proxyStats, ok := collector.info.ProxyStatistics[payload.Name]
  96. if !(ok && proxyStats.ProxyType == payload.ProxyType) {
  97. proxyStats = &ProxyStatistics{
  98. Name: payload.Name,
  99. ProxyType: payload.ProxyType,
  100. CurConns: metric.NewCounter(),
  101. TrafficIn: metric.NewDateCounter(ReserveDays),
  102. TrafficOut: metric.NewDateCounter(ReserveDays),
  103. }
  104. collector.info.ProxyStatistics[payload.Name] = proxyStats
  105. }
  106. proxyStats.LastStartTime = time.Now()
  107. }
  108. func (collector *internalCollector) closeProxy(payload *CloseProxyPayload) {
  109. collector.mu.Lock()
  110. defer collector.mu.Unlock()
  111. if counter, ok := collector.info.ProxyTypeCounts[payload.ProxyType]; ok {
  112. counter.Dec(1)
  113. }
  114. if proxyStats, ok := collector.info.ProxyStatistics[payload.Name]; ok {
  115. proxyStats.LastCloseTime = time.Now()
  116. }
  117. }
  118. func (collector *internalCollector) openConnection(payload *OpenConnectionPayload) {
  119. collector.info.CurConns.Inc(1)
  120. collector.mu.Lock()
  121. defer collector.mu.Unlock()
  122. proxyStats, ok := collector.info.ProxyStatistics[payload.ProxyName]
  123. if ok {
  124. proxyStats.CurConns.Inc(1)
  125. collector.info.ProxyStatistics[payload.ProxyName] = proxyStats
  126. }
  127. }
  128. func (collector *internalCollector) closeConnection(payload *CloseConnectionPayload) {
  129. collector.info.CurConns.Dec(1)
  130. collector.mu.Lock()
  131. defer collector.mu.Unlock()
  132. proxyStats, ok := collector.info.ProxyStatistics[payload.ProxyName]
  133. if ok {
  134. proxyStats.CurConns.Dec(1)
  135. collector.info.ProxyStatistics[payload.ProxyName] = proxyStats
  136. }
  137. }
  138. func (collector *internalCollector) addTrafficIn(payload *AddTrafficInPayload) {
  139. collector.info.TotalTrafficIn.Inc(payload.TrafficBytes)
  140. collector.mu.Lock()
  141. defer collector.mu.Unlock()
  142. proxyStats, ok := collector.info.ProxyStatistics[payload.ProxyName]
  143. if ok {
  144. proxyStats.TrafficIn.Inc(payload.TrafficBytes)
  145. collector.info.ProxyStatistics[payload.ProxyName] = proxyStats
  146. }
  147. }
  148. func (collector *internalCollector) addTrafficOut(payload *AddTrafficOutPayload) {
  149. collector.info.TotalTrafficOut.Inc(payload.TrafficBytes)
  150. collector.mu.Lock()
  151. defer collector.mu.Unlock()
  152. proxyStats, ok := collector.info.ProxyStatistics[payload.ProxyName]
  153. if ok {
  154. proxyStats.TrafficOut.Inc(payload.TrafficBytes)
  155. collector.info.ProxyStatistics[payload.ProxyName] = proxyStats
  156. }
  157. }
  158. func (collector *internalCollector) GetServer() *ServerStats {
  159. collector.mu.Lock()
  160. defer collector.mu.Unlock()
  161. s := &ServerStats{
  162. TotalTrafficIn: collector.info.TotalTrafficIn.TodayCount(),
  163. TotalTrafficOut: collector.info.TotalTrafficOut.TodayCount(),
  164. CurConns: collector.info.CurConns.Count(),
  165. ClientCounts: collector.info.ClientCounts.Count(),
  166. ProxyTypeCounts: make(map[string]int64),
  167. }
  168. for k, v := range collector.info.ProxyTypeCounts {
  169. s.ProxyTypeCounts[k] = v.Count()
  170. }
  171. return s
  172. }
  173. func (collector *internalCollector) GetProxiesByType(proxyType string) []*ProxyStats {
  174. res := make([]*ProxyStats, 0)
  175. collector.mu.Lock()
  176. defer collector.mu.Unlock()
  177. for name, proxyStats := range collector.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: 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 (collector *internalCollector) GetProxiesByTypeAndName(proxyType string, proxyName string) (res *ProxyStats) {
  199. collector.mu.Lock()
  200. defer collector.mu.Unlock()
  201. for name, proxyStats := range collector.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: 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 (collector *internalCollector) GetProxyTraffic(name string) (res *ProxyTrafficInfo) {
  226. collector.mu.Lock()
  227. defer collector.mu.Unlock()
  228. proxyStats, ok := collector.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. }