forked from AcalephStorage/consul-alerts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws-sns-notifier.go
90 lines (76 loc) · 2.43 KB
/
aws-sns-notifier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package notifier
import (
"fmt"
log "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/Sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
)
type AwsSnsNotifier struct {
ClusterName string `json:"cluster-name"`
Enabled bool
Region string `json:"region"`
TopicArn string `json:"topic-arn"`
Template string `json:"template"`
}
// NotifierName provides name for notifier selection
func (awssns *AwsSnsNotifier) NotifierName() string {
return "awssns"
}
func (awssns *AwsSnsNotifier) Copy() Notifier {
notifier := *awssns
return ¬ifier
}
func (awssns *AwsSnsNotifier) Notify(messages Messages) bool {
subject := awssns.makeSubject(messages)
body := awssns.makeBody(messages)
return sendSNS(awssns, subject, body)
}
func (awssns *AwsSnsNotifier) makeSubject(messages Messages) string {
overallStatus, pass, warn, fail := messages.Summary()
return fmt.Sprintf("%s--Fail: %d, Warn: %d, Pass: %d", overallStatus, fail, warn, pass)
}
func (awssns *AwsSnsNotifier) makeBody(messages Messages) string {
overallStatus, pass, warn, fail := messages.Summary()
t := TemplateData{
ClusterName: awssns.ClusterName,
SystemStatus: overallStatus,
FailCount: fail,
WarnCount: warn,
PassCount: pass,
Nodes: mapByNodes(messages),
}
body, err := renderTemplate(t, awssns.Template, snsDefaultTemplate)
if err != nil {
log.Println("Template error, unable to send email notification: ", err)
return fmt.Sprintf("error rendering template %v", err)
} else {
return body
}
}
var sendSNS = func(awssns *AwsSnsNotifier, subject string, message string) bool {
svc := sns.New(session.New(&aws.Config{
Region: aws.String(awssns.Region),
}))
params := &sns.PublishInput{
Message: aws.String(message),
MessageAttributes: map[string]*sns.MessageAttributeValue{
"Key": {
DataType: aws.String("String"),
StringValue: aws.String("String"),
},
},
MessageStructure: aws.String("messageStructure"),
Subject: aws.String(subject),
TopicArn: aws.String(awssns.TopicArn),
}
resp, err := svc.Publish(params)
if err != nil {
log.Println(err.Error())
return false
}
log.Println(resp)
return true
}
var snsDefaultTemplate string = `
{{ range $name, $checks := .Nodes }}{{ range $check := $checks }}{{ $name }}:{{$check.Service}}:{{$check.Check}} is {{$check.Status}}.{{ end }}{{ end }}`