-
Notifications
You must be signed in to change notification settings - Fork 204
/
alarm.go
792 lines (758 loc) · 24.2 KB
/
alarm.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
package service
import (
"fmt"
"net/http"
"sort"
"strconv"
"strings"
"time"
"github.com/ego-component/egorm"
"github.com/gotomicro/ego/core/econf"
"github.com/gotomicro/ego/core/elog"
"github.com/pkg/errors"
"gorm.io/gorm"
"github.com/clickvisual/clickvisual/api/internal/invoker"
"github.com/clickvisual/clickvisual/api/internal/pkg/component/core"
db2 "github.com/clickvisual/clickvisual/api/internal/pkg/model/db"
"github.com/clickvisual/clickvisual/api/internal/pkg/model/view"
"github.com/clickvisual/clickvisual/api/internal/service/alarm/alertcomponent"
"github.com/clickvisual/clickvisual/api/internal/service/alarm/pusher"
"github.com/clickvisual/clickvisual/api/internal/service/alarm/rule"
"github.com/clickvisual/clickvisual/api/internal/service/inquiry/factory"
"github.com/clickvisual/clickvisual/api/internal/service/inquiry/factory/builder/bumo"
)
// const prometheusRuleTemplate = `groups:
// - name: default
// rules:
// - alert: ClickVisual-%s
// expr: %s
// for: %s
// labels:
// severity: warning
// annotations:
// summary: "告警 {{ $labels.name }}"
// description: "{{ $labels.desc }} (当前值: {{ $value }})"`
const prometheusRuleTemplateWithoutFor = `groups:
- name: default
rules:
- alert: ClickVisual-%s
expr: %s
labels:
severity: warning
annotations:
summary: "告警 {{ $labels.name }}"
description: "{{ $labels.desc }} (当前值: {{ $value }})"`
const (
reloadTimes = 30
reloadInterval = time.Second * 5
)
const (
NoDataOpDefault = 0
NoDataOpOK = 1
NoDataOpAlert = 2
)
var _ iAlert = (*alert)(nil)
type iAlert interface {
FilterCreate(tx *gorm.DB, alarmObj *db2.Alarm, filters []view.ReqAlarmFilterCreate) (res map[int]view.AlarmFilterItem, err error)
ConditionCreate(tx *gorm.DB, obj *db2.Alarm, conditions []view.ReqAlarmConditionCreate, filter *db2.AlarmFilter) (exp string, err error)
PrometheusReload(prometheusTarget string) (err error)
PrometheusRuleGen(obj *db2.Alarm, exp string, filterId int) string
PrometheusRuleCreateOrUpdate(instance db2.BaseInstance, groupName, ruleName, content string) (err error)
DeletePrometheusRule(instance *db2.BaseInstance, obj *db2.Alarm) (err error)
CreateOrUpdate(alarmObj *db2.Alarm, req view.ReqAlarmCreate) (err error)
OpenOperator(id int) (err error)
Update(uid, alarmId int, req view.ReqAlarmCreate) (err error)
AddPrometheusReloadChan()
IsAllClosed(instanceId int) (err error)
HandlerAlertManager(alarmUUID string, filterId string, notification db2.Notification) (err error)
}
type alert struct {
reloadChan chan int64
}
// NewAlarm ...
func NewAlarm() *alert {
a := &alert{
reloadChan: make(chan int64, reloadTimes),
}
go func() {
for r := range a.reloadChan {
elog.Info("AllPrometheusReload", elog.Int("times", len(a.reloadChan)), elog.Int64("r", r), elog.Int64("now", time.Now().Unix()))
AllPrometheusReload()
core.LoggerError("alert", "ruleReload", AlertRuleCheck())
time.Sleep(reloadInterval)
}
}()
a.reloadChan <- time.Now().Unix()
return a
}
func (i *alert) FilterCreate(tx *gorm.DB, alarmObj *db2.Alarm, filters []view.ReqAlarmFilterCreate) (res map[int]view.AlarmFilterItem, err error) {
res = make(map[int]view.AlarmFilterItem, 0)
for _, filter := range filters {
// create filter
filterObj := &db2.AlarmFilter{
AlarmId: alarmObj.ID,
Tid: filter.Tid,
When: filter.When,
SetOperatorTyp: filter.SetOperatorTyp,
SetOperatorExp: filter.SetOperatorExp,
Mode: filter.Mode,
}
if filterObj.When == "" {
filterObj.When = "1=1"
}
err = db2.AlarmFilterCreate(tx, filterObj)
if err != nil {
return
}
row := view.AlarmFilterItem{
AlarmFilter: filterObj,
}
// create condition
row.Exp, err = i.ConditionCreate(tx, alarmObj, filter.Conditions, filterObj)
if err != nil {
return
}
res[filterObj.ID] = row
}
return
}
func (i *alert) ConditionCreate(tx *gorm.DB, obj *db2.Alarm, conditions []view.ReqAlarmConditionCreate, filter *db2.AlarmFilter) (exp string, err error) {
expVal := fmt.Sprintf("%s{%s} offset 10s", bumo.PrometheusMetricName, factory.TagsToString(obj, false, filter.ID))
sort.Slice(conditions, func(i, j int) bool {
return conditions[i].SetOperatorTyp < conditions[j].SetOperatorTyp
})
for _, condition := range conditions {
var innerCond string
var ot string
switch condition.SetOperatorExp {
case 0:
ot = "avg_over_time"
case 1:
ot = "min_over_time"
case 2:
ot = "max_over_time"
case 3:
ot = "sum_over_time"
case 4:
ot = "count_over_time"
}
expValOverTime := fmt.Sprintf("%s(%s{%s}[%s] offset 10s)", ot, bumo.PrometheusMetricName, factory.TagsToString(obj, false, filter.ID), obj.AlertInterval())
switch condition.Cond {
case 0:
innerCond = fmt.Sprintf("%s>%d", expValOverTime, condition.Val1)
case 1:
innerCond = fmt.Sprintf("%s<%d", expValOverTime, condition.Val1)
case 2:
innerCond = fmt.Sprintf("(%s<%d or %s>%d)", expValOverTime, condition.Val1, expValOverTime, condition.Val2)
case 3:
innerCond = fmt.Sprintf("(%s>=%d and %s<=%d)", expValOverTime, condition.Val1, expValOverTime, condition.Val2)
}
switch condition.SetOperatorTyp {
case 0:
exp = innerCond
case 1:
if exp == "" {
err = errors.New("conditions error")
return
}
exp = fmt.Sprintf("%s and %s", exp, innerCond)
case 2:
if exp == "" {
err = errors.New("conditions error")
return
}
exp = fmt.Sprintf("%s or %s", exp, innerCond)
}
conditionObj := &db2.AlarmCondition{
AlarmId: obj.ID,
FilterId: filter.ID,
SetOperatorTyp: condition.SetOperatorTyp,
SetOperatorExp: condition.SetOperatorExp,
Cond: condition.Cond,
Val1: condition.Val1,
Val2: condition.Val2,
}
err = db2.AlarmConditionCreate(tx, conditionObj)
if err != nil {
return
}
}
// empty data alert
exp = aggregationOp(filter.Mode, exp, expVal)
exp = noDataOp(obj.NoDataOp, exp, expVal)
return
}
func (i *alert) PrometheusReload(prometheusTarget string) (err error) {
resp, err := http.Post(strings.TrimSuffix(prometheusTarget, "/")+"/-/reload", "text/html;charset=utf-8", nil)
if err != nil {
elog.Error("reload", elog.Any("reload", prometheusTarget+"/-/reload"), elog.Any("err", err.Error()))
return
}
defer func() { _ = resp.Body.Close() }()
return
}
func (i *alert) PrometheusRuleGen(obj *db2.Alarm, exp string, filterId int) string {
// TODO 后期增加 for 参数
// return fmt.Sprintf(prometheusRuleTemplate, obj.UniqueName(filterId), exp, obj.AlertInterval())
return fmt.Sprintf(prometheusRuleTemplateWithoutFor, obj.UniqueName(filterId), exp)
}
func (i *alert) PrometheusRuleCreateOrUpdate(instance db2.BaseInstance, groupName, ruleName, content string) (err error) {
rc, err := rule.GetComponent(instance.RuleStoreType, &rule.Params{
InstanceID: instance.ID,
RulePath: instance.FilePath,
ClusterId: instance.K8sClusterId,
Namespace: instance.K8sNamespace,
Configmap: instance.K8sConfigmap,
PrometheusOperator: instance.ConfigPrometheusOperator,
})
if err != nil {
return err
}
if err = rc.CreateOrUpdate(groupName, ruleName, content); err != nil {
return
}
go i.AddPrometheusReloadChan()
return nil
}
func (i *alert) PrometheusRuleBatchSet(clusterRuleGroups map[string]db2.ClusterRuleGroup) (err error) {
for _, clusterRuleGroup := range clusterRuleGroups {
rc, err := rule.GetComponent(clusterRuleGroup.Instance.RuleStoreType, &rule.Params{
InstanceID: clusterRuleGroup.Instance.ID,
RulePath: clusterRuleGroup.Instance.FilePath,
ClusterId: clusterRuleGroup.Instance.K8sClusterId,
Namespace: clusterRuleGroup.Instance.K8sNamespace,
Configmap: clusterRuleGroup.Instance.K8sConfigmap,
PrometheusOperator: clusterRuleGroup.Instance.ConfigPrometheusOperator,
})
if err != nil {
return errors.Wrap(err, "k8s configmap write error")
}
if err = rc.BatchSet(clusterRuleGroup.GroupName, clusterRuleGroup.Rules); err != nil {
return err
}
}
go i.AddPrometheusReloadChan()
return nil
}
func (i *alert) PrometheusRuleBatchRemove(clusterRuleGroups map[string]db2.ClusterRuleGroup) (err error) {
for _, clusterRuleGroup := range clusterRuleGroups {
rc, err := rule.GetComponent(clusterRuleGroup.Instance.RuleStoreType, &rule.Params{
InstanceID: clusterRuleGroup.Instance.ID,
RulePath: clusterRuleGroup.Instance.FilePath,
ClusterId: clusterRuleGroup.Instance.K8sClusterId,
Namespace: clusterRuleGroup.Instance.K8sNamespace,
Configmap: clusterRuleGroup.Instance.K8sConfigmap,
PrometheusOperator: clusterRuleGroup.Instance.ConfigPrometheusOperator,
})
if err != nil {
return err
}
if err = rc.BatchRemove(clusterRuleGroup.GroupName); err != nil {
return err
}
}
go i.AddPrometheusReloadChan()
return nil
}
func (i *alert) DeletePrometheusRule(instance *db2.BaseInstance, obj *db2.Alarm) (err error) {
if obj.AlertRules == nil || len(obj.AlertRules) == 0 {
// v1 version
return alarmRuleDelete(instance, obj.GetGroupName(instance.ID), obj.RuleName(0))
} else {
// v2 version
for iidRuleName := range obj.AlertRules {
ruleName := iidRuleName
ins := *instance
iidTableArr := strings.Split(iidRuleName, "|")
if len(iidTableArr) == 2 {
ruleName = iidTableArr[1]
iid, _ := strconv.Atoi(iidTableArr[0])
ins, _ = db2.InstanceInfo(invoker.Db, iid)
}
if ins.RuleStoreType == db2.RuleStoreTypeK8sOperator {
continue
}
if err = alarmRuleDelete(&ins, obj.GetGroupName(ins.ID), ruleName); err != nil {
return
}
}
}
go i.AddPrometheusReloadChan()
return nil
}
func (i *alert) CreateOrUpdate(alarmObj *db2.Alarm, req view.ReqAlarmCreate) (err error) {
// v1 -> v2 disable root conditions field
req.ConvertV2()
filtersDB, err := i.FilterCreate(invoker.Db, alarmObj, req.Filters)
if err != nil {
return
}
// create new views
viewDDLs := db2.String2String{}
alertRules := db2.String2String{}
clusterRuleGroups := map[string]db2.ClusterRuleGroup{}
for filterId, filterItem := range filtersDB {
var tableInfo db2.BaseTable
// table info
tableInfo, err = db2.TableInfo(invoker.Db, filterItem.Tid)
if err != nil {
return
}
// prometheus set
var instance db2.BaseInstance
instance, err = db2.InstanceInfo(invoker.Db, tableInfo.Database.Iid)
if err != nil {
return
}
var op factory.Operator
op, err = InstanceManager.Load(tableInfo.Database.Iid)
if err != nil {
return
}
// drop alert views
if len(alarmObj.ViewDDLs) > 0 {
for iidTable := range alarmObj.ViewDDLs {
ddlOp := op
table := iidTable
iidTableArr := strings.Split(iidTable, "|")
if len(iidTableArr) == 2 {
table = iidTableArr[1]
iid, _ := strconv.Atoi(iidTableArr[0])
ddlOp, err = InstanceManager.Load(iid)
if err != nil {
return
}
if iid != instance.ID {
continue
}
}
if err = ddlOp.DeleteAlertView(table, tableInfo.Database.Cluster); err != nil {
return
}
}
} else {
if alarmObj.ViewTableName != "" {
err = op.DeleteAlertView(alarmObj.ViewTableName, tableInfo.Database.Cluster)
if err != nil {
elog.Error("alert", elog.String("step", "alert create failed 05"), elog.String("err", err.Error()))
return
}
}
}
// gen view table name & sql
table, ddl, errAlertViewGen := op.GetAlertViewSQL(alarmObj, tableInfo, filterId, &filterItem)
if errAlertViewGen != nil {
return errAlertViewGen
}
// exec view sql
if err = op.CreateAlertView(table, ddl, tableInfo.Database.Cluster); err != nil {
return
}
viewDDLs[fmt.Sprintf("%d|%s", tableInfo.Database.Iid, table)] = ddl
// rule store
r := i.PrometheusRuleGen(alarmObj, filterItem.Exp, filterId)
ruleName := alarmObj.RuleName(filterId)
alertRules[fmt.Sprintf("%d|%s", tableInfo.Database.Iid, ruleName)] = r
if instance.RuleStoreType == db2.RuleStoreTypeK8sOperator {
clusterRuleGroup := db2.ClusterRuleGroup{}
if tmp, ok := clusterRuleGroups[instance.GetRuleStoreKey()]; ok {
clusterRuleGroup = tmp
} else {
clusterRuleGroup.ClusterId = instance.K8sClusterId
clusterRuleGroup.Instance = instance
clusterRuleGroup.GroupName = alarmObj.GetGroupName(instance.ID)
clusterRuleGroup.Rules = make([]db2.ClusterRuleItem, 0)
}
clusterRuleGroup.Rules = append(clusterRuleGroup.Rules, db2.ClusterRuleItem{
RuleName: ruleName,
Content: r,
})
clusterRuleGroups[instance.GetRuleStoreKey()] = clusterRuleGroup
} else if instance.RuleStoreType == db2.RuleStoreTypeFile || instance.RuleStoreType == db2.RuleStoreTypeK8sConfigMap {
if err = Alert.DeletePrometheusRule(&instance, alarmObj); err != nil {
return
}
if err = i.PrometheusRuleCreateOrUpdate(instance, alarmObj.GetGroupName(instance.ID), ruleName, r); err != nil {
return
}
}
}
ups := make(map[string]interface{}, 0)
ups["alert_rules"] = alertRules
ups["view_ddl_s"] = viewDDLs
ups["status"] = db2.AlarmStatusRuleCheck
err = db2.AlarmUpdate(invoker.Db, alarmObj.ID, ups)
if err != nil {
return
}
if len(clusterRuleGroups) > 0 {
if err = i.PrometheusRuleBatchSet(clusterRuleGroups); err != nil {
return
}
}
return nil
}
func (i *alert) OpenOperator(id int) (err error) {
alarmInfo, relatedList, err := db2.GetAlarmTableInstanceInfo(id)
if err != nil {
return
}
clusterRuleGroups := map[string]db2.ClusterRuleGroup{}
for _, ri := range relatedList {
op, errInstanceManager := InstanceManager.Load(ri.Instance.ID)
if errInstanceManager != nil {
return errInstanceManager
}
if len(alarmInfo.ViewDDLs) > 0 {
for iidTable, ddl := range alarmInfo.ViewDDLs {
table := iidTable
iidTableArr := strings.Split(iidTable, "|")
if len(iidTableArr) == 2 {
table = iidTableArr[1]
iid, _ := strconv.Atoi(iidTableArr[0])
op, err = InstanceManager.Load(iid)
if err != nil {
return
}
if iid != ri.Table.Database.Iid {
continue
}
}
if err = op.CreateAlertView(table, ddl, ri.Table.Database.Cluster); err != nil {
return
}
}
} else {
if err = op.CreateAlertView(alarmInfo.ViewTableName, alarmInfo.View, ri.Table.Database.Cluster); err != nil {
return
}
}
if len(alarmInfo.AlertRules) > 0 {
for iidRuleName, alertRule := range alarmInfo.AlertRules {
ruleName := iidRuleName
iidTableArr := strings.Split(iidRuleName, "|")
var instance db2.BaseInstance
if len(iidTableArr) == 2 {
ruleName = iidTableArr[1]
iid, _ := strconv.Atoi(iidTableArr[0])
instance, _ = db2.InstanceInfo(invoker.Db, iid)
}
if instance.RuleStoreType == db2.RuleStoreTypeK8sOperator {
clusterRuleGroup := db2.ClusterRuleGroup{}
if tmp, ok := clusterRuleGroups[instance.GetRuleStoreKey()]; ok {
clusterRuleGroup = tmp
} else {
clusterRuleGroup.ClusterId = instance.K8sClusterId
clusterRuleGroup.Instance = instance
clusterRuleGroup.GroupName = alarmInfo.GetGroupName(instance.ID)
clusterRuleGroup.Rules = make([]db2.ClusterRuleItem, 0)
}
clusterRuleGroup.Rules = append(clusterRuleGroup.Rules, db2.ClusterRuleItem{
RuleName: ruleName,
Content: alertRule,
})
clusterRuleGroups[instance.GetRuleStoreKey()] = clusterRuleGroup
} else if instance.RuleStoreType == db2.RuleStoreTypeFile || instance.RuleStoreType == db2.RuleStoreTypeK8sConfigMap {
if err = i.PrometheusRuleCreateOrUpdate(instance, alarmInfo.GetGroupName(instance.ID), ruleName, alertRule); err != nil {
elog.Error("alert", elog.String("step", "prometheus rule delete failed"), elog.String("err", err.Error()))
return
}
}
}
} else if alarmInfo.Tid > 0 {
table, _ := db2.TableInfo(invoker.Db, alarmInfo.Tid)
instance, _ := db2.InstanceInfo(invoker.Db, table.Database.Iid)
if instance.RuleStoreType == db2.RuleStoreTypeK8sOperator {
clusterRuleGroup := db2.ClusterRuleGroup{}
if tmp, ok := clusterRuleGroups[instance.GetRuleStoreKey()]; ok {
clusterRuleGroup = tmp
} else {
clusterRuleGroup.ClusterId = instance.K8sClusterId
clusterRuleGroup.Instance = instance
clusterRuleGroup.GroupName = alarmInfo.GetGroupName(instance.ID)
clusterRuleGroup.Rules = make([]db2.ClusterRuleItem, 0)
}
clusterRuleGroup.Rules = append(clusterRuleGroup.Rules, db2.ClusterRuleItem{
RuleName: alarmInfo.GetGroupName(instance.ID),
Content: alarmInfo.RuleName(0),
})
clusterRuleGroups[instance.GetRuleStoreKey()] = clusterRuleGroup
} else if instance.RuleStoreType == db2.RuleStoreTypeFile || instance.RuleStoreType == db2.RuleStoreTypeK8sConfigMap {
if err = i.PrometheusRuleCreateOrUpdate(instance, alarmInfo.GetGroupName(instance.ID), alarmInfo.RuleName(0), alarmInfo.AlertRule); err != nil {
elog.Error("alert", elog.String("step", "prometheus rule delete failed"), elog.String("err", err.Error()))
return
}
}
}
}
if len(clusterRuleGroups) > 0 {
if err = i.PrometheusRuleBatchSet(clusterRuleGroups); err != nil {
return
}
}
_ = db2.AlarmFilterUpdateStatus(invoker.Db, id, map[string]interface{}{"status": db2.AlarmStatusNormal})
if err = db2.AlarmUpdate(invoker.Db, id, map[string]interface{}{"status": db2.AlarmStatusRuleCheck}); err != nil {
return
}
return
}
func (i *alert) Update(uid, alarmId int, req view.ReqAlarmCreate) (err error) {
if req.Name == "" || req.Interval == 0 || len(req.ChannelIds) == 0 {
return errors.New("error params")
}
tx := invoker.Db.Begin()
ups := make(map[string]interface{}, 0)
ups["name"] = req.Name
ups["desc"] = req.Desc
ups["interval"] = req.Interval
ups["unit"] = req.Unit
ups["uid"] = uid
ups["no_data_op"] = req.NoDataOp
ups["level"] = req.Level
ups["channel_ids"] = db2.Ints(req.ChannelIds)
ups["duty_officers"] = db2.Ints(req.DutyOfficers)
ups["is_disable_resolve"] = req.IsDisableResolve
tableIds := db2.Ints{}
for _, f := range req.Filters {
tableIds = append(tableIds, f.Tid)
}
ups["table_ids"] = tableIds
if err = db2.AlarmUpdate(tx, alarmId, ups); err != nil {
tx.Rollback()
return
}
// filter
if err = db2.AlarmFilterDeleteBatch(tx, alarmId); err != nil {
tx.Rollback()
return
}
// condition
if err = db2.AlarmConditionDeleteBatch(tx, alarmId); err != nil {
tx.Rollback()
return
}
obj, err := db2.AlarmInfo(tx, alarmId)
if err != nil {
tx.Rollback()
return
}
if err = tx.Commit().Error; err != nil {
return
}
if err = i.CreateOrUpdate(&obj, req); err != nil {
return
}
return
}
func (i *alert) AddPrometheusReloadChan() {
// 10 times
for k := 0; k < reloadTimes; k++ {
if len(i.reloadChan) < reloadTimes {
i.reloadChan <- time.Now().Unix()
}
}
}
func (i *alert) PlusOnePrometheusReloadChan() {
// 10 times
if len(i.reloadChan) < reloadTimes {
i.reloadChan <- time.Now().Unix()
}
}
func (i *alert) IsAllClosed(iid int) (err error) {
tables, err := db2.TableListByInstanceId(invoker.Db, iid)
if err != nil {
return err
}
tidArr := make([]int, 0)
for _, table := range tables {
tidArr = append(tidArr, table.ID)
}
// Detect whether there is an alert in effect.
conds := egorm.Conds{}
conds["status"] = egorm.Cond{
Op: ">",
Val: 1,
}
alarms, err := db2.AlarmListByTidArr(conds, tidArr)
if err != nil {
return err
}
if len(alarms) == 0 {
return nil
}
errReason := ""
for _, a := range alarms {
errReason = fmt.Sprintf("%sid: %d, name: %s ;", errReason, a.ID, a.Name)
}
return errors.New("Contains non-closed alert:" + errReason)
}
func AllPrometheusReload() {
instances, err := db2.InstanceList(egorm.Conds{})
if err != nil {
elog.Error("AllPrometheusReload", elog.String("step", "InstanceList"), elog.String("error", err.Error()))
return
}
pm := make(map[string]interface{})
for _, ins := range instances {
if ins.PrometheusTarget != "" {
pm[ins.PrometheusTarget] = struct{}{}
}
}
for target := range pm {
errReload := Alert.PrometheusReload(target)
if errReload != nil {
elog.Error("AllPrometheusReload", elog.String("step", "PrometheusReload"), elog.String("error", errReload.Error()))
}
}
}
// AlertRuleCheck Detect alert rules in progress
func AlertRuleCheck() error {
conds := egorm.Conds{}
conds["status"] = db2.AlarmStatusRuleCheck
alarms, err := db2.AlarmList(conds)
if err != nil {
return err
}
// Find all instances
promPool := make(map[int]*alertcomponent.Prometheus)
for _, alarm := range alarms {
isRuleOk := true
rulesCheckMap := make(map[int][]string, 0)
rulesV2 := alarm.RuleNameMap()
if alarm.AlertRule == "" && len(rulesV2) == 0 {
// v1版本规则已删除,v2版本规则未下发
// 理论上这是一种异常情况
isRuleOk = false
}
if len(rulesV2) > 0 {
// v2 check
rulesCheckMap = rulesV2
} else if alarm.AlertRule != "" {
// v1 check
tableInfo, _ := db2.TableInfo(invoker.Db, alarm.Tid)
if tableInfo.ID != 0 {
rulesCheckMap[tableInfo.Database.Iid] = append(rulesCheckMap[tableInfo.Database.Iid], alarm.AlertRule)
}
}
for iid, ruleList := range rulesCheckMap {
prom, ok := promPool[iid]
if !ok {
// Cache once
ins, _ := db2.InstanceInfo(invoker.Db, iid)
if ins.RuleStoreType == 0 {
isRuleOk = false
break
}
prom, err = alertcomponent.NewPrometheus(ins.PrometheusTarget, ins.RuleStoreType)
if err != nil {
core.LoggerError("ruleCheck", "prometheus", err)
isRuleOk = false
break
}
promPool[iid] = prom
}
if okIsEffect, errIsEffect := prom.IsRuleTakeEffect(ruleList); errIsEffect != nil {
core.LoggerError("ruleCheck", "isRuleTakeEffect", errIsEffect)
isRuleOk = false
break
} else if !okIsEffect {
isRuleOk = false
break
}
}
if isRuleOk {
if err = db2.AlarmUpdate(invoker.Db, alarm.ID, map[string]interface{}{"status": db2.AlarmStatusNormal}); err != nil {
core.LoggerError("ruleCheck", "isRuleTakeEffect", err)
continue
}
}
}
return nil
}
func SendTestToChannel(c *db2.AlarmChannel) (err error) {
ci, err := pusher.GetPusher(c.Typ)
if err != nil {
return errors.Wrapf(err, "channel type %d not found", c.Typ)
}
err = ci.Send(c, &db2.PushMsg{
Title: "Hello",
Text: "test/alert/alarm/告警 the availability of the alarm channel",
Mobiles: econf.GetStringSlice("app.mobiles"),
})
if err != nil {
return errors.Wrapf(err, "channel type %d send failed", c.Typ)
}
return
}
func AlarmAttachInfo(respList []*db2.Alarm) []view.RespAlarmList {
res := make([]view.RespAlarmList, 0)
cache := make(map[int]*db2.RespAlarmListRelatedInfo, 0)
for _, a := range respList {
alarmInfo, relatedList, errAlarmInfo := db2.GetAlarmTableInstanceInfoWithCache(a.ID, cache)
if errAlarmInfo != nil {
core.LoggerError("alert", "attach", errAlarmInfo)
continue
}
if alarmInfo.User == nil || alarmInfo.User.ID == 0 {
u, _ := db2.UserInfo(alarmInfo.Uid)
alarmInfo.User = &u
}
alarmInfo.User.Password = "*"
var (
tableInfo db2.BaseTable
instanceInfo db2.BaseInstance
)
if len(relatedList) > 0 {
tableInfo = relatedList[0].Table
instanceInfo = relatedList[0].Instance
}
res = append(res, view.RespAlarmList{
Alarm: &alarmInfo,
RelatedList: relatedList,
TableName: tableInfo.Name,
TableDesc: tableInfo.Desc,
Tid: tableInfo.ID,
DatabaseName: tableInfo.Database.Name,
DatabaseDesc: tableInfo.Database.Desc,
Did: tableInfo.Did,
InstanceName: instanceInfo.Name,
InstanceDesc: instanceInfo.Desc,
Iid: instanceInfo.ID,
})
}
return res
}
func aggregationOp(mode int, exp string, expVal string) string {
switch mode {
case db2.AlarmModeAggregation:
return fmt.Sprintf("%s and %s!=-1", exp, expVal)
default:
return exp
}
}
func noDataOp(op int, exp, expVal string) string {
switch op {
case NoDataOpDefault:
return exp
case NoDataOpOK:
return fmt.Sprintf("(%s) or absent(%s)!=1", exp, expVal)
case NoDataOpAlert:
return fmt.Sprintf("(%s) or absent(%s)==1", exp, expVal)
default:
return exp
}
}
func alarmRuleDelete(instance *db2.BaseInstance, groupName, ruleName string) (err error) {
rc, err := rule.GetComponent(instance.RuleStoreType, &rule.Params{
InstanceID: instance.ID,
RulePath: instance.FilePath,
ClusterId: instance.K8sClusterId,
Namespace: instance.K8sNamespace,
Configmap: instance.K8sConfigmap,
PrometheusOperator: instance.ConfigPrometheusOperator,
})
if err != nil {
return err
}
return rc.Delete(groupName, ruleName)
}