2015-10-11 15:24:49 +00:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
2016-08-09 09:04:01 +00:00
|
|
|
package api
|
2015-07-01 11:17:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2015-11-02 18:41:23 +00:00
|
|
|
"sync"
|
2015-09-26 09:12:59 +00:00
|
|
|
"time"
|
2015-07-01 11:17:08 +00:00
|
|
|
|
2016-01-09 12:16:00 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-10-01 20:16:44 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2015-10-16 12:02:22 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2015-07-01 11:17:08 +00:00
|
|
|
"github.com/prometheus/common/route"
|
2016-05-15 10:01:12 +00:00
|
|
|
"github.com/prometheus/common/version"
|
2017-03-16 10:16:10 +00:00
|
|
|
"github.com/prometheus/prometheus/pkg/labels"
|
2015-09-26 09:12:59 +00:00
|
|
|
|
2016-11-22 14:05:14 +00:00
|
|
|
"github.com/prometheus/alertmanager/config"
|
2016-08-09 09:04:01 +00:00
|
|
|
"github.com/prometheus/alertmanager/dispatch"
|
2017-03-16 10:16:10 +00:00
|
|
|
"github.com/prometheus/alertmanager/pkg/parse"
|
2015-09-26 09:12:59 +00:00
|
|
|
"github.com/prometheus/alertmanager/provider"
|
2016-08-30 09:58:27 +00:00
|
|
|
"github.com/prometheus/alertmanager/silence"
|
|
|
|
"github.com/prometheus/alertmanager/silence/silencepb"
|
2015-09-26 09:12:59 +00:00
|
|
|
"github.com/prometheus/alertmanager/types"
|
2017-03-07 17:17:03 +00:00
|
|
|
"github.com/weaveworks/mesh"
|
2015-07-01 11:17:08 +00:00
|
|
|
)
|
|
|
|
|
2016-01-09 12:29:02 +00:00
|
|
|
var (
|
|
|
|
numReceivedAlerts = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
Namespace: "alertmanager",
|
|
|
|
Name: "alerts_received_total",
|
|
|
|
Help: "The total number of received alerts.",
|
|
|
|
}, []string{"status"})
|
|
|
|
|
|
|
|
numInvalidAlerts = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: "alertmanager",
|
2016-01-09 12:49:25 +00:00
|
|
|
Name: "alerts_invalid_total",
|
2016-01-09 12:29:02 +00:00
|
|
|
Help: "The total number of received alerts that were invalid.",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
prometheus.Register(numReceivedAlerts)
|
|
|
|
prometheus.Register(numInvalidAlerts)
|
|
|
|
}
|
|
|
|
|
2016-09-04 15:43:25 +00:00
|
|
|
var corsHeaders = map[string]string{
|
|
|
|
"Access-Control-Allow-Headers": "Accept, Authorization, Content-Type, Origin",
|
2017-01-17 09:42:29 +00:00
|
|
|
"Access-Control-Allow-Methods": "GET, DELETE, OPTIONS",
|
2016-09-04 15:43:25 +00:00
|
|
|
"Access-Control-Allow-Origin": "*",
|
|
|
|
"Access-Control-Expose-Headers": "Date",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enables cross-site script calls.
|
|
|
|
func setCORS(w http.ResponseWriter) {
|
|
|
|
for h, v := range corsHeaders {
|
|
|
|
w.Header().Set(h, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 09:49:32 +00:00
|
|
|
// API provides registration of handlers for API routes.
|
2015-07-01 11:17:08 +00:00
|
|
|
type API struct {
|
2015-11-27 14:41:22 +00:00
|
|
|
alerts provider.Alerts
|
2016-08-30 09:58:27 +00:00
|
|
|
silences *silence.Silences
|
2015-11-27 14:41:22 +00:00
|
|
|
config string
|
2016-11-22 14:05:14 +00:00
|
|
|
configJSON config.Config
|
2015-11-27 14:41:22 +00:00
|
|
|
resolveTimeout time.Duration
|
|
|
|
uptime time.Time
|
2017-03-07 17:17:03 +00:00
|
|
|
mrouter *mesh.Router
|
2015-10-21 14:34:56 +00:00
|
|
|
|
2017-04-28 22:46:52 +00:00
|
|
|
groups groupsFn
|
|
|
|
getAlertStatus getAlertStatusFn
|
2015-10-21 14:34:56 +00:00
|
|
|
|
2017-04-26 00:36:36 +00:00
|
|
|
mtx sync.RWMutex
|
2015-07-01 11:17:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 10:16:10 +00:00
|
|
|
type groupsFn func([]*labels.Matcher) dispatch.AlertOverview
|
2017-04-28 22:46:52 +00:00
|
|
|
type getAlertStatusFn func(model.Fingerprint) types.AlertStatus
|
2017-03-16 10:16:10 +00:00
|
|
|
|
2016-08-09 09:04:01 +00:00
|
|
|
// New returns a new API.
|
2017-04-28 22:46:52 +00:00
|
|
|
func New(alerts provider.Alerts, silences *silence.Silences, gf groupsFn, sf getAlertStatusFn, router *mesh.Router) *API {
|
2015-11-02 18:41:23 +00:00
|
|
|
return &API{
|
2017-04-28 22:46:52 +00:00
|
|
|
alerts: alerts,
|
|
|
|
silences: silences,
|
|
|
|
groups: gf,
|
|
|
|
getAlertStatus: sf,
|
|
|
|
uptime: time.Now(),
|
|
|
|
mrouter: router,
|
2015-07-01 11:17:08 +00:00
|
|
|
}
|
2015-11-02 18:41:23 +00:00
|
|
|
}
|
2015-07-01 16:36:37 +00:00
|
|
|
|
2016-02-14 14:40:48 +00:00
|
|
|
// Register registers the API handlers under their correct routes
|
2015-11-05 09:49:32 +00:00
|
|
|
// in the given router.
|
2015-11-02 18:41:23 +00:00
|
|
|
func (api *API) Register(r *route.Router) {
|
2016-09-04 15:43:25 +00:00
|
|
|
ihf := func(name string, f http.HandlerFunc) http.HandlerFunc {
|
|
|
|
return prometheus.InstrumentHandlerFunc(name, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
setCORS(w)
|
|
|
|
f(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Options("/*path", ihf("options", func(w http.ResponseWriter, r *http.Request) {}))
|
2016-01-09 12:16:00 +00:00
|
|
|
|
2015-10-16 12:02:22 +00:00
|
|
|
// Register legacy forwarder for alert pushing.
|
2016-01-09 12:16:00 +00:00
|
|
|
r.Post("/alerts", ihf("legacy_add_alerts", api.legacyAddAlerts))
|
2015-10-16 12:02:22 +00:00
|
|
|
|
|
|
|
// Register actual API.
|
|
|
|
r = r.WithPrefix("/v1")
|
|
|
|
|
2016-01-09 12:16:00 +00:00
|
|
|
r.Get("/status", ihf("status", api.status))
|
|
|
|
r.Get("/alerts/groups", ihf("alert_groups", api.alertGroups))
|
2015-07-01 11:17:08 +00:00
|
|
|
|
2016-01-09 12:16:00 +00:00
|
|
|
r.Get("/alerts", ihf("list_alerts", api.listAlerts))
|
|
|
|
r.Post("/alerts", ihf("add_alerts", api.addAlerts))
|
2015-07-01 11:17:08 +00:00
|
|
|
|
2016-01-09 12:16:00 +00:00
|
|
|
r.Get("/silences", ihf("list_silences", api.listSilences))
|
2017-05-16 14:48:25 +00:00
|
|
|
r.Post("/silences", ihf("add_silence", api.setSilence))
|
2016-01-09 12:16:00 +00:00
|
|
|
r.Get("/silence/:sid", ihf("get_silence", api.getSilence))
|
|
|
|
r.Del("/silence/:sid", ihf("del_silence", api.delSilence))
|
2015-11-02 18:41:23 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 09:49:32 +00:00
|
|
|
// Update sets the configuration string to a new value.
|
2017-01-17 14:59:11 +00:00
|
|
|
func (api *API) Update(cfg string, resolveTimeout time.Duration) error {
|
2015-11-02 18:41:23 +00:00
|
|
|
api.mtx.Lock()
|
|
|
|
defer api.mtx.Unlock()
|
2015-07-01 11:17:08 +00:00
|
|
|
|
2016-11-22 14:05:14 +00:00
|
|
|
api.config = cfg
|
2015-11-27 14:41:22 +00:00
|
|
|
api.resolveTimeout = resolveTimeout
|
2016-11-22 14:05:14 +00:00
|
|
|
|
|
|
|
configJSON, err := config.Load(cfg)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error: %v", err)
|
2017-01-17 14:59:11 +00:00
|
|
|
return err
|
2016-11-22 14:05:14 +00:00
|
|
|
}
|
2017-01-17 14:59:11 +00:00
|
|
|
|
2016-11-22 14:05:14 +00:00
|
|
|
api.configJSON = *configJSON
|
2017-01-17 14:59:11 +00:00
|
|
|
return nil
|
2015-07-01 11:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type errorType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
errorNone errorType = ""
|
2015-12-07 12:41:18 +00:00
|
|
|
errorInternal = "server_error"
|
2015-07-01 11:17:08 +00:00
|
|
|
errorBadData = "bad_data"
|
|
|
|
)
|
|
|
|
|
|
|
|
type apiError struct {
|
|
|
|
typ errorType
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *apiError) Error() string {
|
|
|
|
return fmt.Sprintf("%s: %s", e.typ, e.err)
|
|
|
|
}
|
|
|
|
|
2015-11-02 18:41:23 +00:00
|
|
|
func (api *API) status(w http.ResponseWriter, req *http.Request) {
|
|
|
|
api.mtx.RLock()
|
|
|
|
|
|
|
|
var status = struct {
|
|
|
|
Config string `json:"config"`
|
2016-11-22 14:05:14 +00:00
|
|
|
ConfigJSON config.Config `json:"configJSON"`
|
2015-11-02 18:41:23 +00:00
|
|
|
VersionInfo map[string]string `json:"versionInfo"`
|
2015-11-02 19:04:37 +00:00
|
|
|
Uptime time.Time `json:"uptime"`
|
2017-03-07 17:17:03 +00:00
|
|
|
MeshStatus meshStatus `json:"meshStatus"`
|
2015-11-02 18:41:23 +00:00
|
|
|
}{
|
2016-11-22 14:05:14 +00:00
|
|
|
Config: api.config,
|
|
|
|
ConfigJSON: api.configJSON,
|
2016-05-15 10:01:12 +00:00
|
|
|
VersionInfo: map[string]string{
|
|
|
|
"version": version.Version,
|
|
|
|
"revision": version.Revision,
|
|
|
|
"branch": version.Branch,
|
|
|
|
"buildUser": version.BuildUser,
|
|
|
|
"buildDate": version.BuildDate,
|
|
|
|
"goVersion": version.GoVersion,
|
|
|
|
},
|
2017-03-07 17:17:03 +00:00
|
|
|
Uptime: api.uptime,
|
|
|
|
MeshStatus: getMeshStatus(api),
|
2015-11-02 18:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
api.mtx.RUnlock()
|
|
|
|
|
|
|
|
respond(w, status)
|
|
|
|
}
|
|
|
|
|
2017-03-07 17:17:03 +00:00
|
|
|
type meshStatus struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
NickName string `json:"nickName"`
|
|
|
|
Peers []peerStatus `json:"peers"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type peerStatus struct {
|
|
|
|
Name string `json:"name"` // e.g. "00:00:00:00:00:01"
|
|
|
|
NickName string `json:"nickName"` // e.g. "a"
|
|
|
|
UID uint64 `json:"uid"` // e.g. "14015114173033265000"
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMeshStatus(api *API) meshStatus {
|
|
|
|
status := mesh.NewStatus(api.mrouter)
|
|
|
|
strippedStatus := meshStatus{
|
|
|
|
Name: status.Name,
|
|
|
|
NickName: status.NickName,
|
|
|
|
Peers: make([]peerStatus, len(status.Peers)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(status.Peers); i++ {
|
|
|
|
strippedStatus.Peers[i] = peerStatus{
|
|
|
|
Name: status.Peers[i].Name,
|
|
|
|
NickName: status.Peers[i].NickName,
|
|
|
|
UID: uint64(status.Peers[i].UID),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strippedStatus
|
|
|
|
}
|
|
|
|
|
2015-11-07 13:30:21 +00:00
|
|
|
func (api *API) alertGroups(w http.ResponseWriter, req *http.Request) {
|
2017-03-16 10:16:10 +00:00
|
|
|
var err error
|
|
|
|
matchers := []*labels.Matcher{}
|
|
|
|
if filter := req.FormValue("filter"); filter != "" {
|
|
|
|
matchers, err = parse.Matchers(filter)
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, apiError{
|
|
|
|
typ: errorBadData,
|
|
|
|
err: err,
|
|
|
|
}, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
groups := api.groups(matchers)
|
|
|
|
|
|
|
|
respond(w, groups)
|
2015-10-21 14:34:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 22:46:52 +00:00
|
|
|
type APIAlert struct {
|
|
|
|
*model.Alert
|
|
|
|
|
2017-05-10 09:49:02 +00:00
|
|
|
Status types.AlertStatus `json:"status"`
|
2017-04-28 22:46:52 +00:00
|
|
|
}
|
2015-10-15 10:47:15 +00:00
|
|
|
|
2017-04-28 22:46:52 +00:00
|
|
|
func (api *API) listAlerts(w http.ResponseWriter, r *http.Request) {
|
2015-10-15 10:47:15 +00:00
|
|
|
var (
|
|
|
|
err error
|
2017-05-02 09:23:56 +00:00
|
|
|
// Initialize result slice to prevent api returning `null` when there
|
|
|
|
// are no alerts present
|
2017-05-17 08:14:42 +00:00
|
|
|
res = []*APIAlert{}
|
|
|
|
matchers = []*labels.Matcher{}
|
|
|
|
showSilenced = true
|
2015-10-15 10:47:15 +00:00
|
|
|
)
|
2017-04-28 22:46:52 +00:00
|
|
|
|
|
|
|
if filter := r.FormValue("filter"); filter != "" {
|
|
|
|
matchers, err = parse.Matchers(filter)
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, apiError{
|
|
|
|
typ: errorBadData,
|
|
|
|
err: err,
|
|
|
|
}, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 08:14:42 +00:00
|
|
|
if silencedParam := r.FormValue("silenced"); silencedParam != "" {
|
|
|
|
if silencedParam == "false" {
|
|
|
|
showSilenced = false
|
|
|
|
} else if silencedParam != "true" {
|
|
|
|
respondError(w, apiError{
|
|
|
|
typ: errorBadData,
|
|
|
|
err: fmt.Errorf(
|
|
|
|
"parameter 'silenced' can either be 'true' or 'false', not '%v'",
|
|
|
|
silencedParam,
|
|
|
|
),
|
|
|
|
}, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:46:52 +00:00
|
|
|
alerts := api.alerts.GetPending()
|
|
|
|
defer alerts.Close()
|
|
|
|
|
2015-10-15 10:47:15 +00:00
|
|
|
// TODO(fabxc): enforce a sensible timeout.
|
|
|
|
for a := range alerts.Next() {
|
|
|
|
if err = alerts.Err(); err != nil {
|
|
|
|
break
|
|
|
|
}
|
2017-04-28 22:46:52 +00:00
|
|
|
|
|
|
|
if !alertMatchesFilterLabels(&a.Alert, matchers) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
status := api.getAlertStatus(a.Fingerprint())
|
|
|
|
|
2017-05-17 08:14:42 +00:00
|
|
|
if !showSilenced && len(status.SilencedBy) != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-04-28 22:46:52 +00:00
|
|
|
apiAlert := &APIAlert{
|
2017-05-10 09:49:02 +00:00
|
|
|
Alert: &a.Alert,
|
|
|
|
Status: status,
|
2017-04-28 22:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, apiAlert)
|
2015-10-15 10:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, apiError{
|
2015-12-07 12:41:18 +00:00
|
|
|
typ: errorInternal,
|
2015-10-15 10:47:15 +00:00
|
|
|
err: err,
|
|
|
|
}, nil)
|
|
|
|
return
|
|
|
|
}
|
2017-04-28 22:46:52 +00:00
|
|
|
respond(w, res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func alertMatchesFilterLabels(a *model.Alert, matchers []*labels.Matcher) bool {
|
|
|
|
for _, m := range matchers {
|
|
|
|
if v, prs := a.Labels[model.LabelName(m.Name)]; !prs || !m.Matches(string(v)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2015-09-26 09:12:59 +00:00
|
|
|
}
|
2015-09-25 16:14:46 +00:00
|
|
|
|
2015-10-16 12:02:22 +00:00
|
|
|
func (api *API) legacyAddAlerts(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var legacyAlerts = []struct {
|
|
|
|
Summary model.LabelValue `json:"summary"`
|
|
|
|
Description model.LabelValue `json:"description"`
|
|
|
|
Runbook model.LabelValue `json:"runbook"`
|
|
|
|
Labels model.LabelSet `json:"labels"`
|
|
|
|
Payload model.LabelSet `json:"payload"`
|
|
|
|
}{}
|
|
|
|
if err := receive(r, &legacyAlerts); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var alerts []*types.Alert
|
|
|
|
for _, la := range legacyAlerts {
|
|
|
|
a := &types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: la.Labels,
|
|
|
|
Annotations: la.Payload,
|
|
|
|
},
|
|
|
|
}
|
2015-10-16 14:55:56 +00:00
|
|
|
if a.Annotations == nil {
|
|
|
|
a.Annotations = model.LabelSet{}
|
|
|
|
}
|
2015-10-16 12:02:22 +00:00
|
|
|
a.Annotations["summary"] = la.Summary
|
|
|
|
a.Annotations["description"] = la.Description
|
|
|
|
a.Annotations["runbook"] = la.Runbook
|
|
|
|
|
|
|
|
alerts = append(alerts, a)
|
|
|
|
}
|
|
|
|
|
2015-12-09 17:21:06 +00:00
|
|
|
api.insertAlerts(w, r, alerts...)
|
|
|
|
}
|
2015-10-16 12:02:22 +00:00
|
|
|
|
2015-12-09 17:21:06 +00:00
|
|
|
func (api *API) addAlerts(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var alerts []*types.Alert
|
|
|
|
if err := receive(r, &alerts); err != nil {
|
2015-10-16 12:02:22 +00:00
|
|
|
respondError(w, apiError{
|
2015-12-09 17:21:06 +00:00
|
|
|
typ: errorBadData,
|
2015-10-16 12:02:22 +00:00
|
|
|
err: err,
|
|
|
|
}, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-09 17:21:06 +00:00
|
|
|
api.insertAlerts(w, r, alerts...)
|
2015-10-16 12:02:22 +00:00
|
|
|
}
|
|
|
|
|
2015-12-09 17:21:06 +00:00
|
|
|
func (api *API) insertAlerts(w http.ResponseWriter, r *http.Request, alerts ...*types.Alert) {
|
2015-09-30 12:53:05 +00:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
for _, alert := range alerts {
|
2015-09-29 15:26:44 +00:00
|
|
|
alert.UpdatedAt = now
|
|
|
|
|
2016-01-09 12:29:02 +00:00
|
|
|
// Ensure StartsAt is set.
|
2015-09-29 15:26:44 +00:00
|
|
|
if alert.StartsAt.IsZero() {
|
|
|
|
alert.StartsAt = now
|
2015-09-26 09:12:59 +00:00
|
|
|
}
|
2016-01-09 12:29:02 +00:00
|
|
|
// If no end time is defined, set a timeout after which an alert
|
|
|
|
// is marked resolved if it is not updated.
|
2015-09-29 15:26:44 +00:00
|
|
|
if alert.EndsAt.IsZero() {
|
2015-09-30 12:53:05 +00:00
|
|
|
alert.Timeout = true
|
2016-02-03 13:12:10 +00:00
|
|
|
alert.EndsAt = now.Add(api.resolveTimeout)
|
2016-01-09 12:29:02 +00:00
|
|
|
|
|
|
|
numReceivedAlerts.WithLabelValues("firing").Inc()
|
|
|
|
} else {
|
|
|
|
numReceivedAlerts.WithLabelValues("resolved").Inc()
|
2015-09-26 09:12:59 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-25 16:14:46 +00:00
|
|
|
|
2016-01-09 12:29:02 +00:00
|
|
|
// Make a best effort to insert all alerts that are valid.
|
|
|
|
var (
|
|
|
|
validAlerts = make([]*types.Alert, 0, len(alerts))
|
|
|
|
validationErrs = &types.MultiError{}
|
|
|
|
)
|
2015-12-09 17:21:06 +00:00
|
|
|
for _, a := range alerts {
|
|
|
|
if err := a.Validate(); err != nil {
|
2016-01-09 12:29:02 +00:00
|
|
|
validationErrs.Add(err)
|
|
|
|
numInvalidAlerts.Inc()
|
|
|
|
continue
|
2015-12-09 17:21:06 +00:00
|
|
|
}
|
2016-01-09 12:29:02 +00:00
|
|
|
validAlerts = append(validAlerts, a)
|
2015-12-09 17:21:06 +00:00
|
|
|
}
|
2016-01-09 12:29:02 +00:00
|
|
|
if err := api.alerts.Put(validAlerts...); err != nil {
|
2015-09-26 09:12:59 +00:00
|
|
|
respondError(w, apiError{
|
2015-12-07 12:41:18 +00:00
|
|
|
typ: errorInternal,
|
2015-09-26 09:12:59 +00:00
|
|
|
err: err,
|
|
|
|
}, nil)
|
|
|
|
return
|
|
|
|
}
|
2016-01-09 12:29:02 +00:00
|
|
|
|
|
|
|
if validationErrs.Len() > 0 {
|
|
|
|
respondError(w, apiError{
|
|
|
|
typ: errorBadData,
|
|
|
|
err: validationErrs,
|
|
|
|
}, nil)
|
|
|
|
return
|
|
|
|
}
|
2015-09-25 16:14:46 +00:00
|
|
|
|
2015-09-26 09:12:59 +00:00
|
|
|
respond(w, nil)
|
|
|
|
}
|
2015-09-25 16:14:46 +00:00
|
|
|
|
2017-05-16 14:48:25 +00:00
|
|
|
func (api *API) setSilence(w http.ResponseWriter, r *http.Request) {
|
2016-08-09 12:13:05 +00:00
|
|
|
var sil types.Silence
|
|
|
|
if err := receive(r, &sil); err != nil {
|
2015-12-16 15:39:37 +00:00
|
|
|
respondError(w, apiError{
|
|
|
|
typ: errorBadData,
|
|
|
|
err: err,
|
|
|
|
}, nil)
|
2015-09-27 12:07:04 +00:00
|
|
|
return
|
|
|
|
}
|
2016-08-30 09:58:27 +00:00
|
|
|
psil, err := silenceToProto(&sil)
|
|
|
|
if err != nil {
|
2016-05-29 23:12:05 +00:00
|
|
|
respondError(w, apiError{
|
|
|
|
typ: errorBadData,
|
|
|
|
err: err,
|
|
|
|
}, nil)
|
|
|
|
return
|
|
|
|
}
|
2015-12-09 17:21:06 +00:00
|
|
|
|
2017-05-16 14:48:25 +00:00
|
|
|
sid, err := api.silences.Set(psil)
|
2015-10-01 18:57:37 +00:00
|
|
|
if err != nil {
|
2015-09-27 12:07:04 +00:00
|
|
|
respondError(w, apiError{
|
2017-05-16 14:48:25 +00:00
|
|
|
typ: errorBadData,
|
2015-09-27 12:07:04 +00:00
|
|
|
err: err,
|
|
|
|
}, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-01 18:57:37 +00:00
|
|
|
respond(w, struct {
|
2016-08-30 09:58:27 +00:00
|
|
|
SilenceID string `json:"silenceId"`
|
2015-10-01 18:57:37 +00:00
|
|
|
}{
|
|
|
|
SilenceID: sid,
|
|
|
|
})
|
2015-09-27 12:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) getSilence(w http.ResponseWriter, r *http.Request) {
|
2017-04-26 00:36:36 +00:00
|
|
|
sid := route.Param(r.Context(), "sid")
|
2015-09-27 12:07:04 +00:00
|
|
|
|
2016-08-30 09:58:27 +00:00
|
|
|
sils, err := api.silences.Query(silence.QIDs(sid))
|
|
|
|
if err != nil || len(sils) == 0 {
|
2015-09-27 12:07:04 +00:00
|
|
|
http.Error(w, fmt.Sprint("Error getting silence: ", err), http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2016-08-30 09:58:27 +00:00
|
|
|
sil, err := silenceFromProto(sils[0])
|
2015-09-27 12:07:04 +00:00
|
|
|
if err != nil {
|
|
|
|
respondError(w, apiError{
|
2016-08-30 09:58:27 +00:00
|
|
|
typ: errorInternal,
|
2015-09-27 12:07:04 +00:00
|
|
|
err: err,
|
|
|
|
}, nil)
|
2015-12-09 17:21:06 +00:00
|
|
|
return
|
2015-09-27 12:07:04 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 09:58:27 +00:00
|
|
|
respond(w, sil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) delSilence(w http.ResponseWriter, r *http.Request) {
|
2017-04-26 00:36:36 +00:00
|
|
|
sid := route.Param(r.Context(), "sid")
|
2016-08-30 09:58:27 +00:00
|
|
|
|
|
|
|
if err := api.silences.Expire(sid); err != nil {
|
2015-09-27 12:07:04 +00:00
|
|
|
respondError(w, apiError{
|
2016-08-30 09:58:27 +00:00
|
|
|
typ: errorBadData,
|
2015-09-27 12:07:04 +00:00
|
|
|
err: err,
|
|
|
|
}, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
respond(w, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) listSilences(w http.ResponseWriter, r *http.Request) {
|
2016-08-30 09:58:27 +00:00
|
|
|
psils, err := api.silences.Query()
|
2015-09-27 12:07:04 +00:00
|
|
|
if err != nil {
|
|
|
|
respondError(w, apiError{
|
2015-12-07 12:41:18 +00:00
|
|
|
typ: errorInternal,
|
2015-09-27 12:07:04 +00:00
|
|
|
err: err,
|
|
|
|
}, nil)
|
|
|
|
return
|
|
|
|
}
|
2016-08-30 09:58:27 +00:00
|
|
|
|
2017-03-16 10:16:10 +00:00
|
|
|
matchers := []*labels.Matcher{}
|
|
|
|
if filter := r.FormValue("filter"); filter != "" {
|
|
|
|
matchers, err = parse.Matchers(filter)
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, apiError{
|
|
|
|
typ: errorBadData,
|
|
|
|
err: err,
|
|
|
|
}, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sils := []*types.Silence{}
|
2016-08-30 09:58:27 +00:00
|
|
|
for _, ps := range psils {
|
|
|
|
s, err := silenceFromProto(ps)
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, apiError{
|
|
|
|
typ: errorInternal,
|
|
|
|
err: err,
|
|
|
|
}, nil)
|
|
|
|
return
|
|
|
|
}
|
2017-03-16 10:16:10 +00:00
|
|
|
|
|
|
|
if !matchesFilterLabels(s, matchers) {
|
|
|
|
continue
|
|
|
|
}
|
2016-08-30 09:58:27 +00:00
|
|
|
sils = append(sils, s)
|
|
|
|
}
|
|
|
|
|
2015-09-27 12:07:04 +00:00
|
|
|
respond(w, sils)
|
|
|
|
}
|
2015-07-01 11:17:08 +00:00
|
|
|
|
2017-03-16 10:16:10 +00:00
|
|
|
func matchesFilterLabels(s *types.Silence, matchers []*labels.Matcher) bool {
|
|
|
|
sms := map[string]string{}
|
|
|
|
for _, m := range s.Matchers {
|
|
|
|
sms[m.Name] = m.Value
|
|
|
|
}
|
|
|
|
for _, m := range matchers {
|
|
|
|
if v, prs := sms[m.Name]; !prs || !m.Matches(v) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-08-30 09:58:27 +00:00
|
|
|
func silenceToProto(s *types.Silence) (*silencepb.Silence, error) {
|
|
|
|
sil := &silencepb.Silence{
|
|
|
|
Id: s.ID,
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: s.StartsAt,
|
|
|
|
EndsAt: s.EndsAt,
|
|
|
|
UpdatedAt: s.UpdatedAt,
|
2017-05-16 14:48:25 +00:00
|
|
|
Comment: s.Comment,
|
|
|
|
CreatedBy: s.CreatedBy,
|
2016-08-30 09:58:27 +00:00
|
|
|
}
|
|
|
|
for _, m := range s.Matchers {
|
|
|
|
matcher := &silencepb.Matcher{
|
|
|
|
Name: m.Name,
|
|
|
|
Pattern: m.Value,
|
|
|
|
Type: silencepb.Matcher_EQUAL,
|
|
|
|
}
|
|
|
|
if m.IsRegex {
|
|
|
|
matcher.Type = silencepb.Matcher_REGEXP
|
|
|
|
}
|
|
|
|
sil.Matchers = append(sil.Matchers, matcher)
|
|
|
|
}
|
|
|
|
return sil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func silenceFromProto(s *silencepb.Silence) (*types.Silence, error) {
|
|
|
|
sil := &types.Silence{
|
|
|
|
ID: s.Id,
|
2017-04-18 08:46:40 +00:00
|
|
|
StartsAt: s.StartsAt,
|
|
|
|
EndsAt: s.EndsAt,
|
|
|
|
UpdatedAt: s.UpdatedAt,
|
2017-05-10 09:55:28 +00:00
|
|
|
Status: types.SilenceStatus{
|
|
|
|
State: types.CalcSilenceState(s.StartsAt, s.EndsAt),
|
|
|
|
},
|
2017-05-16 14:48:25 +00:00
|
|
|
Comment: s.Comment,
|
|
|
|
CreatedBy: s.CreatedBy,
|
2016-08-30 09:58:27 +00:00
|
|
|
}
|
|
|
|
for _, m := range s.Matchers {
|
|
|
|
matcher := &types.Matcher{
|
|
|
|
Name: m.Name,
|
|
|
|
Value: m.Pattern,
|
|
|
|
}
|
|
|
|
switch m.Type {
|
|
|
|
case silencepb.Matcher_EQUAL:
|
|
|
|
case silencepb.Matcher_REGEXP:
|
|
|
|
matcher.IsRegex = true
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown matcher type")
|
|
|
|
}
|
|
|
|
sil.Matchers = append(sil.Matchers, matcher)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sil, nil
|
|
|
|
}
|
|
|
|
|
2015-07-01 11:17:08 +00:00
|
|
|
type status string
|
|
|
|
|
|
|
|
const (
|
|
|
|
statusSuccess status = "success"
|
|
|
|
statusError = "error"
|
|
|
|
)
|
|
|
|
|
|
|
|
type response struct {
|
|
|
|
Status status `json:"status"`
|
|
|
|
Data interface{} `json:"data,omitempty"`
|
|
|
|
ErrorType errorType `json:"errorType,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func respond(w http.ResponseWriter, data interface{}) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2015-07-02 16:38:05 +00:00
|
|
|
w.WriteHeader(200)
|
2015-07-01 11:17:08 +00:00
|
|
|
|
|
|
|
b, err := json.Marshal(&response{
|
|
|
|
Status: statusSuccess,
|
|
|
|
Data: data,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-11-22 14:05:14 +00:00
|
|
|
log.Errorf("errorr: %v", err)
|
2015-07-01 11:17:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func respondError(w http.ResponseWriter, apiErr apiError, data interface{}) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2015-12-07 12:41:18 +00:00
|
|
|
|
|
|
|
switch apiErr.typ {
|
|
|
|
case errorBadData:
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
case errorInternal:
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
default:
|
2016-02-15 10:18:23 +00:00
|
|
|
panic(fmt.Sprintf("unknown error type %q", apiErr))
|
2015-12-07 12:41:18 +00:00
|
|
|
}
|
2015-07-01 11:17:08 +00:00
|
|
|
|
|
|
|
b, err := json.Marshal(&response{
|
|
|
|
Status: statusError,
|
|
|
|
ErrorType: apiErr.typ,
|
|
|
|
Error: apiErr.err.Error(),
|
|
|
|
Data: data,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-11-18 15:50:15 +00:00
|
|
|
log.Errorf("api error: %v", apiErr.Error())
|
2015-12-07 12:41:18 +00:00
|
|
|
|
2015-07-01 11:17:08 +00:00
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func receive(r *http.Request, v interface{}) error {
|
|
|
|
dec := json.NewDecoder(r.Body)
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
2015-10-01 20:16:44 +00:00
|
|
|
err := dec.Decode(v)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Decoding request failed: %v", err)
|
|
|
|
}
|
|
|
|
return err
|
2015-07-01 11:17:08 +00:00
|
|
|
}
|