forked from AcalephStorage/consul-alerts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
aws-sns-notifier.go
73 lines (61 loc) · 1.77 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
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 {
Enabled bool
Region string `json:"region"`
TopicArn string `json:"topic-arn"`
}
// 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 := MakeSubject(messages)
body := MakeBody(messages)
return awssns.Send(subject, body)
}
func 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 MakeBody(messages Messages) string {
body := ""
for _, message := range messages {
body += fmt.Sprintf("\n%s:%s:%s is %s.", message.Node, message.Service, message.Check, message.Status)
}
return body
}
func (awssns *AwsSnsNotifier) Send(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
}