2017-03-23 02:01:25 +08:00
|
|
|
// Copyright 2017 fatedier, fatedier@gmail.com
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2017-05-26 02:00:00 +08:00
|
|
|
"time"
|
2017-03-23 02:01:25 +08:00
|
|
|
|
2018-04-10 17:46:49 +08:00
|
|
|
"github.com/fatedier/frp/g"
|
2017-05-26 02:00:00 +08:00
|
|
|
"github.com/fatedier/frp/utils/log"
|
2017-03-23 02:01:25 +08:00
|
|
|
"github.com/fatedier/frp/utils/metric"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ReserveDays = 7
|
|
|
|
)
|
|
|
|
|
|
|
|
var globalStats *ServerStatistics
|
|
|
|
|
|
|
|
type ServerStatistics struct {
|
2017-03-27 01:39:05 +08:00
|
|
|
TotalTrafficIn metric.DateCounter
|
|
|
|
TotalTrafficOut metric.DateCounter
|
|
|
|
CurConns metric.Counter
|
2017-03-23 02:01:25 +08:00
|
|
|
|
2017-05-19 18:38:31 +08:00
|
|
|
// counter for clients
|
|
|
|
ClientCounts metric.Counter
|
|
|
|
|
|
|
|
// counter for proxy types
|
2017-03-23 02:01:25 +08:00
|
|
|
ProxyTypeCounts map[string]metric.Counter
|
|
|
|
|
2017-05-19 18:38:31 +08:00
|
|
|
// statistics for different proxies
|
|
|
|
// key is proxy name
|
2017-03-23 02:01:25 +08:00
|
|
|
ProxyStatistics map[string]*ProxyStatistics
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProxyStatistics struct {
|
2017-05-26 02:00:00 +08:00
|
|
|
Name string
|
|
|
|
ProxyType string
|
|
|
|
TrafficIn metric.DateCounter
|
|
|
|
TrafficOut metric.DateCounter
|
|
|
|
CurConns metric.Counter
|
|
|
|
LastStartTime time.Time
|
|
|
|
LastCloseTime time.Time
|
2017-03-23 02:01:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
globalStats = &ServerStatistics{
|
2017-03-27 01:39:05 +08:00
|
|
|
TotalTrafficIn: metric.NewDateCounter(ReserveDays),
|
|
|
|
TotalTrafficOut: metric.NewDateCounter(ReserveDays),
|
|
|
|
CurConns: metric.NewCounter(),
|
2017-03-23 02:01:25 +08:00
|
|
|
|
|
|
|
ClientCounts: metric.NewCounter(),
|
|
|
|
ProxyTypeCounts: make(map[string]metric.Counter),
|
|
|
|
|
|
|
|
ProxyStatistics: make(map[string]*ProxyStatistics),
|
|
|
|
}
|
2017-05-26 02:00:00 +08:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
time.Sleep(12 * time.Hour)
|
|
|
|
log.Debug("start to clear useless proxy statistics data...")
|
|
|
|
StatsClearUselessInfo()
|
|
|
|
log.Debug("finish to clear useless proxy statistics data")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func StatsClearUselessInfo() {
|
|
|
|
// To check if there are proxies that closed than 7 days and drop them.
|
|
|
|
globalStats.mu.Lock()
|
|
|
|
defer globalStats.mu.Unlock()
|
|
|
|
for name, data := range globalStats.ProxyStatistics {
|
|
|
|
if !data.LastCloseTime.IsZero() && time.Since(data.LastCloseTime) > time.Duration(7*24)*time.Hour {
|
|
|
|
delete(globalStats.ProxyStatistics, name)
|
|
|
|
log.Trace("clear proxy [%s]'s statistics data, lastCloseTime: [%s]", name, data.LastCloseTime.String())
|
|
|
|
}
|
|
|
|
}
|
2017-03-23 02:01:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func StatsNewClient() {
|
2018-04-10 17:46:49 +08:00
|
|
|
if g.GlbServerCfg.DashboardPort != 0 {
|
2017-03-23 02:01:25 +08:00
|
|
|
globalStats.ClientCounts.Inc(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func StatsCloseClient() {
|
2018-04-10 17:46:49 +08:00
|
|
|
if g.GlbServerCfg.DashboardPort != 0 {
|
2017-03-23 02:01:25 +08:00
|
|
|
globalStats.ClientCounts.Dec(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func StatsNewProxy(name string, proxyType string) {
|
2018-04-10 17:46:49 +08:00
|
|
|
if g.GlbServerCfg.DashboardPort != 0 {
|
2017-03-23 02:01:25 +08:00
|
|
|
globalStats.mu.Lock()
|
|
|
|
defer globalStats.mu.Unlock()
|
|
|
|
counter, ok := globalStats.ProxyTypeCounts[proxyType]
|
|
|
|
if !ok {
|
|
|
|
counter = metric.NewCounter()
|
|
|
|
}
|
|
|
|
counter.Inc(1)
|
|
|
|
globalStats.ProxyTypeCounts[proxyType] = counter
|
|
|
|
|
|
|
|
proxyStats, ok := globalStats.ProxyStatistics[name]
|
2017-05-19 18:38:31 +08:00
|
|
|
if !(ok && proxyStats.ProxyType == proxyType) {
|
2017-03-23 02:01:25 +08:00
|
|
|
proxyStats = &ProxyStatistics{
|
2017-05-31 02:21:15 +08:00
|
|
|
Name: name,
|
|
|
|
ProxyType: proxyType,
|
|
|
|
CurConns: metric.NewCounter(),
|
|
|
|
TrafficIn: metric.NewDateCounter(ReserveDays),
|
|
|
|
TrafficOut: metric.NewDateCounter(ReserveDays),
|
2017-03-23 02:01:25 +08:00
|
|
|
}
|
|
|
|
globalStats.ProxyStatistics[name] = proxyStats
|
|
|
|
}
|
2017-05-31 02:21:15 +08:00
|
|
|
proxyStats.LastStartTime = time.Now()
|
2017-03-23 02:01:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 02:00:00 +08:00
|
|
|
func StatsCloseProxy(proxyName string, proxyType string) {
|
2018-04-10 17:46:49 +08:00
|
|
|
if g.GlbServerCfg.DashboardPort != 0 {
|
2017-03-23 02:01:25 +08:00
|
|
|
globalStats.mu.Lock()
|
|
|
|
defer globalStats.mu.Unlock()
|
|
|
|
if counter, ok := globalStats.ProxyTypeCounts[proxyType]; ok {
|
|
|
|
counter.Dec(1)
|
|
|
|
}
|
2017-05-26 02:00:00 +08:00
|
|
|
if proxyStats, ok := globalStats.ProxyStatistics[proxyName]; ok {
|
|
|
|
proxyStats.LastCloseTime = time.Now()
|
|
|
|
}
|
2017-03-23 02:01:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func StatsOpenConnection(name string) {
|
2018-04-10 17:46:49 +08:00
|
|
|
if g.GlbServerCfg.DashboardPort != 0 {
|
2017-03-23 02:01:25 +08:00
|
|
|
globalStats.CurConns.Inc(1)
|
|
|
|
|
|
|
|
globalStats.mu.Lock()
|
|
|
|
defer globalStats.mu.Unlock()
|
|
|
|
proxyStats, ok := globalStats.ProxyStatistics[name]
|
|
|
|
if ok {
|
|
|
|
proxyStats.CurConns.Inc(1)
|
|
|
|
globalStats.ProxyStatistics[name] = proxyStats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func StatsCloseConnection(name string) {
|
2018-04-10 17:46:49 +08:00
|
|
|
if g.GlbServerCfg.DashboardPort != 0 {
|
2017-03-23 02:01:25 +08:00
|
|
|
globalStats.CurConns.Dec(1)
|
|
|
|
|
|
|
|
globalStats.mu.Lock()
|
|
|
|
defer globalStats.mu.Unlock()
|
|
|
|
proxyStats, ok := globalStats.ProxyStatistics[name]
|
|
|
|
if ok {
|
|
|
|
proxyStats.CurConns.Dec(1)
|
|
|
|
globalStats.ProxyStatistics[name] = proxyStats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 01:39:05 +08:00
|
|
|
func StatsAddTrafficIn(name string, trafficIn int64) {
|
2018-04-10 17:46:49 +08:00
|
|
|
if g.GlbServerCfg.DashboardPort != 0 {
|
2017-03-27 01:39:05 +08:00
|
|
|
globalStats.TotalTrafficIn.Inc(trafficIn)
|
2017-03-23 02:01:25 +08:00
|
|
|
|
|
|
|
globalStats.mu.Lock()
|
|
|
|
defer globalStats.mu.Unlock()
|
|
|
|
|
|
|
|
proxyStats, ok := globalStats.ProxyStatistics[name]
|
|
|
|
if ok {
|
2017-03-27 01:39:05 +08:00
|
|
|
proxyStats.TrafficIn.Inc(trafficIn)
|
2017-03-23 02:01:25 +08:00
|
|
|
globalStats.ProxyStatistics[name] = proxyStats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 01:39:05 +08:00
|
|
|
func StatsAddTrafficOut(name string, trafficOut int64) {
|
2018-04-10 17:46:49 +08:00
|
|
|
if g.GlbServerCfg.DashboardPort != 0 {
|
2017-03-27 01:39:05 +08:00
|
|
|
globalStats.TotalTrafficOut.Inc(trafficOut)
|
2017-03-23 02:01:25 +08:00
|
|
|
|
|
|
|
globalStats.mu.Lock()
|
|
|
|
defer globalStats.mu.Unlock()
|
|
|
|
|
|
|
|
proxyStats, ok := globalStats.ProxyStatistics[name]
|
|
|
|
if ok {
|
2017-03-27 01:39:05 +08:00
|
|
|
proxyStats.TrafficOut.Inc(trafficOut)
|
2017-03-23 02:01:25 +08:00
|
|
|
globalStats.ProxyStatistics[name] = proxyStats
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Functions for getting server stats.
|
|
|
|
type ServerStats struct {
|
2017-03-27 01:39:05 +08:00
|
|
|
TotalTrafficIn int64
|
|
|
|
TotalTrafficOut int64
|
2017-03-23 02:01:25 +08:00
|
|
|
CurConns int64
|
|
|
|
ClientCounts int64
|
|
|
|
ProxyTypeCounts map[string]int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func StatsGetServer() *ServerStats {
|
|
|
|
globalStats.mu.Lock()
|
|
|
|
defer globalStats.mu.Unlock()
|
|
|
|
s := &ServerStats{
|
2017-03-27 01:39:05 +08:00
|
|
|
TotalTrafficIn: globalStats.TotalTrafficIn.TodayCount(),
|
|
|
|
TotalTrafficOut: globalStats.TotalTrafficOut.TodayCount(),
|
2017-03-23 02:01:25 +08:00
|
|
|
CurConns: globalStats.CurConns.Count(),
|
|
|
|
ClientCounts: globalStats.ClientCounts.Count(),
|
|
|
|
ProxyTypeCounts: make(map[string]int64),
|
|
|
|
}
|
|
|
|
for k, v := range globalStats.ProxyTypeCounts {
|
|
|
|
s.ProxyTypeCounts[k] = v.Count()
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProxyStats struct {
|
2017-03-27 01:39:05 +08:00
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
TodayTrafficIn int64
|
|
|
|
TodayTrafficOut int64
|
2017-05-31 02:21:15 +08:00
|
|
|
LastStartTime string
|
|
|
|
LastCloseTime string
|
2017-03-27 01:39:05 +08:00
|
|
|
CurConns int64
|
2017-03-23 02:01:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func StatsGetProxiesByType(proxyType string) []*ProxyStats {
|
|
|
|
res := make([]*ProxyStats, 0)
|
|
|
|
globalStats.mu.Lock()
|
|
|
|
defer globalStats.mu.Unlock()
|
|
|
|
|
|
|
|
for name, proxyStats := range globalStats.ProxyStatistics {
|
|
|
|
if proxyStats.ProxyType != proxyType {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ps := &ProxyStats{
|
2017-03-27 01:39:05 +08:00
|
|
|
Name: name,
|
|
|
|
Type: proxyStats.ProxyType,
|
|
|
|
TodayTrafficIn: proxyStats.TrafficIn.TodayCount(),
|
|
|
|
TodayTrafficOut: proxyStats.TrafficOut.TodayCount(),
|
|
|
|
CurConns: proxyStats.CurConns.Count(),
|
2017-03-23 02:01:25 +08:00
|
|
|
}
|
2017-05-31 02:21:15 +08:00
|
|
|
if !proxyStats.LastStartTime.IsZero() {
|
|
|
|
ps.LastStartTime = proxyStats.LastStartTime.Format("01-02 15:04:05")
|
|
|
|
}
|
|
|
|
if !proxyStats.LastCloseTime.IsZero() {
|
|
|
|
ps.LastCloseTime = proxyStats.LastCloseTime.Format("01-02 15:04:05")
|
|
|
|
}
|
2017-03-23 02:01:25 +08:00
|
|
|
res = append(res, ps)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2017-03-27 01:39:05 +08:00
|
|
|
type ProxyTrafficInfo struct {
|
|
|
|
Name string
|
|
|
|
TrafficIn []int64
|
|
|
|
TrafficOut []int64
|
2017-03-23 02:01:25 +08:00
|
|
|
}
|
|
|
|
|
2017-03-27 01:39:05 +08:00
|
|
|
func StatsGetProxyTraffic(name string) (res *ProxyTrafficInfo) {
|
2017-03-23 02:01:25 +08:00
|
|
|
globalStats.mu.Lock()
|
|
|
|
defer globalStats.mu.Unlock()
|
|
|
|
|
|
|
|
proxyStats, ok := globalStats.ProxyStatistics[name]
|
|
|
|
if ok {
|
2017-03-27 01:39:05 +08:00
|
|
|
res = &ProxyTrafficInfo{
|
2017-03-23 02:01:25 +08:00
|
|
|
Name: name,
|
|
|
|
}
|
2017-03-27 01:39:05 +08:00
|
|
|
res.TrafficIn = proxyStats.TrafficIn.GetLastDaysCount(ReserveDays)
|
|
|
|
res.TrafficOut = proxyStats.TrafficOut.GetLastDaysCount(ReserveDays)
|
2017-03-23 02:01:25 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|