Skip to content

Commit 8a63cf8

Browse files
author
jikun.zhang
committed
增加邮件告警支持自定义邮件标题参数
Signed-off-by: jikun.zhang <[email protected]>
1 parent de8381a commit 8a63cf8

File tree

6 files changed

+24
-13
lines changed

6 files changed

+24
-13
lines changed

controllers/WebTest.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (c *MainController) AlertTest() {
4949
<h3>PrometheusAlert邮件告警测试</h3>
5050
欢迎使用<a href ="https://feiyu563.gitee.io">PrometheusAlert</a><br>
5151
`
52-
ret := SendEmail(TestEmailMessage, beego.AppConfig.String("Default_emails"), logsign)
52+
ret := SendEmail(TestEmailMessage, beego.AppConfig.String("Default_emails"), beego.AppConfig.String("Email_title"), logsign)
5353
c.Data["json"] = ret
5454
case "7moordx":
5555
MobileMessage := "PrometheusAlertCenter测试告警"
@@ -93,7 +93,7 @@ func (c *MainController) AlertTest() {
9393
c.ServeJSON()
9494
}
9595

96-
//markdown test
96+
// markdown test
9797
func (c *MainController) MarkdownTest() {
9898
if !CheckAccount(c.Ctx) {
9999
c.Redirect("/login", 302)
@@ -123,7 +123,7 @@ func (c *MainController) MarkdownTest() {
123123

124124
}
125125

126-
//test page
126+
// test page
127127
func (c *MainController) Test() {
128128
if !CheckAccount(c.Ctx) {
129129
c.Redirect("/login", 302)

controllers/email.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
// SendEmail
13-
func SendEmail(EmailBody, Emails, logsign string) string {
13+
func SendEmail(EmailBody, Emails, EmailTitle, logsign string) string {
1414
open := beego.AppConfig.String("open-email")
1515
if open != "1" {
1616
logs.Info(logsign, "[email]", "email未配置未开启状态,请先配置open-email为1")
@@ -20,7 +20,7 @@ func SendEmail(EmailBody, Emails, logsign string) string {
2020
serverPort, _ := beego.AppConfig.Int("Email_port")
2121
fromEmail := beego.AppConfig.String("Email_user")
2222
Passwd := beego.AppConfig.String("Email_password")
23-
EmailTitle := beego.AppConfig.String("Email_title")
23+
//EmailTitle := beego.AppConfig.String("Email_title")
2424
2525
SendToEmails := []string{}
2626
m := gomail.NewMessage()

controllers/gitlab.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ func sendGitlabEvent(typeid int, event GitlabEvent, eventType, logsign, sendURL
473473
if sendURL == "" {
474474
sendURL = beego.AppConfig.String("Default_emails")
475475
}
476-
SendEmail(EmailMessage, sendURL, logsign)
476+
SendEmail(EmailMessage, sendURL, beego.AppConfig.String("Email_title"), logsign)
477477
// 2 dingding robot
478478
case 2:
479479
DDtext := genDDtext(event, eventType)

controllers/prometheus.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,14 +286,16 @@ func SendMessageR(message Prometheus, rwxurl, rddurl, rfsurl, rphone, remail, rg
286286
//发送消息到Email
287287
if remail == "" && RMessage.Annotations.Email == "" {
288288
Emails := beego.AppConfig.String("Default_emails")
289-
SendEmail(EmailMessage, Emails, logsign)
289+
EmailTitle := beego.AppConfig.String("Email_title")
290+
SendEmail(EmailMessage, Emails, EmailTitle, logsign)
290291
} else {
292+
EmailTitle := beego.AppConfig.String("Email_title")
291293
if remail != "" {
292-
SendEmail(EmailMessage, remail, logsign)
294+
SendEmail(EmailMessage, remail, EmailTitle, logsign)
293295
}
294296
if RMessage.Annotations.Email != "" {
295297
Emails := RMessage.Annotations.Email
296-
SendEmail(EmailMessage, Emails, logsign)
298+
SendEmail(EmailMessage, Emails, EmailTitle, logsign)
297299
}
298300
}
299301
//发送消息到短信

controllers/prometheusalert.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type PrometheusAlertMsg struct {
6868
WebHookUrl string
6969
ToUser string
7070
Email string
71+
EmailTitle string
7172
ToParty string
7273
ToTag string
7374
GroupId string
@@ -141,6 +142,7 @@ func (c *PrometheusAlertController) PrometheusAlert() {
141142
pMsg.ToUser = checkURL(c.Input().Get("wxuser"), beego.AppConfig.String("WorkWechat_ToUser"))
142143
pMsg.ToParty = checkURL(c.Input().Get("wxparty"), beego.AppConfig.String("WorkWechat_ToUser"))
143144
pMsg.ToTag = checkURL(c.Input().Get("wxtag"), beego.AppConfig.String("WorkWechat_ToUser"))
145+
pMsg.EmailTitle = checkURL(c.Input().Get("emailtitle"), beego.AppConfig.String("Email_title"))
144146

145147
// dd, wx, fsv2 的 at 格式不一样,放在告警组里不好处理和组装。
146148
pMsg.AtSomeOne = c.Input().Get("at")
@@ -520,7 +522,7 @@ func SendMessagePrometheusAlert(message string, pmsg *PrometheusAlertMsg, logsig
520522
ReturnMsg += Post7MOORphonecall(message, pmsg.Phone, logsign)
521523
//邮件
522524
case "email":
523-
ReturnMsg += SendEmail(message, pmsg.Email, logsign)
525+
ReturnMsg += SendEmail(message, pmsg.Email, pmsg.EmailTitle, logsign)
524526
// Telegram
525527
case "tg":
526528
ReturnMsg += SendTG(message, logsign)

doc/readme/system-var.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,15 @@
2323
`示例:http://[prometheusalert_url]:8080/prometheusalert?type=dd&tpl=prometheus-dd&ddurl=https://oapi.dingtalk.com/robot/send?access_token=xxxx,https://oapi.dingtalk.com/robot/send?access_token=xxxxxx,https://oapi.dingtalk.com/robot/send?access_token=xxxxxx&rr=true&split=false`
2424

2525

26-
#### 5.自定义模板使用的是go语言的template模版,可以参考默认模版的一些语法来进行自定义。
26+
#### 5.url参数新增 `emailtitle=运维监控中心`,该参数仅针对email有效,作用是替换app.conf中的Email_title邮件标题配置,实现动态定义邮件标题
2727

28-
#### 6.模版数据等信息均存储在程序目录的下的`db/PrometheusAlertDB.db`中。
28+
注意:此参数如设置为`split=false`,则PrometheusAlert web页面的路由和告警记录等功能将自动关闭,请谨慎。
29+
30+
`示例:http://[prometheusalert_url]:8080/prometheusalert?type=email&tpl=prometheus-email&[email protected]&emailtitle=运维监控中心`
31+
32+
33+
#### 6.自定义模板使用的是go语言的template模版,可以参考默认模版的一些语法来进行自定义。
34+
35+
#### 7.模版数据等信息均存储在程序目录的下的`db/PrometheusAlertDB.db`中。
2936

30-
#### 7.关于优先级问题:路由功能 > URL参数 > app.conf
37+
#### 8.关于优先级问题:路由功能 > URL参数 > app.conf

0 commit comments

Comments
 (0)