update for metric

This commit is contained in:
fatedier 2017-03-27 01:39:05 +08:00
parent 634e048d0c
commit 8b2cde3a30
5 changed files with 79 additions and 116 deletions

View File

@ -42,13 +42,14 @@ func RunDashboardServer(addr string, port int64) (err error) {
router.GET("/api/proxy/udp", apiProxyUdp)
router.GET("/api/proxy/http", apiProxyHttp)
router.GET("/api/proxy/https", apiProxyHttps)
router.GET("/api/proxy/flow/:name", apiProxyFlow)
router.GET("/api/proxy/traffic/:name", apiProxyTraffic)
// view, see dashboard_view.go
//router.GET("/favicon.ico", http.FileServer(assets.FileSystem))
// view
router.Handler("GET", "/favicon.ico", http.FileServer(assets.FileSystem))
router.Handler("GET", "/static", http.StripPrefix("/static/", http.FileServer(assets.FileSystem)))
//router.GET("/", use(viewDashboard, basicAuth))
router.Handler("GET", "/static/*filepath", http.StripPrefix("/static/", http.FileServer(assets.FileSystem)))
router.HandlerFunc("GET", "/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/static/", http.StatusMovedPermanently)
})
address := fmt.Sprintf("%s:%d", addr, port)
server := &http.Server{

View File

@ -41,8 +41,8 @@ type ServerInfoResp struct {
MaxPoolCount int64 `json:"max_pool_count"`
HeartBeatTimeout int64 `json:"heart_beat_timeout"`
TotalFlowIn int64 `json:"total_flow_in"`
TotalFlowOut int64 `json:"total_flow_out"`
TotalTrafficIn int64 `json:"total_traffic_in"`
TotalTrafficOut int64 `json:"total_traffic_out"`
CurConns int64 `json:"cur_conns"`
ClientCounts int64 `json:"client_counts"`
ProxyTypeCounts map[string]int64 `json:"proxy_type_count"`
@ -68,8 +68,8 @@ func apiServerInfo(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
MaxPoolCount: cfg.MaxPoolCount,
HeartBeatTimeout: cfg.HeartBeatTimeout,
TotalFlowIn: serverStats.TotalFlowIn,
TotalFlowOut: serverStats.TotalFlowOut,
TotalTrafficIn: serverStats.TotalTrafficIn,
TotalTrafficOut: serverStats.TotalTrafficOut,
CurConns: serverStats.CurConns,
ClientCounts: serverStats.ClientCounts,
ProxyTypeCounts: serverStats.ProxyTypeCounts,
@ -81,11 +81,12 @@ func apiServerInfo(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
// Get proxy info.
type ProxyStatsInfo struct {
Conf config.ProxyConf `json:"conf"`
TodayFlowIn int64 `json:"today_flow_in"`
TodayFlowOut int64 `json:"today_flow_out"`
CurConns int64 `json:"cur_conns"`
Status string `json:"status"`
Name string `json:"name"`
Conf config.ProxyConf `json:"conf"`
TodayTrafficIn int64 `json:"today_traffic_in"`
TodayTrafficOut int64 `json:"today_traffic_out"`
CurConns int64 `json:"cur_conns"`
Status string `json:"status"`
}
type GetProxyInfoResp struct {
@ -172,43 +173,44 @@ func getProxyStatsByType(proxyType string) (proxyInfos []*ProxyStatsInfo) {
} else {
proxyInfo.Status = consts.Offline
}
proxyInfo.TodayFlowIn = ps.TodayFlowIn
proxyInfo.TodayFlowOut = ps.TodayFlowOut
proxyInfo.TodayTrafficIn = ps.TodayTrafficIn
proxyInfo.TodayTrafficOut = ps.TodayTrafficOut
proxyInfo.CurConns = ps.CurConns
proxyInfo.Name = ps.Name
proxyInfos = append(proxyInfos, proxyInfo)
}
return
}
// api/proxy/:name/flow
type GetProxyFlowResp struct {
// api/proxy/traffic/:name
type GetProxyTrafficResp struct {
GeneralResponse
Name string `json:"name"`
FlowIn []int64 `json:"flow_in"`
FlowOut []int64 `json:"flow_out"`
Name string `json:"name"`
TrafficIn []int64 `json:"traffic_in"`
TrafficOut []int64 `json:"traffic_out"`
}
func apiProxyFlow(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
func apiProxyTraffic(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
var (
buf []byte
res GetProxyFlowResp
res GetProxyTrafficResp
)
name := params.ByName("name")
defer func() {
log.Info("Http response [/api/proxy/flow/:name]: code [%d]", res.Code)
log.Info("Http response [/api/proxy/traffic/:name]: code [%d]", res.Code)
}()
log.Info("Http request: [/api/proxy/flow/:name]")
log.Info("Http request: [/api/proxy/traffic/:name]")
res.Name = name
proxyFlowInfo := StatsGetProxyFlow(name)
if proxyFlowInfo == nil {
proxyTrafficInfo := StatsGetProxyTraffic(name)
if proxyTrafficInfo == nil {
res.Code = 1
res.Msg = "no proxy info found"
} else {
res.FlowIn = proxyFlowInfo.FlowIn
res.FlowOut = proxyFlowInfo.FlowOut
res.TrafficIn = proxyTrafficInfo.TrafficIn
res.TrafficOut = proxyTrafficInfo.TrafficOut
}
buf, _ = json.Marshal(&res)

View File

@ -1,42 +0,0 @@
// Copyright 2016 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 (
"html/template"
"net/http"
"github.com/fatedier/frp/src/assets"
"github.com/fatedier/frp/src/models/metric"
"github.com/fatedier/frp/src/utils/log"
)
func viewDashboard(w http.ResponseWriter, r *http.Request) {
metrics := metric.GetAllProxyMetrics()
dashboardTpl, err := assets.ReadFile("index.html")
if err != nil {
http.Error(w, "get dashboard template file error", http.StatusInternalServerError)
return
}
t := template.Must(template.New("index.html").Delims("<<<", ">>>").Parse(dashboardTpl))
err = t.Execute(w, metrics)
if err != nil {
log.Warn("parse template file [index.html] error: %v", err)
http.Error(w, "parse template file error", http.StatusInternalServerError)
}
}
*/

View File

@ -28,9 +28,9 @@ const (
var globalStats *ServerStatistics
type ServerStatistics struct {
TotalFlowIn metric.DateCounter
TotalFlowOut metric.DateCounter
CurConns metric.Counter
TotalTrafficIn metric.DateCounter
TotalTrafficOut metric.DateCounter
CurConns metric.Counter
ClientCounts metric.Counter
ProxyTypeCounts map[string]metric.Counter
@ -41,17 +41,17 @@ type ServerStatistics struct {
}
type ProxyStatistics struct {
ProxyType string
FlowIn metric.DateCounter
FlowOut metric.DateCounter
CurConns metric.Counter
ProxyType string
TrafficIn metric.DateCounter
TrafficOut metric.DateCounter
CurConns metric.Counter
}
func init() {
globalStats = &ServerStatistics{
TotalFlowIn: metric.NewDateCounter(ReserveDays),
TotalFlowOut: metric.NewDateCounter(ReserveDays),
CurConns: metric.NewCounter(),
TotalTrafficIn: metric.NewDateCounter(ReserveDays),
TotalTrafficOut: metric.NewDateCounter(ReserveDays),
CurConns: metric.NewCounter(),
ClientCounts: metric.NewCounter(),
ProxyTypeCounts: make(map[string]metric.Counter),
@ -86,10 +86,10 @@ func StatsNewProxy(name string, proxyType string) {
proxyStats, ok := globalStats.ProxyStatistics[name]
if !ok {
proxyStats = &ProxyStatistics{
ProxyType: proxyType,
CurConns: metric.NewCounter(),
FlowIn: metric.NewDateCounter(ReserveDays),
FlowOut: metric.NewDateCounter(ReserveDays),
ProxyType: proxyType,
CurConns: metric.NewCounter(),
TrafficIn: metric.NewDateCounter(ReserveDays),
TrafficOut: metric.NewDateCounter(ReserveDays),
}
globalStats.ProxyStatistics[name] = proxyStats
}
@ -134,31 +134,31 @@ func StatsCloseConnection(name string) {
}
}
func StatsAddFlowIn(name string, flowIn int64) {
func StatsAddTrafficIn(name string, trafficIn int64) {
if config.ServerCommonCfg.DashboardPort != 0 {
globalStats.TotalFlowIn.Inc(flowIn)
globalStats.TotalTrafficIn.Inc(trafficIn)
globalStats.mu.Lock()
defer globalStats.mu.Unlock()
proxyStats, ok := globalStats.ProxyStatistics[name]
if ok {
proxyStats.FlowIn.Inc(flowIn)
proxyStats.TrafficIn.Inc(trafficIn)
globalStats.ProxyStatistics[name] = proxyStats
}
}
}
func StatsAddFlowOut(name string, flowOut int64) {
func StatsAddTrafficOut(name string, trafficOut int64) {
if config.ServerCommonCfg.DashboardPort != 0 {
globalStats.TotalFlowOut.Inc(flowOut)
globalStats.TotalTrafficOut.Inc(trafficOut)
globalStats.mu.Lock()
defer globalStats.mu.Unlock()
proxyStats, ok := globalStats.ProxyStatistics[name]
if ok {
proxyStats.FlowOut.Inc(flowOut)
proxyStats.TrafficOut.Inc(trafficOut)
globalStats.ProxyStatistics[name] = proxyStats
}
}
@ -166,8 +166,8 @@ func StatsAddFlowOut(name string, flowOut int64) {
// Functions for getting server stats.
type ServerStats struct {
TotalFlowIn int64
TotalFlowOut int64
TotalTrafficIn int64
TotalTrafficOut int64
CurConns int64
ClientCounts int64
ProxyTypeCounts map[string]int64
@ -177,8 +177,8 @@ func StatsGetServer() *ServerStats {
globalStats.mu.Lock()
defer globalStats.mu.Unlock()
s := &ServerStats{
TotalFlowIn: globalStats.TotalFlowIn.TodayCount(),
TotalFlowOut: globalStats.TotalFlowOut.TodayCount(),
TotalTrafficIn: globalStats.TotalTrafficIn.TodayCount(),
TotalTrafficOut: globalStats.TotalTrafficOut.TodayCount(),
CurConns: globalStats.CurConns.Count(),
ClientCounts: globalStats.ClientCounts.Count(),
ProxyTypeCounts: make(map[string]int64),
@ -190,11 +190,11 @@ func StatsGetServer() *ServerStats {
}
type ProxyStats struct {
Name string
Type string
TodayFlowIn int64
TodayFlowOut int64
CurConns int64
Name string
Type string
TodayTrafficIn int64
TodayTrafficOut int64
CurConns int64
}
func StatsGetProxiesByType(proxyType string) []*ProxyStats {
@ -208,34 +208,34 @@ func StatsGetProxiesByType(proxyType string) []*ProxyStats {
}
ps := &ProxyStats{
Name: name,
Type: proxyStats.ProxyType,
TodayFlowIn: proxyStats.FlowIn.TodayCount(),
TodayFlowOut: proxyStats.FlowOut.TodayCount(),
CurConns: proxyStats.CurConns.Count(),
Name: name,
Type: proxyStats.ProxyType,
TodayTrafficIn: proxyStats.TrafficIn.TodayCount(),
TodayTrafficOut: proxyStats.TrafficOut.TodayCount(),
CurConns: proxyStats.CurConns.Count(),
}
res = append(res, ps)
}
return res
}
type ProxyFlowInfo struct {
Name string
FlowIn []int64
FlowOut []int64
type ProxyTrafficInfo struct {
Name string
TrafficIn []int64
TrafficOut []int64
}
func StatsGetProxyFlow(name string) (res *ProxyFlowInfo) {
func StatsGetProxyTraffic(name string) (res *ProxyTrafficInfo) {
globalStats.mu.Lock()
defer globalStats.mu.Unlock()
proxyStats, ok := globalStats.ProxyStatistics[name]
if ok {
res = &ProxyFlowInfo{
res = &ProxyTrafficInfo{
Name: name,
}
res.FlowIn = proxyStats.FlowIn.GetLastDaysCount(ReserveDays)
res.FlowOut = proxyStats.FlowOut.GetLastDaysCount(ReserveDays)
res.TrafficIn = proxyStats.TrafficIn.GetLastDaysCount(ReserveDays)
res.TrafficOut = proxyStats.TrafficOut.GetLastDaysCount(ReserveDays)
}
return
}

View File

@ -306,7 +306,7 @@ func (pxy *UdpProxy) Run() (err error) {
if errRet := msg.ReadMsgInto(conn, &udpMsg); errRet != nil {
pxy.Warn("read from workConn for udp error: %v", errRet)
conn.Close()
// notity proxy to start a new work connection
// notify proxy to start a new work connection
errors.PanicToError(func() {
pxy.checkCloseCh <- 1
})
@ -314,6 +314,7 @@ func (pxy *UdpProxy) Run() (err error) {
}
if errRet := errors.PanicToError(func() {
pxy.readCh <- &udpMsg
StatsAddTrafficOut(pxy.GetName(), int64(len(udpMsg.Content)))
}); errRet != nil {
pxy.Info("reader goroutine for udp work connection closed")
return
@ -332,6 +333,7 @@ func (pxy *UdpProxy) Run() (err error) {
pxy.Info("sender goroutine for udp work connection closed: %v", errRet)
return
} else {
StatsAddTrafficIn(pxy.GetName(), int64(len(udpMsg.Content)))
continue
}
case <-ctx.Done():
@ -420,7 +422,7 @@ func HandleUserTcpConnection(pxy Proxy, userConn frpNet.Conn) {
StatsOpenConnection(pxy.GetName())
inCount, outCount := tcp.Join(local, userConn)
StatsCloseConnection(pxy.GetName())
StatsAddFlowIn(pxy.GetName(), inCount)
StatsAddFlowOut(pxy.GetName(), outCount)
StatsAddTrafficIn(pxy.GetName(), inCount)
StatsAddTrafficOut(pxy.GetName(), outCount)
pxy.Debug("join connections closed")
}