2021-12-03 03:05:17 +01:00
|
|
|
package email
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/TwiN/gatus/v3/alerting/alert"
|
|
|
|
"github.com/TwiN/gatus/v3/core"
|
|
|
|
gomail "gopkg.in/mail.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AlertProvider is the configuration necessary for sending an alert using SMTP
|
|
|
|
type AlertProvider struct {
|
|
|
|
From string `yaml:"from"`
|
2022-01-12 02:07:25 +01:00
|
|
|
Username string `yaml:"username"`
|
2021-12-03 03:05:17 +01:00
|
|
|
Password string `yaml:"password"`
|
|
|
|
Host string `yaml:"host"`
|
|
|
|
Port int `yaml:"port"`
|
|
|
|
To string `yaml:"to"`
|
|
|
|
|
|
|
|
// DefaultAlert is the default alert configuration to use for endpoints with an alert of the appropriate type
|
|
|
|
DefaultAlert *alert.Alert `yaml:"default-alert,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsValid returns whether the provider's configuration is valid
|
|
|
|
func (provider *AlertProvider) IsValid() bool {
|
|
|
|
return len(provider.From) > 0 && len(provider.Password) > 0 && len(provider.Host) > 0 && len(provider.To) > 0 && provider.Port > 0 && provider.Port < math.MaxUint16
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send an alert using the provider
|
|
|
|
func (provider *AlertProvider) Send(endpoint *core.Endpoint, alert *alert.Alert, result *core.Result, resolved bool) error {
|
2022-01-12 02:07:25 +01:00
|
|
|
var username string
|
|
|
|
if len(provider.Username) > 0 {
|
|
|
|
username = provider.Username
|
|
|
|
} else {
|
|
|
|
username = provider.From
|
|
|
|
}
|
2021-12-03 03:05:17 +01:00
|
|
|
subject, body := provider.buildMessageSubjectAndBody(endpoint, alert, result, resolved)
|
|
|
|
m := gomail.NewMessage()
|
|
|
|
m.SetHeader("From", provider.From)
|
|
|
|
m.SetHeader("To", strings.Split(provider.To, ",")...)
|
|
|
|
m.SetHeader("Subject", subject)
|
|
|
|
m.SetBody("text/plain", body)
|
2022-01-12 02:07:25 +01:00
|
|
|
d := gomail.NewDialer(provider.Host, provider.Port, username, provider.Password)
|
2021-12-03 03:05:17 +01:00
|
|
|
return d.DialAndSend(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildMessageSubjectAndBody builds the message subject and body
|
|
|
|
func (provider *AlertProvider) buildMessageSubjectAndBody(endpoint *core.Endpoint, alert *alert.Alert, result *core.Result, resolved bool) (string, string) {
|
|
|
|
var subject, message, results string
|
|
|
|
if resolved {
|
2021-12-12 22:33:16 +01:00
|
|
|
subject = fmt.Sprintf("[%s] Alert resolved", endpoint.DisplayName())
|
|
|
|
message = fmt.Sprintf("An alert for %s has been resolved after passing successfully %d time(s) in a row", endpoint.DisplayName(), alert.SuccessThreshold)
|
2021-12-03 03:05:17 +01:00
|
|
|
} else {
|
2021-12-12 22:33:16 +01:00
|
|
|
subject = fmt.Sprintf("[%s] Alert triggered", endpoint.DisplayName())
|
|
|
|
message = fmt.Sprintf("An alert for %s has been triggered due to having failed %d time(s) in a row", endpoint.DisplayName(), alert.FailureThreshold)
|
2021-12-03 03:05:17 +01:00
|
|
|
}
|
|
|
|
for _, conditionResult := range result.ConditionResults {
|
|
|
|
var prefix string
|
|
|
|
if conditionResult.Success {
|
|
|
|
prefix = "✅"
|
|
|
|
} else {
|
|
|
|
prefix = "❌"
|
|
|
|
}
|
|
|
|
results += fmt.Sprintf("%s %s\n", prefix, conditionResult.Condition)
|
|
|
|
}
|
|
|
|
var description string
|
|
|
|
if alertDescription := alert.GetDescription(); len(alertDescription) > 0 {
|
|
|
|
description = "\n\nAlert description: " + alertDescription
|
|
|
|
}
|
|
|
|
return subject, message + description + "\n\nCondition results:\n" + results
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDefaultAlert returns the provider's default alert configuration
|
|
|
|
func (provider AlertProvider) GetDefaultAlert() *alert.Alert {
|
|
|
|
return provider.DefaultAlert
|
|
|
|
}
|