/
conn.go
103 lines (93 loc) · 2.52 KB
/
conn.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
package http_handler
import (
"fmt"
"github.com/bytedance/Elkeid/server/agent_center/common/ylog"
"github.com/bytedance/Elkeid/server/agent_center/grpctrans/grpc_handler"
"github.com/gin-gonic/gin"
)
const (
ConnResetRandom = "random"
ConnResetStrict = "strict"
)
type ResetRequest struct {
IDList []string `json:"id_list"`
Type string `json:"type" binding:"required" `
Count int `json:"count"`
}
type ConnStatRsp struct {
AgentInfo map[string]interface{} `json:"agent_info"`
PluginsInfo []map[string]interface{} `json:"plugins_info"`
}
func ConnStat(c *gin.Context) {
res := grpc_handler.GlobalGRPCPool.GetList()
ylog.Debugf("ConnStat", ">>>>API ConnStat connMap len: %d", len(res))
resList := make([]ConnStatRsp, 0, len(res))
for _, v := range res {
tmp := ConnStatRsp{
AgentInfo: v.GetAgentDetail(),
PluginsInfo: v.GetPluginsList(),
}
resList = append(resList, tmp)
}
CreateResponse(c, SuccessCode, resList)
return
}
func ConnList(c *gin.Context) {
res := grpc_handler.GlobalGRPCPool.GetList()
ylog.Debugf("ConnStat", ">>>>API ConnStat connMap len: %d", len(res))
resList := make([]string, 0, len(res))
for _, v := range res {
resList = append(resList, v.AgentID)
}
CreateResponse(c, SuccessCode, resList)
return
}
func ConnCount(c *gin.Context) {
count := grpc_handler.GlobalGRPCPool.GetCount()
ylog.Debugf("ConnCount", ">>>>API ConnCount %d", count)
CreateResponse(c, SuccessCode, count)
return
}
func ConnReset(c *gin.Context) {
var param ResetRequest
err := c.BindJSON(¶m)
if err != nil {
ylog.Errorf("ConnReset", ">>>>ConnReset Parse para error : %s", err.Error())
CreateResponse(c, ParamInvalidErrorCode, err.Error())
return
}
res := gin.H{}
detail := gin.H{}
switch param.Type {
case ConnResetRandom:
conn := grpc_handler.GlobalGRPCPool.GetList()
id := 0
for id = 0; id < param.Count && id < len(conn); id++ {
agentID := conn[id].AgentID
err := grpc_handler.GlobalGRPCPool.Close(agentID)
if err != nil {
detail[agentID] = err.Error()
} else {
detail[agentID] = "ok"
}
}
res["count"] = id
res["detail"] = detail
case ConnResetStrict:
for _, v := range param.IDList {
err := grpc_handler.GlobalGRPCPool.Close(v)
if err != nil {
detail[v] = err.Error()
} else {
detail[v] = "ok"
}
}
res["count"] = len(param.IDList)
res["detail"] = detail
default:
CreateResponse(c, ParamInvalidErrorCode, fmt.Sprintf("type only support %s|%s", ConnResetStrict, ConnResetRandom))
return
}
CreateResponse(c, SuccessCode, res)
return
}