forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 113
/
client_alert.go
549 lines (481 loc) · 14.9 KB
/
client_alert.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package sls
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"strconv"
)
// SavedSearch ...
type SavedSearch struct {
SavedSearchName string `json:"savedsearchName"`
SearchQuery string `json:"searchQuery"`
Logstore string `json:"logstore"`
Topic string `json:"topic"`
DisplayName string `json:"displayName"`
}
type ResponseSavedSearchItem struct {
SavedSearchName string `json:"savedsearchName"`
DisplayName string `json:"displayName"`
}
const (
NotificationTypeSMS = "SMS"
NotificationTypeWebhook = "Webhook"
NotificationTypeDingTalk = "DingTalk"
NotificationTypeEmail = "Email"
NotificationTypeMessageCenter = "MessageCenter"
)
const (
CountConditionKey = "__count__"
)
type Severity int
const (
Report Severity = 2
Low Severity = 4
Medium Severity = 6
High Severity = 8
Critical Severity = 10
)
// power sql
type PowerSqlMode string
const (
PowerSqlModeAuto PowerSqlMode = "auto"
PowerSqlModeEnable PowerSqlMode = "enable"
PowerSqlModeDisable PowerSqlMode = "disable"
)
const (
JoinTypeCross = "cross_join"
JoinTypeInner = "inner_join"
JoinTypeLeft = "left_join"
JoinTypeRight = "right_join"
JoinTypeFull = "full_join"
JoinTypeLeftExclude = "left_exclude"
JoinTypeRightExclude = "right_exclude"
JoinTypeConcat = "concat"
JoinTypeNo = "no_join"
)
const (
GroupTypeNoGroup = "no_group"
GroupTypeLabelsAuto = "labels_auto"
GroupTypeCustom = "custom"
)
const (
ScheduleTypeFixedRate = "FixedRate"
ScheduleTypeHourly = "Hourly"
ScheduleTypeDaily = "Daily"
ScheduleTypeWeekly = "Weekly"
ScheduleTypeCron = "Cron"
ScheduleTypeDayRun = "DryRun"
ScheduleTypeResident = "Resident"
)
const (
StoreTypeLog = "log"
StoreTypeMetric = "metric"
StoreTypeMeta = "meta"
)
// SeverityConfiguration severity config by group
type SeverityConfiguration struct {
Severity Severity `json:"severity"`
EvalCondition ConditionConfiguration `json:"evalCondition"`
}
type ConditionConfiguration struct {
Condition string `json:"condition"`
CountCondition string `json:"countCondition"`
}
type JoinConfiguration struct {
Type string `json:"type"`
Condition string `json:"condition"`
}
type GroupConfiguration struct {
Type string `json:"type"`
Fields []string `json:"fields"`
}
type Tag struct {
Key string `json:"key"`
Value string `json:"value"`
}
type Token struct {
Name string `json:"name"`
DisplayName string `json:"display_name"`
Required bool `json:"required"`
Type string `json:"type"`
Default string `json:"default"`
Hide bool `json:"hide"`
}
type TemplateConfiguration struct {
Id string `json:"id"`
Type string `json:"type"`
Version string `json:"version"`
Lang string `json:"lang"`
Tokens map[string]string `json:"tokens"`
Annotations map[string]string `json:"annotations"`
}
type PolicyConfiguration struct {
UseDefault bool `json:"useDefault"`
RepeatInterval string `json:"repeatInterval"`
AlertPolicyId string `json:"alertPolicyId"`
ActionPolicyId string `json:"actionPolicyId"`
}
type Alert struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Description string `json:"description"`
State string `json:"state"`
Status string `json:"status"`
Configuration *AlertConfiguration `json:"configuration"`
Schedule *Schedule `json:"schedule"`
CreateTime int64 `json:"createTime,omitempty"`
LastModifiedTime int64 `json:"lastModifiedTime,omitempty"`
}
func (alert *Alert) MarshalJSON() ([]byte, error) {
body := map[string]interface{}{
"name": alert.Name,
"displayName": alert.DisplayName,
"description": alert.Description,
"state": alert.State,
"status": alert.Status,
"configuration": alert.Configuration,
"schedule": alert.Schedule,
"type": "Alert",
}
return json.Marshal(body)
}
type AlertQuery struct {
ChartTitle string `json:"chartTitle"`
LogStore string `json:"logStore"`
Query string `json:"query"`
TimeSpanType string `json:"timeSpanType"`
Start string `json:"start"`
End string `json:"end"`
StoreType string `json:"storeType"`
Project string `json:"project"`
Store string `json:"store"`
Region string `json:"region"`
RoleArn string `json:"roleArn"`
DashboardId string `json:"dashboardId"`
PowerSqlMode PowerSqlMode `json:"powerSqlMode"`
}
type Notification struct {
Type string `json:"type"`
Content string `json:"content"`
EmailList []string `json:"emailList,omitempty"`
Method string `json:"method,omitempty"`
MobileList []string `json:"mobileList,omitempty"`
ServiceUri string `json:"serviceUri,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
}
type Schedule struct {
Type string `json:"type"`
Interval string `json:"interval"`
CronExpression string `json:"cronExpression"`
Delay int32 `json:"delay"`
DayOfWeek int32 `json:"dayOfWeek"`
Hour int32 `json:"hour"`
RunImmediately bool `json:"runImmediately"`
TimeZone string `json:"timeZone,omitempty"`
}
type AlertConfiguration struct {
Condition string `json:"condition"`
MuteUntil int64 `json:"muteUntil"`
NotificationList []*Notification `json:"notificationList"`
NotifyThreshold int32 `json:"notifyThreshold"`
Throttling string `json:"throttling"`
Version string `json:"version"`
Type string `json:"type"`
TemplateConfiguration *TemplateConfiguration `json:"templateConfiguration"`
Dashboard string `json:"dashboard"`
Threshold int `json:"threshold"`
NoDataFire bool `json:"noDataFire"`
NoDataSeverity Severity `json:"noDataSeverity"`
SendResolved bool `json:"sendResolved"`
QueryList []*AlertQuery `json:"queryList"`
Annotations []*Tag `json:"annotations"`
Labels []*Tag `json:"labels"`
SeverityConfigurations []*SeverityConfiguration `json:"severityConfigurations"`
JoinConfigurations []*JoinConfiguration `json:"joinConfigurations"`
GroupConfiguration GroupConfiguration `json:"groupConfiguration"`
PolicyConfiguration PolicyConfiguration `json:"policyConfiguration"`
AutoAnnotation bool `json:"autoAnnotation"`
}
func (c *Client) CreateSavedSearch(project string, savedSearch *SavedSearch) error {
body, err := json.Marshal(savedSearch)
if err != nil {
return NewClientError(err)
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
}
uri := "/savedsearches"
r, err := c.request(project, "POST", uri, h, body)
if err != nil {
return err
}
r.Body.Close()
return nil
}
func (c *Client) UpdateSavedSearch(project string, savedSearch *SavedSearch) error {
body, err := json.Marshal(savedSearch)
if err != nil {
return NewClientError(err)
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
}
uri := "/savedsearches/" + savedSearch.SavedSearchName
r, err := c.request(project, "PUT", uri, h, body)
if err != nil {
return err
}
r.Body.Close()
return nil
}
func (c *Client) DeleteSavedSearch(project string, savedSearchName string) error {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
uri := "/savedsearches/" + savedSearchName
r, err := c.request(project, "DELETE", uri, h, nil)
if err != nil {
return err
}
r.Body.Close()
return nil
}
func (c *Client) GetSavedSearch(project string, savedSearchName string) (*SavedSearch, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
uri := "/savedsearches/" + savedSearchName
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return nil, err
}
defer r.Body.Close()
buf, _ := ioutil.ReadAll(r.Body)
savedSearch := &SavedSearch{}
if err = json.Unmarshal(buf, savedSearch); err != nil {
err = NewClientError(err)
}
return savedSearch, err
}
func (c *Client) ListSavedSearch(project string, savedSearchName string, offset, size int) (savedSearches []string, total int, count int, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
"savedsearchName": savedSearchName,
"offset": strconv.Itoa(offset),
"size": strconv.Itoa(size),
}
uri := "/savedsearches"
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return nil, 0, 0, err
}
defer r.Body.Close()
type ListSavedSearch struct {
Total int `json:"total"`
Count int `json:"count"`
Savedsearches []string `json:"savedsearches"`
}
buf, _ := ioutil.ReadAll(r.Body)
listSavedSearch := &ListSavedSearch{}
if err = json.Unmarshal(buf, listSavedSearch); err != nil {
err = NewClientError(err)
}
return listSavedSearch.Savedsearches, listSavedSearch.Total, listSavedSearch.Count, err
}
func (c *Client) ListSavedSearchV2(project string, savedSearchName string, offset, size int) (savedSearches []string, savedsearchItems []ResponseSavedSearchItem, total int, count int, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
"savedsearchName": savedSearchName,
"offset": strconv.Itoa(offset),
"size": strconv.Itoa(size),
}
uri := "/savedsearches"
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return nil, nil, 0, 0, err
}
defer r.Body.Close()
type ListSavedSearch struct {
Total int `json:"total"`
Count int `json:"count"`
Savedsearches []string `json:"savedsearches"`
SavedsearchItems []ResponseSavedSearchItem `json:"savedsearchItems"`
}
buf, _ := ioutil.ReadAll(r.Body)
listSavedSearch := &ListSavedSearch{}
if err = json.Unmarshal(buf, listSavedSearch); err != nil {
err = NewClientError(err)
}
return listSavedSearch.Savedsearches, listSavedSearch.SavedsearchItems, listSavedSearch.Total, listSavedSearch.Count, err
}
func (c *Client) CreateAlert(project string, alert *Alert) error {
body, err := json.Marshal(alert)
if err != nil {
return NewClientError(err)
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
}
uri := "/jobs"
r, err := c.request(project, "POST", uri, h, body)
if err != nil {
return err
}
r.Body.Close()
return nil
}
func (c *Client) CreateAlertString(project string, alert string) error {
body := []byte(alert)
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
}
uri := "/jobs"
r, err := c.request(project, "POST", uri, h, body)
if err != nil {
return err
}
r.Body.Close()
return nil
}
func (c *Client) UpdateAlert(project string, alert *Alert) error {
body, err := json.Marshal(alert)
if err != nil {
return NewClientError(err)
}
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
}
uri := "/jobs/" + alert.Name
r, err := c.request(project, "PUT", uri, h, body)
if err != nil {
return err
}
r.Body.Close()
return nil
}
func (c *Client) UpdateAlertString(project string, alertName, alert string) error {
body := []byte(alert)
h := map[string]string{
"x-log-bodyrawsize": fmt.Sprintf("%v", len(body)),
"Content-Type": "application/json",
}
uri := "/jobs/" + alertName
r, err := c.request(project, "PUT", uri, h, body)
if err != nil {
return err
}
r.Body.Close()
return nil
}
func (c *Client) DeleteAlert(project string, alertName string) error {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
uri := "/jobs/" + alertName
r, err := c.request(project, "DELETE", uri, h, nil)
if err != nil {
return err
}
r.Body.Close()
return nil
}
func (c *Client) DisableAlert(project string, alertName string) error {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
uri := fmt.Sprintf("/jobs/%s?action=disable", alertName)
r, err := c.request(project, "PUT", uri, h, nil)
if err != nil {
return err
}
r.Body.Close()
return nil
}
func (c *Client) EnableAlert(project string, alertName string) error {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
uri := fmt.Sprintf("/jobs/%s?action=enable", alertName)
r, err := c.request(project, "PUT", uri, h, nil)
if err != nil {
return err
}
r.Body.Close()
return nil
}
func (c *Client) GetAlert(project string, alertName string) (*Alert, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
uri := "/jobs/" + alertName
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return nil, err
}
defer r.Body.Close()
buf, _ := ioutil.ReadAll(r.Body)
alert := &Alert{}
if err = json.Unmarshal(buf, alert); err != nil {
err = NewClientError(err)
}
return alert, err
}
func (c *Client) GetAlertString(project string, alertName string) (string, error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
uri := "/jobs/" + alertName
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return "", err
}
defer r.Body.Close()
buf, _ := ioutil.ReadAll(r.Body)
return string(buf), err
}
func (c *Client) ListAlert(project, alertName, dashboard string, offset, size int) (alerts []*Alert, total int, count int, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
v := url.Values{}
v.Add("jobName", alertName)
v.Add("jobType", "Alert")
v.Add("offset", fmt.Sprintf("%d", offset))
v.Add("size", fmt.Sprintf("%d", size))
if dashboard != "" {
v.Add("resourceProvider", dashboard)
}
uri := "/jobs?" + v.Encode()
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return nil, 0, 0, err
}
defer r.Body.Close()
type AlertList struct {
Total int `json:"total"`
Count int `json:"count"`
Results []*Alert `json:"results"`
}
buf, _ := ioutil.ReadAll(r.Body)
listAlert := &AlertList{}
if err = json.Unmarshal(buf, listAlert); err != nil {
err = NewClientError(err)
}
return listAlert.Results, listAlert.Total, listAlert.Count, err
}