2013-07-30 11:19:18 +00:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-03-12 04:45:25 +00:00
|
|
|
"crypto/tls"
|
2013-07-30 11:19:18 +00:00
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2015-04-10 17:12:24 +00:00
|
|
|
"html"
|
2013-08-16 12:50:51 +00:00
|
|
|
"io"
|
2013-07-30 11:19:18 +00:00
|
|
|
"io/ioutil"
|
2015-03-12 04:45:25 +00:00
|
|
|
"net"
|
2013-07-30 11:19:18 +00:00
|
|
|
"net/http"
|
2013-08-15 07:05:12 +00:00
|
|
|
"net/smtp"
|
2015-03-12 20:01:16 +00:00
|
|
|
"os"
|
2015-03-12 04:45:25 +00:00
|
|
|
"strings"
|
2013-07-30 11:19:18 +00:00
|
|
|
"sync"
|
2013-08-15 07:05:12 +00:00
|
|
|
"text/template"
|
2015-04-10 17:12:24 +00:00
|
|
|
"time"
|
2013-07-30 11:19:18 +00:00
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
"github.com/golang/glog"
|
2015-02-16 20:11:00 +00:00
|
|
|
"github.com/thorduri/pushover"
|
2013-08-27 13:32:08 +00:00
|
|
|
|
2013-08-05 09:49:56 +00:00
|
|
|
pb "github.com/prometheus/alertmanager/config/generated"
|
2013-07-30 11:19:18 +00:00
|
|
|
)
|
|
|
|
|
2015-04-24 13:17:49 +00:00
|
|
|
const (
|
|
|
|
contentTypeJson = "application/json"
|
|
|
|
|
|
|
|
notificationOpTrigger notificationOp = iota
|
|
|
|
notificationOpResolve
|
|
|
|
)
|
2013-07-30 11:19:18 +00:00
|
|
|
|
2015-02-16 19:35:45 +00:00
|
|
|
var bodyTmpl = template.Must(template.New("message").Parse(`From: Prometheus Alertmanager <{{.From}}>
|
|
|
|
To: {{.To}}
|
2015-04-17 18:50:30 +00:00
|
|
|
Date: {{.Date}}
|
2015-04-24 13:17:49 +00:00
|
|
|
Subject: [{{ .Status }}] {{.Alert.Labels.alertname}}: {{.Alert.Summary}}
|
2013-08-15 07:05:12 +00:00
|
|
|
|
2015-02-16 19:35:45 +00:00
|
|
|
{{.Alert.Description}}
|
2013-08-15 07:05:12 +00:00
|
|
|
|
|
|
|
Grouping labels:
|
2015-02-16 19:35:45 +00:00
|
|
|
{{range $label, $value := .Alert.Labels}}
|
2013-08-16 12:50:51 +00:00
|
|
|
{{$label}} = "{{$value}}"{{end}}
|
2013-08-15 07:05:12 +00:00
|
|
|
|
|
|
|
Payload labels:
|
2015-02-16 19:35:45 +00:00
|
|
|
{{range $label, $value := .Alert.Payload}}
|
2013-08-16 12:50:51 +00:00
|
|
|
{{$label}} = "{{$value}}"{{end}}`))
|
2013-08-15 07:05:12 +00:00
|
|
|
|
2013-07-30 11:19:18 +00:00
|
|
|
var (
|
2015-04-14 11:42:09 +00:00
|
|
|
notificationBufferSize = flag.Int("notification.buffer-size", 1000, "Size of buffer for pending notifications.")
|
|
|
|
pagerdutyApiUrl = flag.String("notification.pagerduty.url", "https://events.pagerduty.com/generic/2010-04-15/create_event.json", "PagerDuty API URL.")
|
|
|
|
smtpSmartHost = flag.String("notification.smtp.smarthost", "", "Address of the smarthost to send all email notifications to.")
|
|
|
|
smtpSender = flag.String("notification.smtp.sender", "alertmanager@example.org", "Sender email address to use in email notifications.")
|
|
|
|
hipchatUrl = flag.String("notification.hipchat.url", "https://api.hipchat.com/v2", "HipChat API V2 URL.")
|
2013-07-30 11:19:18 +00:00
|
|
|
)
|
|
|
|
|
2015-04-24 13:17:49 +00:00
|
|
|
type notificationOp int
|
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
// A Notifier is responsible for sending notifications for alerts according to
|
2013-07-30 11:19:18 +00:00
|
|
|
// a provided notification configuration.
|
|
|
|
type Notifier interface {
|
|
|
|
// Queue a notification for asynchronous dispatching.
|
2015-04-24 13:17:49 +00:00
|
|
|
QueueNotification(a *Alert, op notificationOp, configName string) error
|
2013-07-30 11:19:18 +00:00
|
|
|
// Replace current notification configs. Already enqueued messages will remain
|
|
|
|
// unaffected.
|
|
|
|
SetNotificationConfigs([]*pb.NotificationConfig)
|
2013-08-27 13:32:08 +00:00
|
|
|
// Start alert notification dispatch loop.
|
|
|
|
Dispatch()
|
|
|
|
// Stop the alert notification dispatch loop.
|
2013-07-30 11:19:18 +00:00
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request for sending a notification.
|
|
|
|
type notificationReq struct {
|
2013-08-27 13:32:08 +00:00
|
|
|
alert *Alert
|
2013-07-30 11:19:18 +00:00
|
|
|
notificationConfig *pb.NotificationConfig
|
2015-04-24 13:17:49 +00:00
|
|
|
op notificationOp
|
2013-07-30 11:19:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Alert notification multiplexer and dispatcher.
|
|
|
|
type notifier struct {
|
|
|
|
// Notifications that are queued to be sent.
|
|
|
|
pendingNotifications chan *notificationReq
|
|
|
|
|
|
|
|
// Mutex to protect the fields below.
|
|
|
|
mu sync.Mutex
|
|
|
|
// Map of notification configs by name.
|
|
|
|
notificationConfigs map[string]*pb.NotificationConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a new notifier.
|
|
|
|
func NewNotifier(configs []*pb.NotificationConfig) *notifier {
|
|
|
|
notifier := ¬ifier{
|
|
|
|
pendingNotifications: make(chan *notificationReq, *notificationBufferSize),
|
|
|
|
}
|
|
|
|
notifier.SetNotificationConfigs(configs)
|
|
|
|
return notifier
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *notifier) SetNotificationConfigs(configs []*pb.NotificationConfig) {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
|
|
|
|
n.notificationConfigs = map[string]*pb.NotificationConfig{}
|
|
|
|
for _, c := range configs {
|
|
|
|
n.notificationConfigs[c.GetName()] = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 13:17:49 +00:00
|
|
|
func (n *notifier) QueueNotification(a *Alert, op notificationOp, configName string) error {
|
2013-07-30 11:19:18 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
nc, ok := n.notificationConfigs[configName]
|
|
|
|
n.mu.Unlock()
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("No such notification configuration %s", configName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to save a reference to the notification config in the
|
|
|
|
// notificationReq since the config might be replaced or gone at the time the
|
|
|
|
// message gets dispatched.
|
|
|
|
n.pendingNotifications <- ¬ificationReq{
|
2013-08-27 13:32:08 +00:00
|
|
|
alert: a,
|
2013-07-30 11:19:18 +00:00
|
|
|
notificationConfig: nc,
|
2015-04-24 13:17:49 +00:00
|
|
|
op: op,
|
2013-07-30 11:19:18 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-24 13:17:49 +00:00
|
|
|
func (n *notifier) sendPagerDutyNotification(serviceKey string, op notificationOp, a *Alert) error {
|
2013-07-30 11:19:18 +00:00
|
|
|
// http://developer.pagerduty.com/documentation/integration/events/trigger
|
2015-04-24 13:17:49 +00:00
|
|
|
eventType := ""
|
|
|
|
switch op {
|
|
|
|
case notificationOpTrigger:
|
|
|
|
eventType = "trigger"
|
|
|
|
case notificationOpResolve:
|
|
|
|
eventType = "resolve"
|
|
|
|
}
|
2013-08-27 13:32:08 +00:00
|
|
|
incidentKey := a.Fingerprint()
|
2013-07-30 11:19:18 +00:00
|
|
|
buf, err := json.Marshal(map[string]interface{}{
|
|
|
|
"service_key": serviceKey,
|
2015-04-24 13:17:49 +00:00
|
|
|
"event_type": eventType,
|
2013-08-27 13:32:08 +00:00
|
|
|
"description": a.Description,
|
2013-07-30 11:19:18 +00:00
|
|
|
"incident_key": incidentKey,
|
|
|
|
"details": map[string]interface{}{
|
2013-08-27 13:32:08 +00:00
|
|
|
"grouping_labels": a.Labels,
|
|
|
|
"extra_labels": a.Payload,
|
2013-07-30 11:19:18 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.Post(
|
|
|
|
*pagerdutyApiUrl,
|
|
|
|
contentTypeJson,
|
|
|
|
bytes.NewBuffer(buf),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
respBuf, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
glog.Infof("Sent PagerDuty notification: %v: HTTP %d: %s", incidentKey, resp.StatusCode, respBuf)
|
2013-07-30 11:19:18 +00:00
|
|
|
// BUG: Check response for result of operation.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-24 13:17:49 +00:00
|
|
|
func (n *notifier) sendHipChatNotification(op notificationOp, config *pb.HipChatConfig, a *Alert) error {
|
2015-04-10 17:12:24 +00:00
|
|
|
// https://www.hipchat.com/docs/apiv2/method/send_room_notification
|
|
|
|
incidentKey := a.Fingerprint()
|
2015-04-24 13:17:49 +00:00
|
|
|
color := ""
|
|
|
|
status := ""
|
|
|
|
switch op {
|
|
|
|
case notificationOpTrigger:
|
|
|
|
color = config.GetColor()
|
|
|
|
status = "firing"
|
|
|
|
case notificationOpResolve:
|
|
|
|
color = config.GetColorResolved()
|
|
|
|
status = "resolved"
|
|
|
|
}
|
2015-04-10 17:12:24 +00:00
|
|
|
buf, err := json.Marshal(map[string]interface{}{
|
|
|
|
"color": color,
|
2015-04-24 13:17:49 +00:00
|
|
|
"message": fmt.Sprintf("<b>%s %s</b>: %s (<a href='%s'>view</a>)", html.EscapeString(a.Labels["alertname"]), status, html.EscapeString(a.Summary), a.Payload["GeneratorURL"]),
|
|
|
|
"notify": config.GetNotify(),
|
2015-04-10 17:12:24 +00:00
|
|
|
"message_format": "html",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout := time.Duration(5 * time.Second)
|
|
|
|
client := http.Client{
|
|
|
|
Timeout: timeout,
|
|
|
|
}
|
|
|
|
resp, err := client.Post(
|
2015-04-24 13:17:49 +00:00
|
|
|
fmt.Sprintf("%s/room/%d/notification?auth_token=%s", *hipchatUrl, config.GetRoomId(), config.GetAuthToken()),
|
2015-04-10 17:12:24 +00:00
|
|
|
contentTypeJson,
|
|
|
|
bytes.NewBuffer(buf),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
respBuf, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.Infof("Sent HipChat notification: %v: HTTP %d: %s", incidentKey, resp.StatusCode, respBuf)
|
|
|
|
// BUG: Check response for result of operation.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-24 13:17:49 +00:00
|
|
|
func writeEmailBody(w io.Writer, from, to, status string, a *Alert) error {
|
|
|
|
return writeEmailBodyWithTime(w, from, to, status, a, time.Now())
|
2015-04-17 18:50:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 13:17:49 +00:00
|
|
|
func writeEmailBodyWithTime(w io.Writer, from, to, status string, a *Alert, moment time.Time) error {
|
2015-02-16 19:35:45 +00:00
|
|
|
err := bodyTmpl.Execute(w, struct {
|
2015-04-24 13:17:49 +00:00
|
|
|
From string
|
|
|
|
To string
|
|
|
|
Date string
|
|
|
|
Alert *Alert
|
|
|
|
Status string
|
2015-02-16 19:35:45 +00:00
|
|
|
}{
|
2015-04-24 13:17:49 +00:00
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Date: moment.Format("Mon, 2 Jan 2006 15:04:05 -0700"),
|
|
|
|
Alert: a,
|
|
|
|
Status: status,
|
2015-02-16 19:35:45 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2013-08-20 14:23:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-08-16 12:50:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-13 15:13:20 +00:00
|
|
|
func getSMTPAuth(hasAuth bool, mechs string) (smtp.Auth, *tls.Config, error) {
|
|
|
|
if !hasAuth {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
username := os.Getenv("SMTP_AUTH_USERNAME")
|
|
|
|
|
|
|
|
for _, mech := range strings.Split(mechs, " ") {
|
|
|
|
switch mech {
|
|
|
|
case "CRAM-MD5":
|
|
|
|
secret := os.Getenv("SMTP_AUTH_SECRET")
|
|
|
|
if secret == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return smtp.CRAMMD5Auth(username, secret), nil, nil
|
|
|
|
case "PLAIN":
|
|
|
|
password := os.Getenv("SMTP_AUTH_PASSWORD")
|
|
|
|
if password == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
identity := os.Getenv("SMTP_AUTH_IDENTITY")
|
|
|
|
|
|
|
|
// We need to know the hostname for both auth and TLS.
|
|
|
|
host, _, err := net.SplitHostPort(*smtpSmartHost)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("invalid address: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := smtp.PlainAuth(identity, username, password, host)
|
|
|
|
cfg := &tls.Config{ServerName: host}
|
|
|
|
return auth, cfg, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2015-04-24 13:17:49 +00:00
|
|
|
func (n *notifier) sendEmailNotification(to string, op notificationOp, a *Alert) error {
|
|
|
|
status := ""
|
|
|
|
switch op {
|
|
|
|
case notificationOpTrigger:
|
|
|
|
status = "ALERT"
|
|
|
|
case notificationOpResolve:
|
|
|
|
status = "RESOLVED"
|
|
|
|
}
|
2013-08-15 07:05:12 +00:00
|
|
|
// Connect to the SMTP smarthost.
|
|
|
|
c, err := smtp.Dial(*smtpSmartHost)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer c.Quit()
|
|
|
|
|
2015-03-12 21:28:28 +00:00
|
|
|
// Authenticate if we and the server are both configured for it.
|
2015-03-13 15:13:20 +00:00
|
|
|
auth, tlsConfig, err := getSMTPAuth(c.Extension("AUTH"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tlsConfig != nil {
|
|
|
|
if err := c.StartTLS(tlsConfig); err != nil {
|
|
|
|
return fmt.Errorf("starttls failed: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if auth != nil {
|
|
|
|
if err := c.Auth(auth); err != nil {
|
|
|
|
return fmt.Errorf("%T failed: %s", auth, err)
|
2015-03-12 04:45:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-15 07:05:12 +00:00
|
|
|
// Set the sender and recipient.
|
|
|
|
c.Mail(*smtpSender)
|
2015-02-16 19:35:45 +00:00
|
|
|
c.Rcpt(to)
|
2013-08-15 07:05:12 +00:00
|
|
|
|
|
|
|
// Send the email body.
|
|
|
|
wc, err := c.Data()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer wc.Close()
|
|
|
|
|
2015-04-24 13:17:49 +00:00
|
|
|
return writeEmailBody(wc, *smtpSender, status, to, a)
|
2013-07-30 11:19:18 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 13:17:49 +00:00
|
|
|
func (n *notifier) sendPushoverNotification(token string, op notificationOp, userKey string, a *Alert) error {
|
2015-02-16 20:11:00 +00:00
|
|
|
po, err := pushover.NewPushover(token, userKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate credentials
|
|
|
|
err = po.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send pushover message
|
|
|
|
_, _, err = po.Push(&pushover.Message{
|
|
|
|
Title: a.Summary,
|
|
|
|
Message: a.Description,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-24 13:17:49 +00:00
|
|
|
func (n *notifier) handleNotification(a *Alert, op notificationOp, config *pb.NotificationConfig) {
|
2013-07-30 11:19:18 +00:00
|
|
|
for _, pdConfig := range config.PagerdutyConfig {
|
2015-04-24 13:17:49 +00:00
|
|
|
if err := n.sendPagerDutyNotification(pdConfig.GetServiceKey(), op, a); err != nil {
|
2013-08-27 13:32:08 +00:00
|
|
|
glog.Error("Error sending PagerDuty notification: ", err)
|
2013-07-30 11:19:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, emailConfig := range config.EmailConfig {
|
2015-04-24 13:17:49 +00:00
|
|
|
if op == notificationOpResolve && !emailConfig.GetSendResolved() {
|
2015-04-27 21:16:48 +00:00
|
|
|
continue
|
2015-04-24 13:17:49 +00:00
|
|
|
}
|
2013-08-15 07:05:12 +00:00
|
|
|
if *smtpSmartHost == "" {
|
2013-08-27 13:32:08 +00:00
|
|
|
glog.Warning("No SMTP smarthost configured, not sending email notification.")
|
2013-08-15 08:39:10 +00:00
|
|
|
continue
|
2013-08-15 07:05:12 +00:00
|
|
|
}
|
2015-04-24 13:17:49 +00:00
|
|
|
if err := n.sendEmailNotification(emailConfig.GetEmail(), op, a); err != nil {
|
2013-08-27 13:32:08 +00:00
|
|
|
glog.Error("Error sending email notification: ", err)
|
2013-07-30 11:19:18 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-16 20:11:00 +00:00
|
|
|
for _, poConfig := range config.PushoverConfig {
|
2015-04-24 13:17:49 +00:00
|
|
|
if op == notificationOpResolve && !poConfig.GetSendResolved() {
|
2015-04-27 21:16:48 +00:00
|
|
|
continue
|
2015-04-24 13:17:49 +00:00
|
|
|
}
|
|
|
|
if err := n.sendPushoverNotification(poConfig.GetToken(), op, poConfig.GetUserKey(), a); err != nil {
|
2015-02-16 20:11:00 +00:00
|
|
|
glog.Error("Error sending Pushover notification: ", err)
|
|
|
|
}
|
|
|
|
}
|
2015-04-10 17:12:24 +00:00
|
|
|
for _, hcConfig := range config.HipchatConfig {
|
2015-04-24 13:17:49 +00:00
|
|
|
if op == notificationOpResolve && !hcConfig.GetSendResolved() {
|
2015-04-27 21:16:48 +00:00
|
|
|
continue
|
2015-04-24 13:17:49 +00:00
|
|
|
}
|
|
|
|
if err := n.sendHipChatNotification(op, hcConfig, a); err != nil {
|
2015-04-10 17:12:24 +00:00
|
|
|
glog.Error("Error sending HipChat notification: ", err)
|
|
|
|
}
|
|
|
|
}
|
2013-07-30 11:19:18 +00:00
|
|
|
}
|
|
|
|
|
2013-08-27 13:32:08 +00:00
|
|
|
func (n *notifier) Dispatch() {
|
2013-07-30 12:49:16 +00:00
|
|
|
for req := range n.pendingNotifications {
|
2015-04-24 13:17:49 +00:00
|
|
|
n.handleNotification(req.alert, req.op, req.notificationConfig)
|
2013-07-30 11:19:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *notifier) Close() {
|
2013-07-30 12:49:16 +00:00
|
|
|
close(n.pendingNotifications)
|
2013-07-30 11:19:18 +00:00
|
|
|
}
|