diff --git a/build/monitor/root/opt/yunion/share/notify_templates/monitor/content@cn/DEFAULT.email b/build/monitor/root/opt/yunion/share/notify_templates/monitor/content@cn/DEFAULT.email index a05f63231e..ee4c3e944d 100644 --- a/build/monitor/root/opt/yunion/share/notify_templates/monitor/content@cn/DEFAULT.email +++ b/build/monitor/root/opt/yunion/share/notify_templates/monitor/content@cn/DEFAULT.email @@ -68,6 +68,7 @@ border-bottom: 1px solid #d7d7d7; } +{{$match_tags_str := .match_tags_str}}

报警提醒

@@ -110,6 +111,9 @@ + {{if gt (len $match_tags_str) 1}} + + {{end}} @@ -127,6 +131,9 @@ + {{if gt (len $match_tags_str) 1}} + + {{end}} {{end}} diff --git a/build/monitor/root/opt/yunion/share/notify_templates/monitor/content@en/DEFAULT.email b/build/monitor/root/opt/yunion/share/notify_templates/monitor/content@en/DEFAULT.email index 251bb773d9..2809601801 100644 --- a/build/monitor/root/opt/yunion/share/notify_templates/monitor/content@en/DEFAULT.email +++ b/build/monitor/root/opt/yunion/share/notify_templates/monitor/content@en/DEFAULT.email @@ -68,6 +68,7 @@ border-bottom: 1px solid #d7d7d7; } +{{$match_tags_str := .match_tags_str}}

Alert

平台 指标 触发值标签
{{ $Matche.metric }} {{ $Matche.value_str }}{{ index $match_tags_str $i }}
@@ -110,6 +111,9 @@ + {{if gt (len $match_tags_str) 1}} + + {{end}} @@ -127,6 +131,9 @@ + {{if gt (len $match_tags_str) 1}} + + {{end}} {{end}} diff --git a/pkg/apis/monitor/template.go b/pkg/apis/monitor/template.go index 8f35f4cff1..5b6402cb07 100644 --- a/pkg/apis/monitor/template.go +++ b/pkg/apis/monitor/template.go @@ -19,10 +19,12 @@ type NotificationTemplateCreateInput struct { } type NotificationTemplateConfig struct { - Title string `json:"title"` - Name string `json:"name"` - ResourceName string `json:"resource_name"` - Matches []*EvalMatch `json:"matches"` + Title string `json:"title"` + Name string `json:"name"` + ResourceName string `json:"resource_name"` + Matches []*EvalMatch `json:"matches"` + MatchTags []map[string]string `json:"match_tags"` + MatchTagsStr []string `json:"match_tags_str"` // PrevAlertState AlertStateType `json:"prev_alert_state"` // State AlertStateType `json:"state"` NoDataFound bool `json:"no_data"` diff --git a/pkg/cloudcommon/notifyclient/notify_internal.go b/pkg/cloudcommon/notifyclient/notify_internal.go index b3fa6433f5..0f7bb80fe6 100644 --- a/pkg/cloudcommon/notifyclient/notify_internal.go +++ b/pkg/cloudcommon/notifyclient/notify_internal.go @@ -265,7 +265,10 @@ func genMsgViaLang(ctx context.Context, p sNotifyParams) ([]npk.SNotifyMessage, topic = p.event } msg.Topic = topic - body, _ := getContent(langSuffix, p.event, "content", p.channel, p.data) + body, err := getContent(langSuffix, p.event, "content", p.channel, p.data) + if err != nil { + log.Errorf("get content error: %s", err) + } if len(body) == 0 { body, _ = p.data.GetString() } diff --git a/pkg/monitor/alerting/eval_context.go b/pkg/monitor/alerting/eval_context.go index f499e1902c..a9de870a99 100644 --- a/pkg/monitor/alerting/eval_context.go +++ b/pkg/monitor/alerting/eval_context.go @@ -22,6 +22,7 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" + "yunion.io/x/pkg/util/sets" "yunion.io/x/onecloud/pkg/apis/monitor" "yunion.io/x/onecloud/pkg/mcclient" @@ -205,11 +206,13 @@ func (c *EvalContext) GetNotificationTemplateConfig(matches []*monitor.EvalMatch desc += "Error: " + c.Error.Error() } tz, _ := time.LoadLocation(options.Options.TimeZone) - return monitor.NotificationTemplateConfig{ + cfg := monitor.NotificationTemplateConfig{ Title: c.GetNotificationTitle(), Name: c.Rule.Name, ResourceName: c.GetResourceNameOfMatches(matches), Matches: matches, + MatchTags: make([]map[string]string, len(matches)), + MatchTagsStr: make([]string, len(matches)), //Matches: c.GetEvalMatches(), StartTime: c.StartTime.In(tz).Format("2006-01-02 15:04:05"), EndTime: c.EndTime.In(tz).Format("2006-01-02 15:04:05"), @@ -218,6 +221,31 @@ func (c *EvalContext) GetNotificationTemplateConfig(matches []*monitor.EvalMatch NoDataFound: c.NoDataFound, WebUrl: c.GetCallbackURLPrefix(), } + // calculate match tags + diffKeySets := make(map[string]sets.String) + for i := range cfg.Matches { + m := cfg.Matches[i] + for mk, mv := range m.Tags { + if _, ok := diffKeySets[mk]; !ok { + diffKeySets[mk] = sets.NewString() + } + if sets.NewString("name", "host", "host_id", "ip", "host_id", "vm_id", "access_ip").Has(mk) { + continue + } + diffKeySets[mk].Insert(mv) + } + } + for i := range cfg.Matches { + m := cfg.Matches[i] + cfg.MatchTags[i] = make(map[string]string) + for diffKey, s := range diffKeySets { + if s.Len() > 1 { + cfg.MatchTags[i][diffKey] = m.Tags[diffKey] + } + } + cfg.MatchTagsStr[i] = jsonutils.Marshal(cfg.MatchTags[i]).String() + } + return cfg } func (c *EvalContext) GetEvalMatches() []monitor.EvalMatch { diff --git a/pkg/monitor/alerting/notifiers/onecloud.go b/pkg/monitor/alerting/notifiers/onecloud.go index 7c745ff587..fb5f137923 100644 --- a/pkg/monitor/alerting/notifiers/onecloud.go +++ b/pkg/monitor/alerting/notifiers/onecloud.go @@ -247,6 +247,10 @@ func getLangBystr(str string) language.Tag { func (oc *OneCloudNotifier) notifyByContextLang(ctx context.Context, evalCtx *alerting.EvalContext, uids []string) error { errs := []error{} + if evalCtx.Rule.State == monitor.AlertStatePending { + log.Warningf("skip notify rule because state is pending: %s", jsonutils.Marshal(evalCtx.Rule)) + return nil + } if len(evalCtx.EvalMatches) > 0 { if err := oc.notifyMatchesByContextLang(ctx, evalCtx, evalCtx.EvalMatches, uids, false); err != nil { errs = append(errs, errors.Wrapf(err, "notify alerting matches")) @@ -520,7 +524,7 @@ func (s *sendRobotImpl) execNotifyFunc() error { } func SendNotifyInfo(base *sendnotifyBase, imp Isendnotify) error { - tmpMatches := base.config.Matches + /*tmpMatches := base.config.Matches batch := 100 for i := 0; i < len(tmpMatches); i += batch { split := i + batch @@ -535,5 +539,7 @@ func SendNotifyInfo(base *sendnotifyBase, imp Isendnotify) error { } } - return nil + return nil*/ + base.config.ResourceName = base.evalCtx.GetResourceNameOfMatches(base.config.Matches) + return imp.execNotifyFunc() }
Brand Metric Trigger valueTags
{{ $Matche.metric }} {{ $Matche.value_str }}{{ index $match_tags_str $i }}