| // |
| 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 |
| 15 | package mem |
| 16 |
| 17 | import ( |
| 18 | "sync" |
| 19 | "time" |
| 20 |
| 21 | "k8s.io/utils/clock" |
| 22 |
| 23 | "github.com/fatedier/frp/pkg/util/log" |
| 24 | "github.com/fatedier/frp/pkg/util/metric" |
| 25 | server "github.com/fatedier/frp/server/metrics" |
| 26 | ) |
| 27 |
| 28 | var ( |
| 29 | sm = newServerMetrics() |
| 30 |
| 31 | ServerMetrics server.ServerMetrics |
| 32 | StatsCollector Collector |
| 33 | ) |
| 34 |
| 35 | func init() { |
| 36 | ServerMetrics = sm |
| 37 | StatsCollector = sm |
| 38 | sm.run() |
| 39 | } |
| 40 |
| 41 | type serverMetrics struct { |
| 42 | info *ServerStatistics |
| 43 | clock clock.WithTicker |
| 44 | mu sync.Mutex |
| 45 | } |
| 46 |
| 47 | func newServerMetrics() *serverMetrics { |
| 48 | return newServerMetricsWithClock(clock.RealClock{}) |
| 49 | } |
| 50 |
| 51 | func newServerMetricsWithClock(clk clock.WithTicker) *serverMetrics { |
| 52 | if clk == nil { |
| 53 | clk = clock.RealClock{} |
| 54 | } |
| 55 | return &serverMetrics{ |
| 56 | clock: clk, |
| 57 | info: &ServerStatistics{ |
| 58 | TotalTrafficIn: metric.NewDateCounter(ReserveDays), |
| 59 | TotalTrafficOut: metric.NewDateCounter(ReserveDays), |
| 60 | CurConns: metric.NewCounter(), |
| 61 |
| 62 | ClientCounts: metric.NewCounter(), |
| 63 | ProxyTypeCounts: make(map[string]metric.Counter), |
| 64 |
| 65 | ProxyStatistics: make(map[string]*ProxyStatistics), |
| 66 | }, |
| 67 | } |
| 68 | } |
| 69 |
| 70 | func (m *serverMetrics) run() { |
| 71 | go m.runUntil(nil) |
| 72 | } |
| 73 |
| 74 | func (m *serverMetrics) runUntil(stopCh <-chan struct{}) { |
| 75 | ticker := m.clock.NewTicker(12 * time.Hour) |
| 76 | defer ticker.Stop() |
| 77 |
| 78 | for { |
| 79 | select { |
| 80 | case <-ticker.C(): |
| 81 | start := m.clock.Now() |
| 82 | count, total := m.clearUselessInfo(time.Duration(7*24) * time.Hour) |
| 83 | log.Debugf("clear useless proxy statistics data count %d/%d, cost %v", count, total, m.clock.Since(start)) |
| 84 | case <-stopCh: |
| 85 | return |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 |
| 90 | func (m *serverMetrics) clearUselessInfo(continuousOfflineDuration time.Duration) (int, int) { |
| 91 | count := 0 |
| 92 | total := 0 |
| 93 | // To check if there are any proxies that have been closed for more than continuousOfflineDuration and remove them. |
| 94 | m.mu.Lock() |
| 95 | defer m.mu.Unlock() |
| 96 | total = len(m.info.ProxyStatistics) |
| 97 | for name, data := range m.info.ProxyStatistics { |
| 98 | if m.shouldClearProxyStats(data, continuousOfflineDuration) { |
| 99 | delete(m.info.ProxyStatistics, name) |
| 100 | count++ |
| 101 | log.Tracef("clear proxy [%s]'s statistics data, lastCloseTime: [%s]", name, data.LastCloseTime.String()) |
| 102 | } |
| 103 | } |
| 104 | return count, total |
| 105 | } |
| 106 |
| 107 | func (m *serverMetrics) shouldClearProxyStats(data *ProxyStatistics, continuousOfflineDuration time.Duration) bool { |
| 108 | return !data.LastCloseTime.IsZero() && |
| 109 | data.LastStartTime.Before(data.LastCloseTime) && |
| 110 | m.clock.Since(data.LastCloseTime) > continuousOfflineDuration |
| 111 | } |
| 112 |
| 113 | func (m *serverMetrics) ClearOfflineProxies() (int, int) { |
| 114 | return m.clearUselessInfo(0) |
| 115 | } |
| 116 |
| 117 | func (m *serverMetrics) PruneOfflineProxies() (int, int) { |
| 118 | return m.clearUselessInfo(0) |
| 119 | } |
| 120 |
| 121 | func (m *serverMetrics) NewClient() { |
| 122 | m.info.ClientCounts.Inc(1) |
| 123 | } |
| 124 |
| 125 | func (m *serverMetrics) CloseClient() { |
| 126 | m.info.ClientCounts.Dec(1) |
| 127 | } |
| 128 |
| 129 | func (m *serverMetrics) NewProxy(name string, proxyType string, user string, clientID string) { |
| 130 | m.mu.Lock() |
| 131 | defer m.mu.Unlock() |
| 132 | counter, ok := m.info.ProxyTypeCounts[proxyType] |
| 133 | if !ok { |
| 134 | counter = metric.NewCounter() |
| 135 | } |
| 136 | counter.Inc(1) |
| 137 | m.info.ProxyTypeCounts[proxyType] = counter |
| 138 |
| 139 | proxyStats, ok := m.info.ProxyStatistics[name] |
| 140 | if !ok || proxyStats.ProxyType != proxyType { |
| 141 | proxyStats = &ProxyStatistics{ |
| 142 | Name: name, |
| 143 | ProxyType: proxyType, |
| 144 | CurConns: metric.NewCounter(), |
| 145 | TrafficIn: metric.NewDateCounter(ReserveDays), |
| 146 | TrafficOut: metric.NewDateCounter(ReserveDays), |
| 147 | } |
| 148 | m.info.ProxyStatistics[name] = proxyStats |
| 149 | } |
| 150 | proxyStats.User = user |
| 151 | proxyStats.ClientID = clientID |
| 152 | proxyStats.LastStartTime = m.clock.Now() |
| 153 | } |
| 154 |
| 155 | func (m *serverMetrics) CloseProxy(name string, proxyType string) { |
| 156 | m.mu.Lock() |
| 157 | defer m.mu.Unlock() |
| 158 | if counter, ok := m.info.ProxyTypeCounts[proxyType]; ok { |
| 159 | counter.Dec(1) |
| 160 | } |
| 161 | if proxyStats, ok := m.info.ProxyStatistics[name]; ok { |
| 162 | proxyStats.LastCloseTime = m.clock.Now() |
| 163 | } |
| 164 | } |
| 165 |
| 166 | func (m *serverMetrics) OpenConnection(name string, _ string) { |
| 167 | m.info.CurConns.Inc(1) |
| 168 |
| 169 | m.mu.Lock() |
| 170 | defer m.mu.Unlock() |
| 171 | proxyStats, ok := m.info.ProxyStatistics[name] |
| 172 | if ok { |
| 173 | proxyStats.CurConns.Inc(1) |
| 174 | } |
| 175 | } |
| 176 |
| 177 | func (m *serverMetrics) CloseConnection(name string, _ string) { |
| 178 | m.info.CurConns.Dec(1) |
| 179 |
| 180 | m.mu.Lock() |
| 181 | defer m.mu.Unlock() |
| 182 | proxyStats, ok := m.info.ProxyStatistics[name] |
| 183 | if ok { |
| 184 | proxyStats.CurConns.Dec(1) |
| 185 | } |
| 186 | } |
| 187 |
| 188 | func (m *serverMetrics) AddTrafficIn(name string, _ string, trafficBytes int64) { |
| 189 | m.info.TotalTrafficIn.Inc(trafficBytes) |
| 190 |
| 191 | m.mu.Lock() |
| 192 | defer m.mu.Unlock() |
| 193 |
| 194 | proxyStats, ok := m.info.ProxyStatistics[name] |
| 195 | if ok { |
| 196 | proxyStats.TrafficIn.Inc(trafficBytes) |
| 197 | } |
| 198 | } |
| 199 |
| 200 | func (m *serverMetrics) AddTrafficOut(name string, _ string, trafficBytes int64) { |
| 201 | m.info.TotalTrafficOut.Inc(trafficBytes) |
| 202 |
| 203 | m.mu.Lock() |
| 204 | defer m.mu.Unlock() |
| 205 |
| 206 | proxyStats, ok := m.info.ProxyStatistics[name] |
| 207 | if ok { |
| 208 | proxyStats.TrafficOut.Inc(trafficBytes) |
| 209 | } |
| 210 | } |
| 211 |
| 212 | // Get stats data api. |
| 213 |
| 214 | func (m *serverMetrics) GetServer() *ServerStats { |
| 215 | m.mu.Lock() |
| 216 | defer m.mu.Unlock() |
| 217 | s := &ServerStats{ |
| 218 | TotalTrafficIn: m.info.TotalTrafficIn.TodayCount(), |
| 219 | TotalTrafficOut: m.info.TotalTrafficOut.TodayCount(), |
| 220 | CurConns: int64(m.info.CurConns.Count()), |
| 221 | ClientCounts: int64(m.info.ClientCounts.Count()), |
| 222 | ProxyTypeCounts: make(map[string]int64), |
| 223 | } |
| 224 | for k, v := range m.info.ProxyTypeCounts { |
| 225 | s.ProxyTypeCounts[k] = int64(v.Count()) |
| 226 | } |
| 227 | return s |
| 228 | } |
| 229 |
| 230 | func toProxyStats(name string, proxyStats *ProxyStatistics) *ProxyStats { |
| 231 | ps := &ProxyStats{ |
| 232 | Name: name, |
| 233 | Type: proxyStats.ProxyType, |
| 234 | User: proxyStats.User, |
| 235 | ClientID: proxyStats.ClientID, |
| 236 | TodayTrafficIn: proxyStats.TrafficIn.TodayCount(), |
| 237 | TodayTrafficOut: proxyStats.TrafficOut.TodayCount(), |
| 238 | CurConns: int64(proxyStats.CurConns.Count()), |
| 239 | } |
| 240 | if !proxyStats.LastStartTime.IsZero() { |
| 241 | ps.LastStartTime = proxyStats.LastStartTime.Format("01-02 15:04:05") |
| 242 | ps.LastStartAt = proxyStats.LastStartTime.Unix() |
| 243 | } |
| 244 | if !proxyStats.LastCloseTime.IsZero() { |
| 245 | ps.LastCloseTime = proxyStats.LastCloseTime.Format("01-02 15:04:05") |
| 246 | ps.LastCloseAt = proxyStats.LastCloseTime.Unix() |
| 247 | } |
| 248 | return ps |
| 249 | } |
| 250 |
| 251 | func (m *serverMetrics) GetProxiesByType(proxyType string) []*ProxyStats { |
| 252 | res := make([]*ProxyStats, 0) |
| 253 | m.mu.Lock() |
| 254 | defer m.mu.Unlock() |
| 255 |
| 256 | for name, proxyStats := range m.info.ProxyStatistics { |
| 257 | if proxyStats.ProxyType != proxyType { |
| 258 | continue |
| 259 | } |
| 260 | res = append(res, toProxyStats(name, proxyStats)) |
| 261 | } |
| 262 | return res |
| 263 | } |
| 264 |
| 265 | func (m *serverMetrics) GetProxiesByTypeAndName(proxyType string, proxyName string) (res *ProxyStats) { |
| 266 | m.mu.Lock() |
| 267 | defer m.mu.Unlock() |
| 268 |
| 269 | proxyStats, ok := m.info.ProxyStatistics[proxyName] |
| 270 | if ok && proxyStats.ProxyType == proxyType { |
| 271 | res = toProxyStats(proxyName, proxyStats) |
| 272 | } |
| 273 | return |
| 274 | } |
| 275 |
| 276 | func (m *serverMetrics) GetProxyByName(proxyName string) (res *ProxyStats) { |
| 277 | m.mu.Lock() |
| 278 | defer m.mu.Unlock() |
| 279 |
| 280 | proxyStats, ok := m.info.ProxyStatistics[proxyName] |
| 281 | if ok { |
| 282 | res = toProxyStats(proxyName, proxyStats) |
| 283 | } |
| 284 | return |
| 285 | } |
| 286 |
| 287 | func (m *serverMetrics) GetProxyTraffic(name string) (res *ProxyTrafficInfo) { |
| 288 | m.mu.Lock() |
| 289 | defer m.mu.Unlock() |
| 290 |
| 291 | proxyStats, ok := m.info.ProxyStatistics[name] |
| 292 | if ok { |
| 293 | res = &ProxyTrafficInfo{ |
| 294 | Name: name, |
| 295 | } |
| 296 | res.TrafficIn = proxyStats.TrafficIn.GetLastDaysCount(ReserveDays) |
| 297 | res.TrafficOut = proxyStats.TrafficOut.GetLastDaysCount(ReserveDays) |
| 298 | } |
| 299 | return |
| 300 | } |