2019-06-18 13:34:46 +00:00
|
|
|
// Copyright 2019 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 pushover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-07-30 08:11:43 +00:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2019-06-18 13:34:46 +00:00
|
|
|
commoncfg "github.com/prometheus/common/config"
|
|
|
|
|
|
|
|
"github.com/prometheus/alertmanager/config"
|
|
|
|
"github.com/prometheus/alertmanager/notify"
|
|
|
|
"github.com/prometheus/alertmanager/template"
|
|
|
|
"github.com/prometheus/alertmanager/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Notifier implements a Notifier for Pushover notifications.
|
|
|
|
type Notifier struct {
|
2019-08-02 14:17:40 +00:00
|
|
|
conf *config.PushoverConfig
|
|
|
|
tmpl *template.Template
|
|
|
|
logger log.Logger
|
|
|
|
client *http.Client
|
|
|
|
retrier *notify.Retrier
|
|
|
|
apiURL string // for tests.
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Pushover notifier.
|
2021-04-16 09:30:31 +00:00
|
|
|
func New(c *config.PushoverConfig, t *template.Template, l log.Logger, httpOpts ...commoncfg.HTTPClientOption) (*Notifier, error) {
|
2021-11-10 16:28:47 +00:00
|
|
|
client, err := commoncfg.NewClientFromConfig(*c.HTTPConfig, "pushover", httpOpts...)
|
2019-06-18 13:34:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Notifier{
|
2019-08-02 14:17:40 +00:00
|
|
|
conf: c,
|
|
|
|
tmpl: t,
|
|
|
|
logger: l,
|
|
|
|
client: client,
|
|
|
|
retrier: ¬ify.Retrier{},
|
|
|
|
apiURL: "https://api.pushover.net/1/messages.json",
|
2019-06-18 13:34:46 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify implements the Notifier interface.
|
|
|
|
func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) {
|
|
|
|
key, ok := notify.GroupKey(ctx)
|
|
|
|
if !ok {
|
|
|
|
return false, fmt.Errorf("group key missing")
|
|
|
|
}
|
|
|
|
data := notify.GetTemplateData(ctx, n.tmpl, as, n.logger)
|
|
|
|
|
|
|
|
level.Debug(n.logger).Log("incident", key)
|
|
|
|
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
message string
|
|
|
|
)
|
|
|
|
tmpl := notify.TmplText(n.tmpl, data, &err)
|
|
|
|
tmplHTML := notify.TmplHTML(n.tmpl, data, &err)
|
|
|
|
|
|
|
|
parameters := url.Values{}
|
|
|
|
parameters.Add("token", tmpl(string(n.conf.Token)))
|
|
|
|
parameters.Add("user", tmpl(string(n.conf.UserKey)))
|
|
|
|
|
2022-11-09 17:14:51 +00:00
|
|
|
// https://pushover.net/api#limits - 250 characters or runes.
|
|
|
|
title, truncated := notify.TruncateInRunes(tmpl(n.conf.Title), 250)
|
2019-06-18 13:34:46 +00:00
|
|
|
if truncated {
|
2022-11-25 19:23:20 +00:00
|
|
|
level.Warn(n.logger).Log("msg", "Truncated title", "incident", key)
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
parameters.Add("title", title)
|
|
|
|
|
|
|
|
if n.conf.HTML {
|
|
|
|
parameters.Add("html", "1")
|
|
|
|
message = tmplHTML(n.conf.Message)
|
|
|
|
} else {
|
|
|
|
message = tmpl(n.conf.Message)
|
|
|
|
}
|
|
|
|
|
2022-11-09 17:14:51 +00:00
|
|
|
// https://pushover.net/api#limits - 1024 characters or runes.
|
|
|
|
message, truncated = notify.TruncateInRunes(message, 1024)
|
2019-06-18 13:34:46 +00:00
|
|
|
if truncated {
|
2022-11-25 19:23:20 +00:00
|
|
|
level.Warn(n.logger).Log("msg", "Truncated message", "incident", key)
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
message = strings.TrimSpace(message)
|
|
|
|
if message == "" {
|
|
|
|
// Pushover rejects empty messages.
|
|
|
|
message = "(no details)"
|
|
|
|
}
|
|
|
|
parameters.Add("message", message)
|
|
|
|
|
2022-11-09 17:14:51 +00:00
|
|
|
// https://pushover.net/api#limits - 512 characters or runes.
|
|
|
|
supplementaryURL, truncated := notify.TruncateInRunes(tmpl(n.conf.URL), 512)
|
2019-06-18 13:34:46 +00:00
|
|
|
if truncated {
|
2022-11-25 19:23:20 +00:00
|
|
|
level.Warn(n.logger).Log("msg", "Truncated URL", "incident", key)
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
|
|
|
parameters.Add("url", supplementaryURL)
|
|
|
|
parameters.Add("url_title", tmpl(n.conf.URLTitle))
|
|
|
|
|
|
|
|
parameters.Add("priority", tmpl(n.conf.Priority))
|
|
|
|
parameters.Add("retry", fmt.Sprintf("%d", int64(time.Duration(n.conf.Retry).Seconds())))
|
|
|
|
parameters.Add("expire", fmt.Sprintf("%d", int64(time.Duration(n.conf.Expire).Seconds())))
|
|
|
|
parameters.Add("sound", tmpl(n.conf.Sound))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(n.apiURL)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
u.RawQuery = parameters.Encode()
|
|
|
|
// Don't log the URL as it contains secret data (see #1825).
|
|
|
|
level.Debug(n.logger).Log("msg", "Sending message", "incident", key)
|
|
|
|
resp, err := notify.PostText(ctx, n.client, u.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return true, notify.RedactURL(err)
|
|
|
|
}
|
|
|
|
defer notify.Drain(resp)
|
|
|
|
|
2022-10-19 13:07:38 +00:00
|
|
|
return n.retrier.Check(resp.StatusCode, resp.Body)
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|