diff --git a/.gitignore b/.gitignore index 474de98..d8b48c7 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ go.work sysmonitord.code-workspace data/ +config.yaml diff --git a/go.mod b/go.mod index 88ccbd4..6271fb8 100644 --- a/go.mod +++ b/go.mod @@ -23,5 +23,7 @@ require ( go.uber.org/zap v1.27.1 // indirect golang.org/x/sys v0.29.0 // indirect golang.org/x/term v0.28.0 // indirect + gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect + gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 216a5db..399cab5 100644 --- a/go.sum +++ b/go.sum @@ -50,6 +50,10 @@ golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.28.0 h1:/Ts8HFuMR2E6IP/jlo7QVLZHggjKQbhu/7H0LJFr3Gg= golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= +gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE= +gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/notifier/alert_manager.go b/internal/notifier/alert_manager.go index 5f7e47b..18a77e0 100644 --- a/internal/notifier/alert_manager.go +++ b/internal/notifier/alert_manager.go @@ -34,7 +34,7 @@ func NewAlerter(cfg config.NotificationConfig) *Alerter { mailer: mail.NewMailer(cfg.Email), eventChan: make(chan AlertEvent, 100), buffer: make([]AlertEvent, 0), - interval: 1 * time.Minute, // Todo: 可配置化 + interval: 30 * time.Second, // Todo: 可配置化 } } @@ -63,6 +63,7 @@ func (a *Alerter) loop() { logger.Log.Debug("[notifier] 收到告警,加入待发送序列", zap.String("path", event.Path)) case <-a.timer.C: + logger.Log.Debug("[notifier] 定时检查告警事件,准备发送", zap.Int("count", len(a.buffer))) a.mu.Lock() if len(a.buffer) > 0 { a.sendAlert() diff --git a/internal/notifier/mail/mailer.go b/internal/notifier/mail/mailer.go index d76fa05..cd28556 100644 --- a/internal/notifier/mail/mailer.go +++ b/internal/notifier/mail/mailer.go @@ -2,11 +2,11 @@ package mail import ( "fmt" - "net/smtp" "sysmonitord/internal/config" "sysmonitord/pkg/logger" "go.uber.org/zap" + "gopkg.in/gomail.v2" ) type Mailer struct { @@ -23,28 +23,35 @@ func (m *Mailer) Send(subject, body string) error { return nil } - headers := make(map[string]string) - headers["From"] = m.cfg.SMTP.Username - headers["To"] = m.cfg.Recipients[0] - headers["Subject"] = subject - - message := "" - for k, v := range headers { - message += fmt.Sprintf("%s: %s\r\n", k, v) + if m.cfg.SMTP.Server == "" || m.cfg.SMTP.Port == 0 { + logger.Log.Error("[notifier] SMTP配置缺失", + zap.String("server", m.cfg.SMTP.Server), + zap.Int("port", m.cfg.SMTP.Port), + ) + return fmt.Errorf("SMTP配置缺失") } - message += "\r\n" + body - auth := smtp.PlainAuth("", m.cfg.SMTP.Username, m.cfg.SMTP.Password, m.cfg.SMTP.Server) - addr := fmt.Sprintf("%s:%d", m.cfg.SMTP.Server, m.cfg.SMTP.Port) + msg := gomail.NewMessage() + msg.SetHeader("From", m.cfg.SMTP.Username) + msg.SetHeader("To", m.cfg.Recipients...) + msg.SetHeader("Subject", subject) + msg.SetBody("text/plain", body) - logger.Log.Info("[notifier] 发送邮件通知", zap.String("subject", subject), zap.String("to", m.cfg.Recipients[0])) + d := gomail.NewDialer( + m.cfg.SMTP.Server, + m.cfg.SMTP.Port, + m.cfg.SMTP.Username, + m.cfg.SMTP.Password, + ) - err := smtp.SendMail(addr, auth, m.cfg.SMTP.Username, m.cfg.Recipients, []byte(message)) - if err != nil { - logger.Log.Error("[notifier] 发送邮件失败", zap.Error(err)) + if m.cfg.SMTP.Port == 465 { + d.SSL = true + } + + if err := d.DialAndSend(msg); err != nil { + logger.Log.Error("[notifier] 邮件发送失败", zap.Error(err)) return err } - - logger.Log.Info("[notifier] 邮件发送成功") + logger.Log.Info("[notifier] 邮件发送成功", zap.Strings("recipients", m.cfg.Recipients)) return nil }