Skip to content

Commit

Permalink
增加邮件告警支持自定义邮件标题参数
Browse files Browse the repository at this point in the history
Signed-off-by: jikun.zhang <[email protected]>
  • Loading branch information
jikun.zhang committed Nov 29, 2024
1 parent de8381a commit 8a63cf8
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 13 deletions.
6 changes: 3 additions & 3 deletions controllers/WebTest.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *MainController) AlertTest() {
<h3>PrometheusAlert邮件告警测试</h3>
欢迎使用<a href ="https://feiyu563.gitee.io">PrometheusAlert</a><br>
`
ret := SendEmail(TestEmailMessage, beego.AppConfig.String("Default_emails"), logsign)
ret := SendEmail(TestEmailMessage, beego.AppConfig.String("Default_emails"), beego.AppConfig.String("Email_title"), logsign)
c.Data["json"] = ret
case "7moordx":
MobileMessage := "PrometheusAlertCenter测试告警"
Expand Down Expand Up @@ -93,7 +93,7 @@ func (c *MainController) AlertTest() {
c.ServeJSON()
}

//markdown test
// markdown test
func (c *MainController) MarkdownTest() {
if !CheckAccount(c.Ctx) {
c.Redirect("/login", 302)
Expand Down Expand Up @@ -123,7 +123,7 @@ func (c *MainController) MarkdownTest() {

}

//test page
// test page
func (c *MainController) Test() {
if !CheckAccount(c.Ctx) {
c.Redirect("/login", 302)
Expand Down
4 changes: 2 additions & 2 deletions controllers/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// SendEmail
func SendEmail(EmailBody, Emails, logsign string) string {
func SendEmail(EmailBody, Emails, EmailTitle, logsign string) string {
open := beego.AppConfig.String("open-email")
if open != "1" {
logs.Info(logsign, "[email]", "email未配置未开启状态,请先配置open-email为1")
Expand All @@ -20,7 +20,7 @@ func SendEmail(EmailBody, Emails, logsign string) string {
serverPort, _ := beego.AppConfig.Int("Email_port")
fromEmail := beego.AppConfig.String("Email_user")
Passwd := beego.AppConfig.String("Email_password")
EmailTitle := beego.AppConfig.String("Email_title")
//EmailTitle := beego.AppConfig.String("Email_title")
//Emails= [email protected],[email protected],[email protected]
SendToEmails := []string{}
m := gomail.NewMessage()
Expand Down
2 changes: 1 addition & 1 deletion controllers/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func sendGitlabEvent(typeid int, event GitlabEvent, eventType, logsign, sendURL
if sendURL == "" {
sendURL = beego.AppConfig.String("Default_emails")
}
SendEmail(EmailMessage, sendURL, logsign)
SendEmail(EmailMessage, sendURL, beego.AppConfig.String("Email_title"), logsign)
// 2 dingding robot
case 2:
DDtext := genDDtext(event, eventType)
Expand Down
8 changes: 5 additions & 3 deletions controllers/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,16 @@ func SendMessageR(message Prometheus, rwxurl, rddurl, rfsurl, rphone, remail, rg
//发送消息到Email
if remail == "" && RMessage.Annotations.Email == "" {
Emails := beego.AppConfig.String("Default_emails")
SendEmail(EmailMessage, Emails, logsign)
EmailTitle := beego.AppConfig.String("Email_title")
SendEmail(EmailMessage, Emails, EmailTitle, logsign)
} else {
EmailTitle := beego.AppConfig.String("Email_title")
if remail != "" {
SendEmail(EmailMessage, remail, logsign)
SendEmail(EmailMessage, remail, EmailTitle, logsign)
}
if RMessage.Annotations.Email != "" {
Emails := RMessage.Annotations.Email
SendEmail(EmailMessage, Emails, logsign)
SendEmail(EmailMessage, Emails, EmailTitle, logsign)
}
}
//发送消息到短信
Expand Down
4 changes: 3 additions & 1 deletion controllers/prometheusalert.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type PrometheusAlertMsg struct {
WebHookUrl string
ToUser string
Email string
EmailTitle string
ToParty string
ToTag string
GroupId string
Expand Down Expand Up @@ -141,6 +142,7 @@ func (c *PrometheusAlertController) PrometheusAlert() {
pMsg.ToUser = checkURL(c.Input().Get("wxuser"), beego.AppConfig.String("WorkWechat_ToUser"))
pMsg.ToParty = checkURL(c.Input().Get("wxparty"), beego.AppConfig.String("WorkWechat_ToUser"))
pMsg.ToTag = checkURL(c.Input().Get("wxtag"), beego.AppConfig.String("WorkWechat_ToUser"))
pMsg.EmailTitle = checkURL(c.Input().Get("emailtitle"), beego.AppConfig.String("Email_title"))

// dd, wx, fsv2 的 at 格式不一样,放在告警组里不好处理和组装。
pMsg.AtSomeOne = c.Input().Get("at")
Expand Down Expand Up @@ -520,7 +522,7 @@ func SendMessagePrometheusAlert(message string, pmsg *PrometheusAlertMsg, logsig
ReturnMsg += Post7MOORphonecall(message, pmsg.Phone, logsign)
//邮件
case "email":
ReturnMsg += SendEmail(message, pmsg.Email, logsign)
ReturnMsg += SendEmail(message, pmsg.Email, pmsg.EmailTitle, logsign)
// Telegram
case "tg":
ReturnMsg += SendTG(message, logsign)
Expand Down
13 changes: 10 additions & 3 deletions doc/readme/system-var.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@
`示例: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`


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

#### 6.模版数据等信息均存储在程序目录的下的`db/PrometheusAlertDB.db`中。
注意:此参数如设置为`split=false`,则PrometheusAlert web页面的路由和告警记录等功能将自动关闭,请谨慎。

`示例:http://[prometheusalert_url]:8080/prometheusalert?type=email&tpl=prometheus-email&[email protected]&emailtitle=运维监控中心`


#### 6.自定义模板使用的是go语言的template模版,可以参考默认模版的一些语法来进行自定义。

#### 7.模版数据等信息均存储在程序目录的下的`db/PrometheusAlertDB.db`中。

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

0 comments on commit 8a63cf8

Please sign in to comment.