Skip to content

Commit

Permalink
Add mail alert
Browse files Browse the repository at this point in the history
  • Loading branch information
cypherean authored and vrongmeal committed Oct 26, 2020
1 parent b733410 commit af15bff
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
google.golang.org/grpc v1.27.1
google.golang.org/protobuf v1.25.0
gopkg.in/ini.v1 v1.52.0 // indirect
gopkg.in/mail.v2 v2.3.1
gorm.io/driver/postgres v1.0.0
gorm.io/gorm v1.20.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.52.0 h1:j+Lt/M1oPPejkniCg1TkWE2J3Eh1oZTsHSXzMTzUXn4=
gopkg.in/ini.v1 v1.52.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/mail.v2 v2.3.1 h1:WYFn/oANrAGP2C0dcV6/pbkPzv8yGzqTjPmTeO7qoXk=
gopkg.in/mail.v2 v2.3.1/go.mod h1:htwXN1Qh09vZJ1NVKxQqHPBaCBbzKhp5GzuJEA4VJWw=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
4 changes: 2 additions & 2 deletions pkg/alerter/discord/alerter.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func (a *Alerter) alert(ctx context.Context, metric checker.Metric, alt alerter.
if err != nil {
return fmt.Errorf("cannot read response: %v", err)
}
if len(buf.String()) > 0 {
return fmt.Errorf(buf.String())
if buf.Len() > 0 {
return fmt.Errorf("invalid response from discord: %s", buf.String())
}

return nil
Expand Down
100 changes: 100 additions & 0 deletions pkg/alerter/mail/alerter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) 2020 SDSLabs
// Use of this source code is governed by an MIT license
// details of which can be found in the LICENSE file.

package mail

import (
"context"
"fmt"

gomail "gopkg.in/mail.v2"

"github.com/sdslabs/pinger/pkg/alerter"
"github.com/sdslabs/pinger/pkg/appcontext"
"github.com/sdslabs/pinger/pkg/checker"

"github.com/sirupsen/logrus"
)

// serviceName is the name of the service used to send the alert.
const serviceName = "mail"

func init() {
alerter.Register(serviceName, func() alerter.Alerter { return new(Alerter) })
}

// senderDetails stores the config for sending E-mail.
type senderDetails struct {
Host string
Port uint16
User string
Secret string
}

// Alerter sends an alert for test status.
type Alerter struct {
log *logrus.Logger
sender senderDetails
}

// Provision initializes required fields for a's execution.
func (a *Alerter) Provision(ctx *appcontext.Context, prov alerter.Provider) error {
a.log = ctx.Logger()
a.sender = senderDetails{prov.GetHost(), prov.GetPort(), prov.GetUser(), prov.GetSecret()}
return nil
}

// Alert sends the notification on slack.
func (a *Alerter) Alert(ctx context.Context, metrics []checker.Metric, amap map[string]alerter.Alert) error {
for i := range metrics {
metric := metrics[i]
alt, ok := amap[metric.GetCheckID()]
if !ok {
continue
}

if err := a.alert(ctx, metric, alt); err != nil {
a.log.Errorf("check %s: %v", metric.GetCheckID(), err)
continue
}
}

return nil
}

// alert sends an individual notification.
func (a *Alerter) alert(ctx context.Context, metric checker.Metric, alt alerter.Alert) error {
var msg string
if metric.IsSuccessful() {
msg = fmt.Sprintf("%s is back up", metric.GetCheckName())
} else {
msg = fmt.Sprintf("%s is down", metric.GetCheckName())
if metric.IsTimeout() {
msg = fmt.Sprintf("%s: timeout", metric.GetCheckName())
}
}

// Receiver's data
to := alt.GetTarget()

// Set E-mail
m := gomail.NewMessage()
m.SetHeader("From", a.sender.User)
m.SetHeader("To", to)
m.SetHeader("Subject", "Pinger Alert: "+msg)
m.SetBody("text/plain", msg)

// Settings for SMTP server
d := gomail.NewDialer(a.sender.Host, int(a.sender.Port), a.sender.User, a.sender.Secret)

// Sending E-Mail
if err := d.DialAndSend(m); err != nil {
return fmt.Errorf("could not send request: %v", err)
}

return nil
}

// Interface guard.
var _ alerter.Alerter = (*Alerter)(nil)
6 changes: 6 additions & 0 deletions pkg/alerter/mail/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) 2020 SDSLabs
// Use of this source code is governed by an MIT license
// details of which can be found in the LICENSE file.

// Package mail implements the mail alerter.
package mail
1 change: 1 addition & 0 deletions pkg/components/agent/alerter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ package agent
import (
// Register all the metrics alerters here.
_ "github.com/sdslabs/pinger/pkg/alerter/discord"
_ "github.com/sdslabs/pinger/pkg/alerter/mail"
_ "github.com/sdslabs/pinger/pkg/alerter/slack"
)

0 comments on commit af15bff

Please sign in to comment.