-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhandler.go
More file actions
141 lines (118 loc) · 3.63 KB
/
Copy pathhandler.go
File metadata and controls
141 lines (118 loc) · 3.63 KB
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
package main
import (
"fmt"
"net"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/darkcrux/gopherduty"
"github.com/hashicorp/consul/api"
"github.com/nlopes/slack"
"gopkg.in/gomail.v2"
)
// AlertHandlers are responsible for alerting to some external endpoint
// when given an alert (email, pagerduty, etc)
type AlertHandler interface {
Alert(datacenter string, alert *AlertState)
}
type StdoutHandler struct {
LogLevel string `mapstructure:"log_level"`
logger *log.Logger
}
func (handler StdoutHandler) Alert(datacenter string, alert *AlertState) {
text := []string{alert.Message}
if alert.Details != "" {
text = append(text, strings.Split(alert.Details, "\n")...)
}
for _, line := range text {
switch strings.ToLower(handler.LogLevel) {
case "panic":
handler.logger.Panic(line)
case "fatal":
handler.logger.Fatal(line)
case "error":
handler.logger.Error(line)
case "warn", "warning":
handler.logger.Warn(line)
case "info":
handler.logger.Info(line)
case "debug":
handler.logger.Debug(line)
}
}
}
type EmailHandler struct {
Recipients []string `mapstructure:"recipients"`
MaxRetries int `mapstructure:"max_retries"`
}
func (handler EmailHandler) Alert(datacenter string, alert *AlertState) {
for _, recipient := range handler.Recipients {
// Get the mail server to use for this recipient
records, err := net.LookupMX(strings.Split(recipient, "@")[1])
if err != nil {
log.Error("Error looking up email server: ", err)
continue
}
m := gomail.NewMessage()
m.SetAddressHeader("From", "consul-alerting@noreply.com", "Consul Alerting")
m.SetAddressHeader("To", recipient, "")
m.SetHeader("Subject", alert.Message)
m.SetBody("text/plain", alert.Details)
d := gomail.NewPlainDialer(records[0].Host, 25, "", "")
tries := 0
for tries <= handler.MaxRetries {
if err := d.DialAndSend(m); err != nil {
log.Error("Error sending alert email: ", err)
log.Error("Retrying email in 5s...")
time.Sleep(5 * time.Second)
tries++
} else {
break
}
}
}
}
type PagerdutyHandler struct {
ServiceKey string `mapstructure:"service_key"`
MaxRetries int `mapstructure:"max_retries"`
}
func (handler PagerdutyHandler) Alert(datacenter string, alert *AlertState) {
client := gopherduty.NewClient(handler.ServiceKey)
client.MaxRetry = handler.MaxRetries
// This key needs to be unique to the datacenter and service/node we're alerting on
incidentKey := datacenter + "-" + alert.Service + "-" + alert.Tag + "-" + alert.Node
var resp *gopherduty.PagerDutyResponse
if alert.Status != api.HealthPassing {
resp = client.Trigger(incidentKey, alert.Message, "", "", alert.Details)
} else {
resp = client.Resolve(incidentKey, alert.Message, alert.Details)
}
for _, err := range resp.Errors {
log.Errorf("Error sending alert to PagerDuty: %v (details: %v, message: %v)", err, alert.Details, alert.Message)
}
}
type SlackHandler struct {
Token string `mapstructure:"api_token"`
ChannelName string `mapstructure:"channel_name"`
MaxRetries int `mapstructure:"max_retries"`
}
const slackMessageFormat = `
*%s*
%s
`
func (handler SlackHandler) Alert(datacenter string, alert *AlertState) {
api := slack.New(handler.Token)
message := fmt.Sprintf(slackMessageFormat, alert.Message, alert.Details)
tries := 0
for tries <= handler.MaxRetries {
_, _, err := api.PostMessage(handler.ChannelName, message, slack.PostMessageParameters{})
if err != nil {
log.Errorf("Error sending alert to Slack (channel: %s): %s", handler.ChannelName, err)
log.Errorf("Retrying alert to slack in 5s...")
time.Sleep(5 * time.Second)
} else {
break
}
tries++
}
}