Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/davidsiefert/consul-alerts
Browse files Browse the repository at this point in the history
…into davidsiefert-master
  • Loading branch information
dexter committed Jan 22, 2016
2 parents 947f0ad + bdb44b8 commit 057af8d
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,20 @@ prefix: `consul-alerts/config/notifiers/opsgenie/`
| cluster-name | The name of the cluster. [Default: "Consul Alerts"] |
| api-key | API Key (mandatory) |

#### Amazon Web Services Simple Notification Service ("SNS")

To enable AWS SNS builtin notifier, set
`consul-alerts/config/notifiers/awssns/enabled` to `true`. AWS SNS details
needs to be configured.

prefix: `consul-alerts/config/notifiers/awssns/`

| key | description |
|--------------|-----------------------------------------------------|
| enabled | Enable the AWS SNS notifier. [Default: false] |
| region | AWS Region (mandatory) |
| topic-arn | Topic ARN to publish to. (mandatory) |

Health Check via API
--------------------

Expand Down
8 changes: 8 additions & 0 deletions consul-alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ func builtinNotifiers() []notifier.Notifier {
pagerdutyConfig := consulClient.PagerDutyConfig()
hipchatConfig := consulClient.HipChatConfig()
opsgenieConfig := consulClient.OpsGenieConfig()
awssnsConfig := consulClient.AwsSnsConfig()

notifiers := []notifier.Notifier{}
if emailConfig.Enabled {
Expand Down Expand Up @@ -266,6 +267,13 @@ func builtinNotifiers() []notifier.Notifier {
}
notifiers = append(notifiers, opsgenieNotifier)
}
if awssnsConfig.Enabled {
awssnsNotifier := &notifier.AwsSnsNotifier{
Region: awssnsConfig.Region,
TopicArn: awssnsConfig.TopicArn,
}
notifiers = append(notifiers, awssnsNotifier)
}

return notifiers
}
12 changes: 12 additions & 0 deletions consul/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ func (c *ConsulAlertClient) LoadConfig() {
case "consul-alerts/config/notifiers/opsgenie/api-key":
valErr = loadCustomValue(&config.Notifiers.OpsGenie.ApiKey, val, ConfigTypeString)

// AwsSns notifier config
case "consul-alerts/config/notifiers/awssns/enabled":
valErr = loadCustomValue(&config.Notifiers.AwsSns.Enabled, val, ConfigTypeBool)
case "consul-alerts/config/notifiers/awssns/region":
valErr = loadCustomValue(&config.Notifiers.AwsSns.Region, val, ConfigTypeString)
case "consul-alerts/config/notifiers/awssns/topic-arn":
valErr = loadCustomValue(&config.Notifiers.AwsSns.TopicArn, val, ConfigTypeString)

}

if valErr != nil {
Expand Down Expand Up @@ -362,6 +370,10 @@ func (c *ConsulAlertClient) OpsGenieConfig() *OpsGenieNotifierConfig {
return c.config.Notifiers.OpsGenie
}

func (c *ConsulAlertClient) AwsSnsConfig() *AwsSnsNotifierConfig {
return c.config.Notifiers.AwsSns
}

func (c *ConsulAlertClient) registerHealthCheck(key string, health *Check) {

log.Printf(
Expand Down
13 changes: 13 additions & 0 deletions consul/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type NotifiersConfig struct {
PagerDuty *PagerDutyNotifierConfig
HipChat *HipChatNotifierConfig
OpsGenie *OpsGenieNotifierConfig
AwsSns *AwsSnsNotifierConfig
Custom []string
}

Expand Down Expand Up @@ -116,6 +117,12 @@ type OpsGenieNotifierConfig struct {
ApiKey string
}

type AwsSnsNotifierConfig struct {
Enabled bool
Region string
TopicArn string
}

type Status struct {
Current string
CurrentTimestamp time.Time
Expand Down Expand Up @@ -146,6 +153,7 @@ type Consul interface {
PagerDutyConfig() *PagerDutyNotifierConfig
HipChatConfig() *HipChatNotifierConfig
OpsGenieConfig() *OpsGenieNotifierConfig
AwsSnsConfig() *AwsSnsNotifierConfig

CheckChangeThreshold() int
UpdateCheckData()
Expand Down Expand Up @@ -214,6 +222,10 @@ func DefaultAlertConfig() *ConsulAlertConfig {
ClusterName: "Consul-Alerts",
}

awsSns := &AwsSnsNotifierConfig{
Enabled: false,
}

notifiers := &NotifiersConfig{
Email: email,
Log: log,
Expand All @@ -222,6 +234,7 @@ func DefaultAlertConfig() *ConsulAlertConfig {
PagerDuty: pagerduty,
HipChat: hipchat,
OpsGenie: opsgenie,
AwsSns: awsSns,
Custom: []string{},
}

Expand Down
62 changes: 62 additions & 0 deletions notifier/aws-sns-notifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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 {
Region string
TopicArn string
}

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
}

0 comments on commit 057af8d

Please sign in to comment.