2020-04-06 11:44:03 +00:00
|
|
|
package notifier
|
2020-02-16 18:59:02 +00:00
|
|
|
|
2020-02-21 21:15:05 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
2020-05-10 16:58:17 +00:00
|
|
|
"context"
|
2020-02-21 21:15:05 +00:00
|
|
|
"fmt"
|
2020-04-06 11:44:03 +00:00
|
|
|
"io/ioutil"
|
2020-02-21 21:15:05 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-02-16 18:59:02 +00:00
|
|
|
// AlertManager represents integration provider with Prometheus alert manager
|
2020-04-06 11:44:03 +00:00
|
|
|
// https://github.com/prometheus/alertmanager
|
2020-02-21 21:15:05 +00:00
|
|
|
type AlertManager struct {
|
2020-06-29 19:21:03 +00:00
|
|
|
alertURL string
|
|
|
|
basicAuthUser string
|
|
|
|
basicAuthPass string
|
|
|
|
argFunc AlertURLGenerator
|
|
|
|
client *http.Client
|
2020-02-21 21:15:05 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 18:59:02 +00:00
|
|
|
// Send an alert or resolve message
|
2020-05-10 16:58:17 +00:00
|
|
|
func (am *AlertManager) Send(ctx context.Context, alerts []Alert) error {
|
2020-04-06 11:44:03 +00:00
|
|
|
b := &bytes.Buffer{}
|
2020-03-13 10:19:31 +00:00
|
|
|
writeamRequest(b, alerts, am.argFunc)
|
2020-05-10 16:58:17 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", am.alertURL, b)
|
2020-02-21 21:15:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-13 08:25:39 +00:00
|
|
|
req.Header.Set("Content-Type", "application/json; charset=utf-8")
|
2020-05-10 16:58:17 +00:00
|
|
|
req = req.WithContext(ctx)
|
2020-06-29 19:21:03 +00:00
|
|
|
if am.basicAuthPass != "" {
|
|
|
|
req.SetBasicAuth(am.basicAuthUser, am.basicAuthPass)
|
|
|
|
}
|
2020-05-10 16:58:17 +00:00
|
|
|
resp, err := am.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-21 21:15:05 +00:00
|
|
|
defer func() { _ = resp.Body.Close() }()
|
2020-04-06 11:44:03 +00:00
|
|
|
|
2020-02-21 21:15:05 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2020-04-06 11:44:03 +00:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("failed to read response from %q: %w", am.alertURL, err)
|
2020-02-21 21:15:05 +00:00
|
|
|
}
|
2020-04-06 11:44:03 +00:00
|
|
|
return fmt.Errorf("invalid SC %d from %q; response body: %s", resp.StatusCode, am.alertURL, string(body))
|
2020-02-21 21:15:05 +00:00
|
|
|
}
|
2020-02-16 18:59:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-06 11:44:03 +00:00
|
|
|
|
|
|
|
// AlertURLGenerator returns URL to single alert by given name
|
2020-06-21 10:32:46 +00:00
|
|
|
type AlertURLGenerator func(Alert) string
|
2020-04-06 11:44:03 +00:00
|
|
|
|
|
|
|
const alertManagerPath = "/api/v2/alerts"
|
|
|
|
|
|
|
|
// NewAlertManager is a constructor for AlertManager
|
2020-06-29 19:21:03 +00:00
|
|
|
func NewAlertManager(alertManagerURL, user, pass string, fn AlertURLGenerator, c *http.Client) *AlertManager {
|
|
|
|
addr := strings.TrimSuffix(alertManagerURL, "/") + alertManagerPath
|
2020-04-06 11:44:03 +00:00
|
|
|
return &AlertManager{
|
2020-06-29 19:21:03 +00:00
|
|
|
alertURL: addr,
|
|
|
|
argFunc: fn,
|
|
|
|
client: c,
|
|
|
|
basicAuthUser: user,
|
|
|
|
basicAuthPass: pass,
|
2020-04-06 11:44:03 +00:00
|
|
|
}
|
|
|
|
}
|