From d590f6d5c159e6337f274f84b311faeaed6fd99c Mon Sep 17 00:00:00 2001 From: Ulric Qin Date: Sat, 8 Jan 2022 17:52:29 +0800 Subject: [PATCH] enable_in_bg logic --- docker/initsql/a-n9e.sql | 1 + src/models/alert_rule.go | 1 + src/server/engine/worker.go | 18 ++++++++++++------ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/docker/initsql/a-n9e.sql b/docker/initsql/a-n9e.sql index 89af895b..89dff8cc 100644 --- a/docker/initsql/a-n9e.sql +++ b/docker/initsql/a-n9e.sql @@ -207,6 +207,7 @@ CREATE TABLE `alert_rule` ( `enable_stime` char(5) not null default '00:00', `enable_etime` char(5) not null default '23:59', `enable_days_of_week` varchar(32) not null default '' comment 'split by space: 0 1 2 3 4 5 6', + `enable_in_bg` tinyint(1) not null default 0 comment '1: only this bg 0: global', `notify_recovered` tinyint(1) not null comment 'whether notify when recovery', `notify_channels` varchar(255) not null default '' comment 'split by space: sms voice email dingtalk wecom', `notify_groups` varchar(255) not null default '' comment 'split by space: 233 43', diff --git a/src/models/alert_rule.go b/src/models/alert_rule.go index 128a4420..24768f5b 100644 --- a/src/models/alert_rule.go +++ b/src/models/alert_rule.go @@ -28,6 +28,7 @@ type AlertRule struct { EnableEtime string `json:"enable_etime"` // e.g. 23:59 EnableDaysOfWeek string `json:"-"` // split by space: 0 1 2 3 4 5 6 EnableDaysOfWeekJSON []string `json:"enable_days_of_week" gorm:"-"` // for fe + EnableInBG int `json:"enable_in_bg"` // 0: global 1: enable one busi-group NotifyRecovered int `json:"notify_recovered"` // whether notify when recovery NotifyChannels string `json:"-"` // split by space: sms voice email dingtalk wecom NotifyChannelsJSON []string `json:"notify_channels" gorm:"-"` // for fe diff --git a/src/server/engine/worker.go b/src/server/engine/worker.go index f364867f..eb006a05 100644 --- a/src/server/engine/worker.go +++ b/src/server/engine/worker.go @@ -202,6 +202,12 @@ func (r RuleEval) judge(vectors []Vector) { tagsMap[string(label)] = string(value) } + // handle rule tags + for _, tag := range r.rule.AppendTagsJSON { + arr := strings.SplitN(tag, "=", 2) + tagsMap[arr[0]] = arr[1] + } + // handle target note targetIdent, has := vectors[i].Labels["ident"] targetNote := "" @@ -209,13 +215,13 @@ func (r RuleEval) judge(vectors []Vector) { target, exists := memsto.TargetCache.Get(string(targetIdent)) if exists { targetNote = target.Note - } - } - // handle rule tags - for _, tag := range r.rule.AppendTagsJSON { - arr := strings.SplitN(tag, "=", 2) - tagsMap[arr[0]] = arr[1] + // 对于包含ident的告警事件,check一下ident所属bg和rule所属bg是否相同 + // 如果告警规则选择了只在本BG生效,那其他BG的机器就不能因此规则产生告警 + if r.rule.EnableInBG == 1 && target.GroupId != r.rule.GroupId { + continue + } + } } event := &models.AlertCurEvent{