-
Notifications
You must be signed in to change notification settings - Fork 81
/
dashboard.go
283 lines (263 loc) · 8.44 KB
/
dashboard.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package observability
import (
"fmt"
"strconv"
"kubegems.io/kubegems/pkg/service/handlers"
"kubegems.io/kubegems/pkg/service/models"
"kubegems.io/kubegems/pkg/utils/prometheus"
"sigs.k8s.io/yaml"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
// ListDashboard 监控dashboard列表
// @Tags Observability
// @Summary 监控dashboard列表
// @Description 监控dashboard列表
// @Accept json
// @Produce json
// @Param environment_id path string true "环境ID"
// @Success 200 {object} handlers.ResponseStruct{Data=[]models.MonitorDashboard} "监控dashboard列表"
// @Router /v1/observability/environment/{environment_id}/monitor/dashboard [get]
// @Security JWT
func (h *ObservabilityHandler) ListDashboard(c *gin.Context) {
ret := []models.MonitorDashboard{}
if err := h.GetDB().Find(&ret, "environment_id = ?", c.Param("environment_id")).Error; err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, ret)
}
// DashboardDetail 监控dashboard详情
// @Tags Observability
// @Summary 监控dashboard详情
// @Description 监控dashboard详情
// @Accept json
// @Produce json
// @Param environment_id path string true "环境ID"
// @Param dashboard_id path uint true "dashboard id"
// @Success 200 {object} handlers.ResponseStruct{Data=models.MonitorDashboard} "监控dashboard列表"
// @Router /v1/observability/environment/{environment_id}/monitor/dashboard/{dashboard_id} [get]
// @Security JWT
func (h *ObservabilityHandler) DashboardDetail(c *gin.Context) {
ret := models.MonitorDashboard{}
if err := h.GetDB().Find(&ret, "id = ?", c.Param("dashboard_id")).Error; err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, ret)
}
// CreateDashboard 创建监控dashboad
// @Tags Observability
// @Summary 创建监控dashboad
// @Description 创建监控dashboad
// @Accept json
// @Produce json
// @Param environment_id path string true "环境ID"
// @Success 200 {object} handlers.ResponseStruct{Data=string} "resp"
// @Router /v1/observability/environment/{environment_id}/monitor/dashboard [post]
// @Security JWT
func (h *ObservabilityHandler) CreateDashboard(c *gin.Context) {
req, err := h.getDashboardReq(c)
if err != nil {
handlers.NotOK(c, err)
return
}
h.SetAuditData(c, "创建", "监控面板", req.Name)
h.SetExtraAuditData(c, models.ResEnvironment, *req.EnvironmentID)
if err := h.GetDB().Save(req).Error; err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, "ok")
}
// UpdateDashboard 更新监控dashboad
// @Tags Observability
// @Summary 更新监控dashboad
// @Description 更新监控dashboad
// @Accept json
// @Produce json
// @Param environment_id path string true "环境ID"
// @Param dashboard_id path uint true "dashboard id"
// @Param from body models.MonitorDashboard true "dashboad配置"
// @Success 200 {object} handlers.ResponseStruct{Data=string} "resp"
// @Router /v1/observability/environment/{environment_id}/monitor/dashboard/{dashboard_id} [put]
// @Security JWT
func (h *ObservabilityHandler) UpdateDashboard(c *gin.Context) {
req, err := h.getDashboardReq(c)
if err != nil {
handlers.NotOK(c, err)
return
}
h.SetAuditData(c, "更新", "监控面板", req.Name)
h.SetExtraAuditData(c, models.ResEnvironment, *req.EnvironmentID)
if err := h.GetDB().Save(req).Error; err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, "ok")
}
// DeleteDashboard 删除监控dashboad
// @Tags Observability
// @Summary 删除监控dashboad
// @Description 删除监控dashboad
// @Accept json
// @Produce json
// @Param environment_id path string true "环境ID"
// @Param dashboard_id path uint true "dashboard id"
// @Success 200 {object} handlers.ResponseStruct{Data=string} "resp"
// @Router /v1/observability/environment/{environment_id}/monitor/dashboard/{dashboard_id} [delete]
// @Security JWT
func (h *ObservabilityHandler) DeleteDashboard(c *gin.Context) {
d := models.MonitorDashboard{}
if err := h.GetDB().First(&d, "id = ?", c.Param("dashboard_id")).Error; err != nil {
handlers.NotOK(c, err)
return
}
h.SetAuditData(c, "删除", "监控面板", d.Name)
h.SetExtraAuditData(c, models.ResEnvironment, *d.EnvironmentID)
if err := h.GetDB().Delete(&d).Error; err != nil {
handlers.NotOK(c, err)
return
}
handlers.OK(c, "ok")
}
func (h *ObservabilityHandler) getDashboardReq(c *gin.Context) (*models.MonitorDashboard, error) {
req := models.MonitorDashboard{}
if err := c.BindJSON(&req); err != nil {
return nil, err
}
if req.Template != "" {
tpls := []models.MonitorDashboard{}
if err := yaml.Unmarshal(alltemplates, &tpls); err != nil {
return nil, err
}
found := false
for _, v := range tpls {
if v.Name == req.Template {
v.Name = req.Name
req = v
found = true
break
}
}
if !found {
return nil, fmt.Errorf("template %s not found", req.Template)
}
}
envid, err := strconv.Atoi(c.Param("environment_id"))
if err != nil {
return nil, errors.Wrap(err, "environment_id")
}
uintid := uint(envid)
req.EnvironmentID = &uintid
u, exist := h.GetContextUser(c)
if !exist {
return nil, fmt.Errorf("not login")
}
req.Creator = u.GetUsername()
env := models.Environment{}
if err := h.GetDB().Preload("Cluster").First(&env, "id = ?", req.EnvironmentID).Error; err != nil {
return nil, err
}
// 默认查近30m
if req.Start == "" || req.End == "" {
req.Start = "now-30m"
req.End = "now"
}
if req.Refresh == "" {
req.Refresh = "30s"
}
if req.Step == "" {
req.Step = "30s"
}
monitoropts := new(prometheus.MonitorOptions)
h.DynamicConfig.Get(c.Request.Context(), monitoropts)
// 逐个校验graph
for i, v := range req.Graphs {
if v.Name == "" {
return nil, fmt.Errorf("图表名不能为空")
}
if v.PromqlGenerator.IsEmpty() {
if v.Expr == "" {
return nil, fmt.Errorf("模板与原生promql不能同时为空")
}
if err := prometheus.CheckQueryExprNamespace(v.Expr, env.Namespace); err != nil {
return nil, err
}
if v.Unit != "" {
if _, err := prometheus.ParseUnit(v.Unit); err != nil {
return nil, err
}
}
} else {
rulectx, err := v.PromqlGenerator.FindRuleContext(monitoropts)
if err != nil {
return nil, err
}
if rulectx.ResourceDetail.Namespaced == false {
return nil, fmt.Errorf("图表: %s 错误!不能查询集群范围资源", v.Name)
}
req.Graphs[i].Unit = rulectx.RuleDetail.Unit
req.Graphs[i].PromqlGenerator.Unit = rulectx.RuleDetail.Unit
}
}
return &req, nil
}
var alltemplates = []byte(`
- name: 容器基础指标监控
step: 30s
refresh: 30s
start: now-30m
end: now
graphs:
- name: 容器CPU总量
promqlGenerator:
resource: container
rule: cpuTotal
- name: 容器CPU使用量
promqlGenerator:
resource: container
rule: cpuUsage
- name: 容器CPU使用率
promqlGenerator:
resource: container
rule: cpuUsagePercent
- name: 容器内存总量
promqlGenerator:
resource: container
rule: memoryTotal
- name: 容器内存使用量
promqlGenerator:
resource: container
rule: memoryUsage
- name: 容器内存使用率
promqlGenerator:
resource: container
rule: memoryUsagePercent
- name: 容器网络接收速率
promqlGenerator:
resource: container
rule: networkInBPS
- name: 容器网络发送速率
promqlGenerator:
resource: container
rule: networkOutBPS
- name: 存储卷监控
step: 30s
refresh: 30s
start: now-30m
end: now
graphs:
- name: 存储卷总量
promqlGenerator:
resource: pvc
rule: volumeTotal
- name: 存储卷总量
promqlGenerator:
resource: pvc
rule: volumeUsage
- name: 存储卷总量
promqlGenerator:
resource: pvc
rule: volumeUsagePercent
`)