From 56fd1da6417d60196c2d845c679e8582e3b7da1f Mon Sep 17 00:00:00 2001 From: doudoustrongly Date: Thu, 3 Aug 2023 15:35:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=82=AE=E4=BB=B6=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=EF=BC=9A=201.=E6=94=AF=E6=8C=81API=E5=8F=91=E9=80=81?= =?UTF-8?q?=E6=8A=84=E9=80=81=E4=BA=BA(email=5Fcc)=E5=92=8C=E9=82=AE?= =?UTF-8?q?=E4=BB=B6=E6=A0=87=E9=A2=98=E5=8F=82=E6=95=B0(email=5Ftitle)=20?= =?UTF-8?q?2.=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E5=A2=9E=E5=8A=A0Email?= =?UTF-8?q?=5Fcc=E9=85=8D=E7=BD=AE=EF=BC=8C=E9=BB=98=E8=AE=A4=E7=A9=BA?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conf/app-example.conf | 2 ++ controllers/WebTest.go | 2 +- controllers/default.go | 2 +- controllers/email.go | 19 +++++++++++++++++-- controllers/gitlab.go | 2 +- controllers/prometheus.go | 6 +++--- controllers/prometheusalert.go | 7 ++++++- 7 files changed, 31 insertions(+), 9 deletions(-) diff --git a/conf/app-example.conf b/conf/app-example.conf index a8eee28c..5084cf3b 100644 --- a/conf/app-example.conf +++ b/conf/app-example.conf @@ -165,6 +165,8 @@ Email_password=xxxxxx Email_title=运维告警 #默认发送邮箱 Default_emails=xxxxx@qq.com,xxxxx@qq.com +#默认抄送邮箱,xxxxx@qq.com,xxxxx@qq.com +Email_cc= #---------------------↓七陌云接口----------------------- #是否开启七陌短信告警通道,可同时开始多个通道0为关闭,1为开启 diff --git a/controllers/WebTest.go b/controllers/WebTest.go index a78cc401..82f3727b 100644 --- a/controllers/WebTest.go +++ b/controllers/WebTest.go @@ -49,7 +49,7 @@ func (c *MainController) AlertTest() {

PrometheusAlert邮件告警测试

欢迎使用PrometheusAlert
` - ret := SendEmail(TestEmailMessage, beego.AppConfig.String("Default_emails"), logsign) + ret := SendEmail("",TestEmailMessage, beego.AppConfig.String("Default_emails"),"", logsign) c.Data["json"] = ret case "7moordx": MobileMessage := "PrometheusAlertCenter测试告警" diff --git a/controllers/default.go b/controllers/default.go index 60d846ad..565c4bb7 100644 --- a/controllers/default.go +++ b/controllers/default.go @@ -30,7 +30,7 @@ type DashboardJson struct { Graylog int `json:"graylog"` Prometheus int `json:"prometheus"` Prometheusalert int `json:"prometheusalert"` - Aliyun int `json:"prometheusalert"` + Aliyun int `json:"aliyun"` } var ChartsJson DashboardJson diff --git a/controllers/email.go b/controllers/email.go index 90e3af7e..a38421dd 100644 --- a/controllers/email.go +++ b/controllers/email.go @@ -10,8 +10,9 @@ import ( ) // SendEmail -func SendEmail(EmailBody, Emails, logsign string) string { +func SendEmail(Title string, EmailBody, Emails, CcEmails , logsign string) string { open := beego.AppConfig.String("open-email") + logs.Info("SendEmail-Param: EmailTitle:%s, toEmails:%s, CcEmails:%s, EmailBody:%s",Title,Emails,CcEmails,EmailBody) if open != "1" { logs.Info(logsign, "[email]", "email未配置未开启状态,请先配置open-email为1") return "eamil未配置未开启状态,请先配置open-email为1" @@ -20,7 +21,10 @@ 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 := Title + if len(Title) == 0 { + EmailTitle = beego.AppConfig.String("Email_title") + } //Emails= xxx1@qq.com,xxx2@qq.com,xxx3@qq.com SendToEmails := []string{} m := gomail.NewMessage() @@ -32,6 +36,17 @@ func SendEmail(EmailBody, Emails, logsign string) string { } // 收件人,...代表打散列表填充不定参数 m.SetHeader("To", SendToEmails...) + //抄送 + if len(CcEmails) == 0 { + CcEmails = beego.AppConfig.DefaultString("Email_cc","") + } + if len(CcEmails) != 0 { + SendToCcEmails := []string{} + for _, Email := range strings.Split(CcEmails, ",") { + SendToCcEmails = append(SendToCcEmails, strings.TrimSpace(Email)) + } + m.SetHeader("Cc", SendToCcEmails...) + } // 发件人 m.SetAddressHeader("From", fromEmail, EmailTitle) // 主题 diff --git a/controllers/gitlab.go b/controllers/gitlab.go index af760abb..bd7af79b 100644 --- a/controllers/gitlab.go +++ b/controllers/gitlab.go @@ -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, "", logsign) // 2 dingding robot case 2: DDtext := genDDtext(event, eventType) diff --git a/controllers/prometheus.go b/controllers/prometheus.go index 69bcdb48..3129b6c0 100644 --- a/controllers/prometheus.go +++ b/controllers/prometheus.go @@ -260,14 +260,14 @@ 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) + SendEmail("", EmailMessage, Emails,"", logsign) } else { if remail != "" { - SendEmail(EmailMessage, remail, logsign) + SendEmail("", EmailMessage, remail,"", logsign) } if RMessage.Annotations.Email != "" { Emails := RMessage.Annotations.Email - SendEmail(EmailMessage, Emails, logsign) + SendEmail("", EmailMessage, Emails,"", logsign) } } //发送消息到短信 diff --git a/controllers/prometheusalert.go b/controllers/prometheusalert.go index 8e46c69b..9ff1773a 100644 --- a/controllers/prometheusalert.go +++ b/controllers/prometheusalert.go @@ -67,6 +67,8 @@ type PrometheusAlertMsg struct { WebHookUrl string ToUser string Email string + CcEmail string + EmailTitle string ToParty string ToTag string GroupId string @@ -134,6 +136,9 @@ func (c *PrometheusAlertController) PrometheusAlert() { if pMsg.Email == "" { pMsg.Email = beego.AppConfig.String("Default_emails") } + pMsg.CcEmail = c.Input().Get("email_cc") + pMsg.EmailTitle = c.Input().Get("email_title") + pMsg.ToUser = c.Input().Get("wxuser") if pMsg.ToUser == "" { pMsg.ToUser = beego.AppConfig.String("WorkWechat_ToUser") @@ -510,7 +515,7 @@ func SendMessagePrometheusAlert(message string, pmsg *PrometheusAlertMsg, logsig ReturnMsg += Post7MOORphonecall(message, pmsg.Phone, logsign) //邮件 case "email": - ReturnMsg += SendEmail(message, pmsg.Email, logsign) + ReturnMsg += SendEmail(pmsg.EmailTitle , message, pmsg.Email, pmsg.CcEmail, logsign) // Telegram case "tg": ReturnMsg += SendTG(message, logsign) -- Gitee