forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamazon_sns.go
151 lines (133 loc) · 4.47 KB
/
amazon_sns.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package notifiers
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/statping/statping/types/null"
"github.com/statping/statping/types/failures"
"github.com/statping/statping/types/notifications"
"github.com/statping/statping/types/notifier"
"github.com/statping/statping/types/services"
)
var _ notifier.Notifier = (*amazonSNS)(nil)
type amazonSNS struct {
*notifications.Notification
}
func (g *amazonSNS) Select() *notifications.Notification {
return g.Notification
}
func (g *amazonSNS) Valid(values notifications.Values) error {
return nil
}
var AmazonSNS = &amazonSNS{¬ifications.Notification{
Method: "amazon_sns",
Title: "Amazon SNS",
Description: "Use amazonSNS to receive push notifications. Add your amazonSNS URL and App Token to receive notifications.",
Author: "Hunter Long",
AuthorUrl: "https://github.com/hunterlong",
Icon: "fab fa-amazon",
Delay: 5 * time.Second,
Limits: 60,
SuccessData: null.NewNullString(`{{.Service.Name}} is back online and was down for {{.Service.Downtime.Human}}`),
FailureData: null.NewNullString(`{{.Service.Name}} is offline and has been down for {{.Service.Downtime.Human}}`),
DataType: "html",
Form: []notifications.NotificationForm{{
Type: "text",
Title: "AWS Access Token",
DbField: "api_key",
Placeholder: "AKPMED5XUXSEU3O5AB6M",
Required: true,
}, {
Type: "text",
Title: "AWS Secret Key",
DbField: "api_secret",
Placeholder: "39eAZODxEosHRgzLx173ttX9sCtJVOE8rzElRE9B",
Required: true,
}, {
Type: "text",
Title: "Region",
SmallText: "Amazon Region for SNS",
DbField: "var1",
Placeholder: "us-west-2",
Required: true,
}, {
Type: "text",
Title: "SNS Topic ARN",
SmallText: "The ARN of the Topic",
DbField: "Host",
Placeholder: "arn:aws:sns:us-west-2:123456789012:YourTopic",
Required: true,
}}},
}
func valToAttr(val interface{}) *sns.MessageAttributeValue {
dataType := "String"
switch val.(type) {
case string, bool:
dataType = "String"
case int, int64, uint, uint64, uint32:
dataType = "Number"
}
return &sns.MessageAttributeValue{
DataType: aws.String(dataType),
StringValue: aws.String(fmt.Sprintf("%v", val)),
}
}
func messageAttributesSNS(s services.Service, f failures.Failure) map[string]*sns.MessageAttributeValue {
attr := make(map[string]*sns.MessageAttributeValue)
attr["service_id"] = valToAttr(s.Id)
attr["online"] = valToAttr(s.Online)
attr["downtime_milliseconds"] = valToAttr(s.Downtime().Milliseconds())
if f.Id != 0 {
attr["failure_issue"] = valToAttr(f.Issue)
attr["failure_reason"] = valToAttr(f.Reason)
attr["failure_status_code"] = valToAttr(f.ErrorCode)
attr["failure_ping"] = valToAttr(f.PingTime)
}
return attr
}
// Send will send a HTTP Post to the amazonSNS API. It accepts type: string
func (g *amazonSNS) sendMessage(msg string, s services.Service, f failures.Failure) (string, error) {
creds := credentials.NewStaticCredentials(g.ApiKey.String, g.ApiSecret.String, "")
c := aws.NewConfig()
c.Credentials = creds
c.Region = aws.String(g.Var1.String)
sess, err := session.NewSession(c)
if err != nil {
return "", err
}
client := sns.New(sess)
input := &sns.PublishInput{
Message: aws.String(msg),
TopicArn: aws.String(g.Host.String),
MessageAttributes: messageAttributesSNS(s, f),
}
result, err := client.Publish(input)
if err != nil {
return "", err
}
return result.String(), nil
}
// OnFailure will trigger failing service
func (g *amazonSNS) OnFailure(s services.Service, f failures.Failure) (string, error) {
msg := ReplaceVars(g.FailureData.String, s, f)
return g.sendMessage(msg, s, f)
}
// OnSuccess will trigger successful service
func (g *amazonSNS) OnSuccess(s services.Service) (string, error) {
msg := ReplaceVars(g.SuccessData.String, s, failures.Failure{})
return g.sendMessage(msg, s, failures.Failure{})
}
// OnTest will test the amazonSNS notifier
func (g *amazonSNS) OnTest() (string, error) {
s := services.Example(true)
f := failures.Example()
msg := ReplaceVars(`This is a test SNS notification from Statping. Service: {{.Service.Name}} - Downtime: {{.Service.Downtime.Human}}`, s, f)
return g.sendMessage(msg, s, f)
}
// OnSave will trigger when this notifier is saved
func (g *amazonSNS) OnSave() (string, error) {
return "", nil
}