metric.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright 2017 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 server
  15. import (
  16. "sync"
  17. "github.com/fatedier/frp/models/config"
  18. "github.com/fatedier/frp/utils/metric"
  19. )
  20. const (
  21. ReserveDays = 7
  22. )
  23. var globalStats *ServerStatistics
  24. type ServerStatistics struct {
  25. TotalTrafficIn metric.DateCounter
  26. TotalTrafficOut metric.DateCounter
  27. CurConns metric.Counter
  28. // counter for clients
  29. ClientCounts metric.Counter
  30. // counter for proxy types
  31. ProxyTypeCounts map[string]metric.Counter
  32. // statistics for different proxies
  33. // key is proxy name
  34. ProxyStatistics map[string]*ProxyStatistics
  35. mu sync.Mutex
  36. }
  37. type ProxyStatistics struct {
  38. ProxyType string
  39. TrafficIn metric.DateCounter
  40. TrafficOut metric.DateCounter
  41. CurConns metric.Counter
  42. }
  43. func init() {
  44. globalStats = &ServerStatistics{
  45. TotalTrafficIn: metric.NewDateCounter(ReserveDays),
  46. TotalTrafficOut: metric.NewDateCounter(ReserveDays),
  47. CurConns: metric.NewCounter(),
  48. ClientCounts: metric.NewCounter(),
  49. ProxyTypeCounts: make(map[string]metric.Counter),
  50. ProxyStatistics: make(map[string]*ProxyStatistics),
  51. }
  52. }
  53. func StatsNewClient() {
  54. if config.ServerCommonCfg.DashboardPort != 0 {
  55. globalStats.ClientCounts.Inc(1)
  56. }
  57. }
  58. func StatsCloseClient() {
  59. if config.ServerCommonCfg.DashboardPort != 0 {
  60. globalStats.ClientCounts.Dec(1)
  61. }
  62. }
  63. func StatsNewProxy(name string, proxyType string) {
  64. if config.ServerCommonCfg.DashboardPort != 0 {
  65. globalStats.mu.Lock()
  66. defer globalStats.mu.Unlock()
  67. counter, ok := globalStats.ProxyTypeCounts[proxyType]
  68. if !ok {
  69. counter = metric.NewCounter()
  70. }
  71. counter.Inc(1)
  72. globalStats.ProxyTypeCounts[proxyType] = counter
  73. proxyStats, ok := globalStats.ProxyStatistics[name]
  74. if !(ok && proxyStats.ProxyType == proxyType) {
  75. proxyStats = &ProxyStatistics{
  76. ProxyType: proxyType,
  77. CurConns: metric.NewCounter(),
  78. TrafficIn: metric.NewDateCounter(ReserveDays),
  79. TrafficOut: metric.NewDateCounter(ReserveDays),
  80. }
  81. globalStats.ProxyStatistics[name] = proxyStats
  82. }
  83. }
  84. }
  85. func StatsCloseProxy(proxyType string) {
  86. if config.ServerCommonCfg.DashboardPort != 0 {
  87. globalStats.mu.Lock()
  88. defer globalStats.mu.Unlock()
  89. if counter, ok := globalStats.ProxyTypeCounts[proxyType]; ok {
  90. counter.Dec(1)
  91. }
  92. }
  93. }
  94. func StatsOpenConnection(name string) {
  95. if config.ServerCommonCfg.DashboardPort != 0 {
  96. globalStats.CurConns.Inc(1)
  97. globalStats.mu.Lock()
  98. defer globalStats.mu.Unlock()
  99. proxyStats, ok := globalStats.ProxyStatistics[name]
  100. if ok {
  101. proxyStats.CurConns.Inc(1)
  102. globalStats.ProxyStatistics[name] = proxyStats
  103. }
  104. }
  105. }
  106. func StatsCloseConnection(name string) {
  107. if config.ServerCommonCfg.DashboardPort != 0 {
  108. globalStats.CurConns.Dec(1)
  109. globalStats.mu.Lock()
  110. defer globalStats.mu.Unlock()
  111. proxyStats, ok := globalStats.ProxyStatistics[name]
  112. if ok {
  113. proxyStats.CurConns.Dec(1)
  114. globalStats.ProxyStatistics[name] = proxyStats
  115. }
  116. }
  117. }
  118. func StatsAddTrafficIn(name string, trafficIn int64) {
  119. if config.ServerCommonCfg.DashboardPort != 0 {
  120. globalStats.TotalTrafficIn.Inc(trafficIn)
  121. globalStats.mu.Lock()
  122. defer globalStats.mu.Unlock()
  123. proxyStats, ok := globalStats.ProxyStatistics[name]
  124. if ok {
  125. proxyStats.TrafficIn.Inc(trafficIn)
  126. globalStats.ProxyStatistics[name] = proxyStats
  127. }
  128. }
  129. }
  130. func StatsAddTrafficOut(name string, trafficOut int64) {
  131. if config.ServerCommonCfg.DashboardPort != 0 {
  132. globalStats.TotalTrafficOut.Inc(trafficOut)
  133. globalStats.mu.Lock()
  134. defer globalStats.mu.Unlock()
  135. proxyStats, ok := globalStats.ProxyStatistics[name]
  136. if ok {
  137. proxyStats.TrafficOut.Inc(trafficOut)
  138. globalStats.ProxyStatistics[name] = proxyStats
  139. }
  140. }
  141. }
  142. // Functions for getting server stats.
  143. type ServerStats struct {
  144. TotalTrafficIn int64
  145. TotalTrafficOut int64
  146. CurConns int64
  147. ClientCounts int64
  148. ProxyTypeCounts map[string]int64
  149. }
  150. func StatsGetServer() *ServerStats {
  151. globalStats.mu.Lock()
  152. defer globalStats.mu.Unlock()
  153. s := &ServerStats{
  154. TotalTrafficIn: globalStats.TotalTrafficIn.TodayCount(),
  155. TotalTrafficOut: globalStats.TotalTrafficOut.TodayCount(),
  156. CurConns: globalStats.CurConns.Count(),
  157. ClientCounts: globalStats.ClientCounts.Count(),
  158. ProxyTypeCounts: make(map[string]int64),
  159. }
  160. for k, v := range globalStats.ProxyTypeCounts {
  161. s.ProxyTypeCounts[k] = v.Count()
  162. }
  163. return s
  164. }
  165. type ProxyStats struct {
  166. Name string
  167. Type string
  168. TodayTrafficIn int64
  169. TodayTrafficOut int64
  170. CurConns int64
  171. }
  172. func StatsGetProxiesByType(proxyType string) []*ProxyStats {
  173. res := make([]*ProxyStats, 0)
  174. globalStats.mu.Lock()
  175. defer globalStats.mu.Unlock()
  176. for name, proxyStats := range globalStats.ProxyStatistics {
  177. if proxyStats.ProxyType != proxyType {
  178. continue
  179. }
  180. ps := &ProxyStats{
  181. Name: name,
  182. Type: proxyStats.ProxyType,
  183. TodayTrafficIn: proxyStats.TrafficIn.TodayCount(),
  184. TodayTrafficOut: proxyStats.TrafficOut.TodayCount(),
  185. CurConns: proxyStats.CurConns.Count(),
  186. }
  187. res = append(res, ps)
  188. }
  189. return res
  190. }
  191. type ProxyTrafficInfo struct {
  192. Name string
  193. TrafficIn []int64
  194. TrafficOut []int64
  195. }
  196. func StatsGetProxyTraffic(name string) (res *ProxyTrafficInfo) {
  197. globalStats.mu.Lock()
  198. defer globalStats.mu.Unlock()
  199. proxyStats, ok := globalStats.ProxyStatistics[name]
  200. if ok {
  201. res = &ProxyTrafficInfo{
  202. Name: name,
  203. }
  204. res.TrafficIn = proxyStats.TrafficIn.GetLastDaysCount(ReserveDays)
  205. res.TrafficOut = proxyStats.TrafficOut.GetLastDaysCount(ReserveDays)
  206. }
  207. return
  208. }