-
Notifications
You must be signed in to change notification settings - Fork 264
/
api_varz.go
117 lines (99 loc) · 3.29 KB
/
api_varz.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package server
import (
"fmt"
"net/http"
"strconv"
"time"
"github.com/WuKongIM/WuKongIM/pkg/pse"
"github.com/WuKongIM/WuKongIM/pkg/wkhttp"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
"github.com/WuKongIM/WuKongIM/pkg/wkutil"
"github.com/WuKongIM/WuKongIM/version"
)
type VarzAPI struct {
wklog.Log
s *Server
}
func NewVarzAPI(s *Server) *VarzAPI {
return &VarzAPI{
s: s,
Log: wklog.NewWKLog("VarzAPI"),
}
}
func (v *VarzAPI) Route(r *wkhttp.WKHttp) {
r.GET("/varz", v.HandleVarz) // 获取系统变量
}
func (v *VarzAPI) HandleVarz(c *wkhttp.Context) {
show := c.Query("show")
connLimit, _ := strconv.Atoi(c.Query("conn_limit"))
if connLimit == 0 {
connLimit = 20
}
varz := CreateVarz(v.s)
if show == "conn" {
resultConns := v.s.GetConnInfos(ByInMsgDesc, 0, connLimit)
connInfos := make([]*ConnInfo, 0, len(resultConns))
for _, resultConn := range resultConns {
if resultConn == nil || !resultConn.IsAuthed() {
continue
}
connInfos = append(connInfos, newConnInfo(resultConn))
}
varz.Conns = connInfos
}
c.JSON(http.StatusOK, varz)
}
func CreateVarz(s *Server) *Varz {
var rss, vss int64 // rss内存 vss虚拟内存
var pcpu float64 // cpu
pse.ProcUsage(&pcpu, &rss, &vss)
opts := s.opts
connCount := s.dispatch.engine.ConnCount()
return &Varz{
ServerID: fmt.Sprintf("%d", opts.ID),
ServerName: "WuKongIM",
Version: version.Version,
Connections: connCount,
Uptime: myUptime(time.Since(s.start)),
CPU: pcpu,
Mem: rss,
InMsgs: s.inMsgs.Load(),
OutMsgs: s.outMsgs.Load(),
InBytes: s.inBytes.Load(),
OutBytes: s.outBytes.Load(),
SlowClients: s.slowClients.Load(),
TCPAddr: opts.External.TCPAddr,
WSAddr: opts.External.WSAddr,
WSSAddr: opts.External.WSSAddr,
MonitorAddr: opts.External.MonitorAddr,
APIURL: opts.External.APIUrl,
MonitorOn: wkutil.BoolToInt(opts.Monitor.On),
Commit: version.Commit,
CommitDate: version.CommitDate,
TreeState: version.TreeState,
}
}
type Varz struct {
ServerID string `json:"server_id"` // 服务端ID
ServerName string `json:"server_name"` // 服务端名称
Version string `json:"version"` // 服务端版本
Connections int `json:"connections"` // 当前连接数量
Uptime string `json:"uptime"` // 上线时间
Mem int64 `json:"mem"` // 内存
CPU float64 `json:"cpu"` // cpu
InMsgs int64 `json:"in_msgs"` // 流入消息数量
OutMsgs int64 `json:"out_msgs"` // 流出消息数量
InBytes int64 `json:"in_bytes"` // 流入字节数量
OutBytes int64 `json:"out_bytes"` // 流出字节数量
SlowClients int64 `json:"slow_clients"` // 慢客户端数量
TCPAddr string `json:"tcp_addr"` // tcp地址
WSAddr string `json:"ws_addr"` // ws地址
WSSAddr string `json:"wss_addr"` // wss地址
MonitorAddr string `json:"monitor_addr"` // 监控地址
MonitorOn int `json:"monitor_on"` // 监控是否开启
Commit string `json:"commit"` // git commit id
CommitDate string `json:"commit_date"` // git commit date
TreeState string `json:"tree_state"` // git tree state
APIURL string `json:"api_url"` // api地址
Conns []*ConnInfo `json:"conns,omitempty"` // 连接信息
}