Merge pull request #5863 from zhaoxiangchun/bugfix/zxc-monitor-mail

Bugfix/zxc monitor mail
This commit is contained in:
Zexi Li
2020-04-20 20:52:56 +08:00
committed by GitHub
3 changed files with 56 additions and 18 deletions
+10 -2
View File
@@ -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")
}
@@ -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 = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<p>## {{.Title}}</p>
<p>- 时间: {{.StartTime}}</p>
<p>- 级别: {{.Level}}</p>
<p>{{range .Matches}}</p>
<p>- 指标: {{.Metric}}</p>
<p>- 当前值: {{.Value}}</p>
</br><p>## 触发条件:</p>
<p> {{.Condition}}</p>
</br><p>## 标签</p>
<p>
{{range $key, $value := .Tags}}
{{ $key }}: {{ $value}} </br>
{{end}}
{{end}}
</p>
</body>
</html>
`
func (c TemplateConfig) GenerateMarkdown() (string, error) {
return CompileTEmplateFromMap(MarkdownTemplate, c)
return CompileTEmplateFromMap(DefaultMarkdownTemplate, c)
}
func (c TemplateConfig) GenerateEmailMarkdown() (string, error) {
return CompileTEmplateFromMap(EmailMarkdownTemplate, c)
}
@@ -16,7 +16,7 @@ package templates
import (
"bytes"
"text/template"
"html/template"
)
func CompileTEmplateFromMap(tmplt string, configMap interface{}) (string, error) {