2020-04-11 11:40:24 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-04-11 17:49:23 +02:00
|
|
|
"sort"
|
2020-04-11 11:40:24 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
|
2020-05-10 18:58:17 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/procutil"
|
2020-04-11 11:40:24 +02:00
|
|
|
)
|
|
|
|
|
2020-05-10 18:58:17 +02:00
|
|
|
// APIAlert represents an notifier.Alert state
|
|
|
|
// for WEB view
|
2020-04-12 14:08:11 +02:00
|
|
|
type APIAlert struct {
|
2020-05-04 23:51:22 +02:00
|
|
|
ID string `json:"id"`
|
2020-04-11 17:49:23 +02:00
|
|
|
Name string `json:"name"`
|
2020-05-10 18:58:17 +02:00
|
|
|
GroupID string `json:"group_id"`
|
2020-04-11 17:49:23 +02:00
|
|
|
Expression string `json:"expression"`
|
|
|
|
State string `json:"state"`
|
|
|
|
Value string `json:"value"`
|
2020-04-11 11:40:24 +02:00
|
|
|
Labels map[string]string `json:"labels"`
|
|
|
|
Annotations map[string]string `json:"annotations"`
|
|
|
|
ActiveAt time.Time `json:"activeAt"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type requestHandler struct {
|
2020-05-10 18:58:17 +02:00
|
|
|
m *manager
|
2020-04-11 11:40:24 +02:00
|
|
|
}
|
|
|
|
|
2020-04-11 17:49:23 +02:00
|
|
|
var pathList = [][]string{
|
|
|
|
{"/api/v1/alerts", "list all active alerts"},
|
2020-05-10 18:58:17 +02:00
|
|
|
{"/api/v1/groupID/alertID/status", "get alert status by ID"},
|
2020-04-11 21:42:01 +02:00
|
|
|
// /metrics is served by httpserver by default
|
|
|
|
{"/metrics", "list of application metrics"},
|
2020-05-10 18:58:17 +02:00
|
|
|
{"/-/reload", "reload configuration"},
|
2020-04-11 17:49:23 +02:00
|
|
|
}
|
|
|
|
|
2020-04-11 11:40:24 +02:00
|
|
|
func (rh *requestHandler) handler(w http.ResponseWriter, r *http.Request) bool {
|
|
|
|
resph := responseHandler{w}
|
|
|
|
switch r.URL.Path {
|
2020-04-11 17:49:23 +02:00
|
|
|
case "/":
|
|
|
|
for _, path := range pathList {
|
|
|
|
p, doc := path[0], path[1]
|
|
|
|
fmt.Fprintf(w, "<a href='%s'>%q</a> - %s<br/>", p, p, doc)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
case "/api/v1/alerts":
|
|
|
|
resph.handle(rh.list())
|
|
|
|
return true
|
2020-05-09 11:32:12 +02:00
|
|
|
case "/-/reload":
|
|
|
|
logger.Infof("api config reload was called, sending sighup")
|
|
|
|
procutil.SelfSIGHUP()
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return true
|
2020-04-11 11:40:24 +02:00
|
|
|
default:
|
2020-04-11 17:49:23 +02:00
|
|
|
// /api/v1/<groupName>/<alertID>/status
|
2020-04-11 11:40:24 +02:00
|
|
|
if strings.HasSuffix(r.URL.Path, "/status") {
|
|
|
|
resph.handle(rh.alert(r.URL.Path))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:51:03 +02:00
|
|
|
type listAlertsResponse struct {
|
|
|
|
Data struct {
|
2020-04-12 14:08:11 +02:00
|
|
|
Alerts []*APIAlert `json:"alerts"`
|
2020-04-12 13:51:03 +02:00
|
|
|
} `json:"data"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
|
|
|
|
2020-04-11 17:49:23 +02:00
|
|
|
func (rh *requestHandler) list() ([]byte, error) {
|
2020-05-10 18:58:17 +02:00
|
|
|
rh.m.groupsMu.RLock()
|
|
|
|
defer rh.m.groupsMu.RUnlock()
|
2020-04-11 11:40:24 +02:00
|
|
|
lr := listAlertsResponse{Status: "success"}
|
2020-05-10 18:58:17 +02:00
|
|
|
for _, g := range rh.m.groups {
|
2020-04-11 17:49:23 +02:00
|
|
|
for _, r := range g.Rules {
|
|
|
|
lr.Data.Alerts = append(lr.Data.Alerts, r.AlertsAPI()...)
|
2020-04-11 11:40:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 17:49:23 +02:00
|
|
|
// sort list of alerts for deterministic output
|
|
|
|
sort.Slice(lr.Data.Alerts, func(i, j int) bool {
|
2020-05-04 23:51:22 +02:00
|
|
|
return lr.Data.Alerts[i].ID < lr.Data.Alerts[j].ID
|
2020-04-11 17:49:23 +02:00
|
|
|
})
|
|
|
|
|
2020-04-11 11:40:24 +02:00
|
|
|
b, err := json.Marshal(lr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &httpserver.ErrorWithStatusCode{
|
|
|
|
Err: fmt.Errorf(`error encoding list of active alerts: %s`, err),
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rh *requestHandler) alert(path string) ([]byte, error) {
|
2020-05-10 18:58:17 +02:00
|
|
|
rh.m.groupsMu.RLock()
|
|
|
|
defer rh.m.groupsMu.RUnlock()
|
|
|
|
|
2020-04-11 11:40:24 +02:00
|
|
|
parts := strings.SplitN(strings.TrimPrefix(path, "/api/v1/"), "/", 3)
|
|
|
|
if len(parts) != 3 {
|
|
|
|
return nil, &httpserver.ErrorWithStatusCode{
|
|
|
|
Err: fmt.Errorf(`path %q cointains /status suffix but doesn't match pattern "/group/alert/status"`, path),
|
|
|
|
StatusCode: http.StatusBadRequest,
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 18:58:17 +02:00
|
|
|
|
|
|
|
groupID, err := uint64FromPath(parts[0])
|
2020-04-11 11:40:24 +02:00
|
|
|
if err != nil {
|
2020-05-10 18:58:17 +02:00
|
|
|
return nil, badRequest(fmt.Errorf(`cannot parse groupID: %s`, err))
|
2020-04-11 11:40:24 +02:00
|
|
|
}
|
2020-05-10 18:58:17 +02:00
|
|
|
alertID, err := uint64FromPath(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, badRequest(fmt.Errorf(`cannot parse alertID: %s`, err))
|
2020-04-11 11:40:24 +02:00
|
|
|
}
|
2020-05-10 18:58:17 +02:00
|
|
|
resp, err := rh.m.AlertAPI(groupID, alertID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errResponse(err, http.StatusNotFound)
|
2020-04-11 11:40:24 +02:00
|
|
|
}
|
2020-05-10 18:58:17 +02:00
|
|
|
return json.Marshal(resp)
|
2020-04-11 11:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// responseHandler wrapper on http.ResponseWriter with sugar
|
|
|
|
type responseHandler struct{ http.ResponseWriter }
|
|
|
|
|
|
|
|
func (w responseHandler) handle(b []byte, err error) {
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, "%s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(b)
|
|
|
|
}
|
2020-05-10 18:58:17 +02:00
|
|
|
|
|
|
|
func uint64FromPath(path string) (uint64, error) {
|
|
|
|
s := strings.TrimRight(path, "/")
|
|
|
|
return strconv.ParseUint(s, 10, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func badRequest(err error) *httpserver.ErrorWithStatusCode {
|
|
|
|
return errResponse(err, http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
func errResponse(err error, sc int) *httpserver.ErrorWithStatusCode {
|
|
|
|
return &httpserver.ErrorWithStatusCode{
|
|
|
|
Err: err,
|
|
|
|
StatusCode: sc,
|
|
|
|
}
|
|
|
|
}
|