diff --git a/pkg/monitor/alerting/notifiers/onecloud.go b/pkg/monitor/alerting/notifiers/onecloud.go index fc1a8560d5..c420094b07 100644 --- a/pkg/monitor/alerting/notifiers/onecloud.go +++ b/pkg/monitor/alerting/notifiers/onecloud.go @@ -91,7 +91,7 @@ func GetNotifyTemplateConfig(ctx *alerting.EvalContext) monitor.NotificationTemp level = "重要" case "fatal", "critical": priority = notify.NotifyPriorityCritical - level = "严重" + level = "致命" } topic := fmt.Sprintf("[%s]", level) @@ -117,7 +117,15 @@ func (oc *OneCloudNotifier) Notify(ctx *alerting.EvalContext, _ jsonutils.JSONOb log.Infof("Sending alert notification %s to onecloud", ctx.GetRuleTitle()) config := GetNotifyTemplateConfig(ctx) contentConfig := oc.buildContent(config) - content, err := contentConfig.GenerateMarkdown() + + var content string + var err error + switch oc.Setting.Channel { + case string(notify.NotifyByEmail): + content, err = contentConfig.GenerateEmailMarkdown() + default: + content, err = contentConfig.GenerateMarkdown() + } if err != nil { return errors.Wrap(err, "build content") } diff --git a/pkg/monitor/alerting/notifiers/templates/template.go b/pkg/monitor/alerting/notifiers/templates/template.go index 4a3db78e34..3840073533 100644 --- a/pkg/monitor/alerting/notifiers/templates/template.go +++ b/pkg/monitor/alerting/notifiers/templates/template.go @@ -26,29 +26,59 @@ func NewTemplateConfig(c monitor.NotificationTemplateConfig) *TemplateConfig { } } -const MarkdownTemplate = ` -## {{.Title}} +const DefaultMarkdownTemplate = ` + ## {{.Title}} -- 时间: {{.StartTime}} -- 级别: {{.Level}} + - 时间: {{.StartTime}} + - 级别: {{.Level}} -{{range .Matches}} + {{range .Matches}} -- 指标: {{.Metric}} -- 当前值: {{.Value}} + - 指标: {{.Metric}} + - 当前值: {{.Value}} -### 触发条件: + ### 触发条件: -> {{.Condition}} + > {{.Condition}} -### 标签 + ### 标签 -{{range $key, $value := .Tags}} -> {{ $key }}: {{ $value}} -{{end}} -{{end}} + {{range $key, $value := .Tags}} + > {{ $key }}: {{ $value}} + {{end}} + {{end}} +` + +const EmailMarkdownTemplate = ` + + +
+ + + +## {{.Title}}
+- 时间: {{.StartTime}}
+- 级别: {{.Level}}
+{{range .Matches}}
+- 指标: {{.Metric}}
+- 当前值: {{.Value}}
+## 触发条件:
+{{.Condition}}
+## 标签
++ {{range $key, $value := .Tags}} + {{ $key }}: {{ $value}} + {{end}} + {{end}} +
+ + ` func (c TemplateConfig) GenerateMarkdown() (string, error) { - return CompileTEmplateFromMap(MarkdownTemplate, c) + return CompileTEmplateFromMap(DefaultMarkdownTemplate, c) +} + +func (c TemplateConfig) GenerateEmailMarkdown() (string, error) { + return CompileTEmplateFromMap(EmailMarkdownTemplate, c) } diff --git a/pkg/monitor/alerting/notifiers/templates/util.go b/pkg/monitor/alerting/notifiers/templates/util.go index 07fab9814a..e3ddc4e4bd 100644 --- a/pkg/monitor/alerting/notifiers/templates/util.go +++ b/pkg/monitor/alerting/notifiers/templates/util.go @@ -16,7 +16,7 @@ package templates import ( "bytes" - "text/template" + "html/template" ) func CompileTEmplateFromMap(tmplt string, configMap interface{}) (string, error) {