2020-04-11 09:40:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-05-30 23:55:28 +00:00
|
|
|
"embed"
|
2020-04-11 09:40:24 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-09-21 11:41:01 +00:00
|
|
|
"path"
|
2020-04-11 15:49:23 +00:00
|
|
|
"sort"
|
2020-04-11 09:40:24 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2021-09-21 11:41:01 +00:00
|
|
|
"sync"
|
2020-04-11 09:40:24 +00:00
|
|
|
|
2022-02-02 12:11:41 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/notifier"
|
2021-09-21 11:41:01 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/tpl"
|
2020-04-11 09:40:24 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
|
2020-05-10 16:58:17 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/procutil"
|
2020-04-11 09:40:24 +00:00
|
|
|
)
|
|
|
|
|
2021-09-21 11:41:01 +00:00
|
|
|
var (
|
|
|
|
once = sync.Once{}
|
|
|
|
apiLinks [][2]string
|
|
|
|
navItems []tpl.NavItem
|
2022-05-30 23:55:28 +00:00
|
|
|
//go:embed static
|
|
|
|
staticFiles embed.FS
|
|
|
|
staticServer = http.FileServer(http.FS(staticFiles))
|
2021-09-21 11:41:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func initLinks() {
|
|
|
|
pathPrefix := httpserver.GetPathPrefix()
|
2022-02-08 19:03:42 +00:00
|
|
|
if pathPrefix == "" {
|
|
|
|
pathPrefix = "/"
|
|
|
|
}
|
2021-09-21 11:41:01 +00:00
|
|
|
apiLinks = [][2]string{
|
2022-03-15 11:54:53 +00:00
|
|
|
{path.Join(pathPrefix, "api/v1/rules"), "list all loaded groups and rules"},
|
2021-09-21 11:41:01 +00:00
|
|
|
{path.Join(pathPrefix, "api/v1/alerts"), "list all active alerts"},
|
|
|
|
{path.Join(pathPrefix, "api/v1/groupID/alertID/status"), "get alert status by ID"},
|
2021-10-19 21:45:05 +00:00
|
|
|
{path.Join(pathPrefix, "flags"), "command-line flags"},
|
2021-09-21 11:41:01 +00:00
|
|
|
{path.Join(pathPrefix, "metrics"), "list of application metrics"},
|
|
|
|
{path.Join(pathPrefix, "-/reload"), "reload configuration"},
|
|
|
|
}
|
|
|
|
navItems = []tpl.NavItem{
|
2022-02-02 12:11:41 +00:00
|
|
|
{Name: "vmalert", Url: path.Join(pathPrefix, "/")},
|
2021-09-21 11:41:01 +00:00
|
|
|
{Name: "Groups", Url: path.Join(pathPrefix, "groups")},
|
2021-10-18 12:12:47 +00:00
|
|
|
{Name: "Alerts", Url: path.Join(pathPrefix, "alerts")},
|
2022-02-02 12:11:41 +00:00
|
|
|
{Name: "Notifiers", Url: path.Join(pathPrefix, "notifiers")},
|
2021-09-21 11:41:01 +00:00
|
|
|
{Name: "Docs", Url: "https://docs.victoriametrics.com/vmalert.html"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 09:40:24 +00:00
|
|
|
type requestHandler struct {
|
2020-05-10 16:58:17 +00:00
|
|
|
m *manager
|
2020-04-11 09:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rh *requestHandler) handler(w http.ResponseWriter, r *http.Request) bool {
|
2021-09-21 11:41:01 +00:00
|
|
|
once.Do(func() {
|
|
|
|
initLinks()
|
|
|
|
})
|
|
|
|
|
2022-02-08 19:03:42 +00:00
|
|
|
pathPrefix := httpserver.GetPathPrefix()
|
|
|
|
if pathPrefix == "" {
|
|
|
|
pathPrefix = "/"
|
|
|
|
}
|
|
|
|
|
2020-04-11 09:40:24 +00:00
|
|
|
switch r.URL.Path {
|
2020-04-11 15:49:23 +00:00
|
|
|
case "/":
|
2021-04-02 19:54:06 +00:00
|
|
|
if r.Method != "GET" {
|
|
|
|
return false
|
|
|
|
}
|
2021-09-21 11:41:01 +00:00
|
|
|
WriteWelcome(w)
|
2020-04-11 15:49:23 +00:00
|
|
|
return true
|
2021-09-07 19:39:22 +00:00
|
|
|
case "/alerts":
|
2022-02-08 19:03:42 +00:00
|
|
|
WriteListAlerts(w, pathPrefix, rh.groupAlerts())
|
2021-09-07 19:39:22 +00:00
|
|
|
return true
|
2022-05-16 08:00:24 +00:00
|
|
|
case "/groups", "/rules":
|
2021-09-07 19:39:22 +00:00
|
|
|
WriteListGroups(w, rh.groups())
|
|
|
|
return true
|
2022-02-02 12:11:41 +00:00
|
|
|
case "/notifiers":
|
|
|
|
WriteListTargets(w, notifier.GetTargets())
|
|
|
|
return true
|
2022-03-15 11:54:53 +00:00
|
|
|
case "/api/v1/rules":
|
2020-07-20 11:00:33 +00:00
|
|
|
data, err := rh.listGroups()
|
|
|
|
if err != nil {
|
2021-07-07 09:59:03 +00:00
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
2020-07-20 11:00:33 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-11-09 16:03:50 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2020-07-20 11:00:33 +00:00
|
|
|
w.Write(data)
|
2020-06-01 10:46:37 +00:00
|
|
|
return true
|
2020-04-11 15:49:23 +00:00
|
|
|
case "/api/v1/alerts":
|
2020-07-20 11:00:33 +00:00
|
|
|
data, err := rh.listAlerts()
|
|
|
|
if err != nil {
|
2021-07-07 09:59:03 +00:00
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
2020-07-20 11:00:33 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-11-09 16:03:50 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2020-07-20 11:00:33 +00:00
|
|
|
w.Write(data)
|
2020-04-11 15:49:23 +00:00
|
|
|
return true
|
2020-05-09 09:32:12 +00:00
|
|
|
case "/-/reload":
|
|
|
|
logger.Infof("api config reload was called, sending sighup")
|
|
|
|
procutil.SelfSIGHUP()
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return true
|
2020-04-11 09:40:24 +00:00
|
|
|
default:
|
2022-05-30 23:55:28 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/static") {
|
|
|
|
staticServer.ServeHTTP(w, r)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-07-20 11:00:33 +00:00
|
|
|
if !strings.HasSuffix(r.URL.Path, "/status") {
|
|
|
|
return false
|
|
|
|
}
|
2021-09-07 19:39:22 +00:00
|
|
|
alert, err := rh.alertByPath(strings.TrimPrefix(r.URL.Path, "/api/v1/"))
|
2020-07-20 11:00:33 +00:00
|
|
|
if err != nil {
|
2021-07-07 09:59:03 +00:00
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
2020-04-11 09:40:24 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-09-07 19:39:22 +00:00
|
|
|
|
|
|
|
// /api/v1/<groupID>/<alertID>/status
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v1/") {
|
|
|
|
data, err := json.Marshal(alert)
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "failed to marshal alert: %s", err)
|
|
|
|
return true
|
|
|
|
}
|
2021-11-09 16:03:50 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2021-09-07 19:39:22 +00:00
|
|
|
w.Write(data)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// <groupID>/<alertID>/status
|
2022-02-08 19:03:42 +00:00
|
|
|
WriteAlert(w, pathPrefix, alert)
|
2020-07-20 11:00:33 +00:00
|
|
|
return true
|
2020-04-11 09:40:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 10:46:37 +00:00
|
|
|
type listGroupsResponse struct {
|
2022-03-15 11:54:53 +00:00
|
|
|
Status string `json:"status"`
|
|
|
|
Data struct {
|
2020-06-01 10:46:37 +00:00
|
|
|
Groups []APIGroup `json:"groups"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
2021-09-07 19:39:22 +00:00
|
|
|
func (rh *requestHandler) groups() []APIGroup {
|
2020-06-01 10:46:37 +00:00
|
|
|
rh.m.groupsMu.RLock()
|
|
|
|
defer rh.m.groupsMu.RUnlock()
|
|
|
|
|
2021-09-07 19:39:22 +00:00
|
|
|
var groups []APIGroup
|
2020-06-01 10:46:37 +00:00
|
|
|
for _, g := range rh.m.groups {
|
2021-09-07 19:39:22 +00:00
|
|
|
groups = append(groups, g.toAPI())
|
2020-06-01 10:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sort list of alerts for deterministic output
|
2021-09-07 19:39:22 +00:00
|
|
|
sort.Slice(groups, func(i, j int) bool {
|
|
|
|
return groups[i].Name < groups[j].Name
|
2020-06-01 10:46:37 +00:00
|
|
|
})
|
|
|
|
|
2021-09-07 19:39:22 +00:00
|
|
|
return groups
|
|
|
|
}
|
2022-03-15 11:54:53 +00:00
|
|
|
|
2021-09-07 19:39:22 +00:00
|
|
|
func (rh *requestHandler) listGroups() ([]byte, error) {
|
|
|
|
lr := listGroupsResponse{Status: "success"}
|
|
|
|
lr.Data.Groups = rh.groups()
|
2020-06-01 10:46:37 +00:00
|
|
|
b, err := json.Marshal(lr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &httpserver.ErrorWithStatusCode{
|
2020-06-30 19:58:18 +00:00
|
|
|
Err: fmt.Errorf(`error encoding list of active alerts: %w`, err),
|
2020-06-01 10:46:37 +00:00
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2020-04-12 11:51:03 +00:00
|
|
|
type listAlertsResponse struct {
|
2022-03-15 11:54:53 +00:00
|
|
|
Status string `json:"status"`
|
|
|
|
Data struct {
|
2020-04-12 12:08:11 +00:00
|
|
|
Alerts []*APIAlert `json:"alerts"`
|
2020-04-12 11:51:03 +00:00
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
2021-09-07 19:39:22 +00:00
|
|
|
func (rh *requestHandler) groupAlerts() []GroupAlerts {
|
|
|
|
rh.m.groupsMu.RLock()
|
|
|
|
defer rh.m.groupsMu.RUnlock()
|
|
|
|
|
|
|
|
var groupAlerts []GroupAlerts
|
|
|
|
for _, g := range rh.m.groups {
|
|
|
|
var alerts []*APIAlert
|
|
|
|
for _, r := range g.Rules {
|
|
|
|
a, ok := r.(*AlertingRule)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-03-15 11:54:53 +00:00
|
|
|
alerts = append(alerts, a.AlertsToAPI()...)
|
2021-09-07 19:39:22 +00:00
|
|
|
}
|
|
|
|
if len(alerts) > 0 {
|
|
|
|
groupAlerts = append(groupAlerts, GroupAlerts{
|
|
|
|
Group: g.toAPI(),
|
|
|
|
Alerts: alerts,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return groupAlerts
|
|
|
|
}
|
|
|
|
|
2020-06-01 10:46:37 +00:00
|
|
|
func (rh *requestHandler) listAlerts() ([]byte, error) {
|
2020-05-10 16:58:17 +00:00
|
|
|
rh.m.groupsMu.RLock()
|
|
|
|
defer rh.m.groupsMu.RUnlock()
|
2020-06-01 10:46:37 +00:00
|
|
|
|
2020-04-11 09:40:24 +00:00
|
|
|
lr := listAlertsResponse{Status: "success"}
|
2020-05-10 16:58:17 +00:00
|
|
|
for _, g := range rh.m.groups {
|
2020-04-11 15:49:23 +00:00
|
|
|
for _, r := range g.Rules {
|
2020-06-01 10:46:37 +00:00
|
|
|
a, ok := r.(*AlertingRule)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-03-15 11:54:53 +00:00
|
|
|
lr.Data.Alerts = append(lr.Data.Alerts, a.AlertsToAPI()...)
|
2020-04-11 09:40:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 15:49:23 +00:00
|
|
|
// sort list of alerts for deterministic output
|
|
|
|
sort.Slice(lr.Data.Alerts, func(i, j int) bool {
|
2020-05-04 21:51:22 +00:00
|
|
|
return lr.Data.Alerts[i].ID < lr.Data.Alerts[j].ID
|
2020-04-11 15:49:23 +00:00
|
|
|
})
|
|
|
|
|
2020-04-11 09:40:24 +00:00
|
|
|
b, err := json.Marshal(lr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &httpserver.ErrorWithStatusCode{
|
2020-06-30 19:58:18 +00:00
|
|
|
Err: fmt.Errorf(`error encoding list of active alerts: %w`, err),
|
2020-04-11 09:40:24 +00:00
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2021-09-07 19:39:22 +00:00
|
|
|
func (rh *requestHandler) alertByPath(path string) (*APIAlert, error) {
|
2020-05-10 16:58:17 +00:00
|
|
|
rh.m.groupsMu.RLock()
|
|
|
|
defer rh.m.groupsMu.RUnlock()
|
|
|
|
|
2021-09-07 19:39:22 +00:00
|
|
|
parts := strings.SplitN(strings.TrimLeft(path, "/"), "/", 3)
|
2020-04-11 09:40:24 +00:00
|
|
|
if len(parts) != 3 {
|
|
|
|
return nil, &httpserver.ErrorWithStatusCode{
|
2021-09-07 19:39:22 +00:00
|
|
|
Err: fmt.Errorf(`path %q cointains /status suffix but doesn't match pattern "/groupID/alertID/status"`, path),
|
2020-04-11 09:40:24 +00:00
|
|
|
StatusCode: http.StatusBadRequest,
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 16:58:17 +00:00
|
|
|
groupID, err := uint64FromPath(parts[0])
|
2020-04-11 09:40:24 +00:00
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, badRequest(fmt.Errorf(`cannot parse groupID: %w`, err))
|
2020-04-11 09:40:24 +00:00
|
|
|
}
|
2020-05-10 16:58:17 +00:00
|
|
|
alertID, err := uint64FromPath(parts[1])
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, badRequest(fmt.Errorf(`cannot parse alertID: %w`, err))
|
2020-04-11 09:40:24 +00:00
|
|
|
}
|
2020-05-10 16:58:17 +00:00
|
|
|
resp, err := rh.m.AlertAPI(groupID, alertID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errResponse(err, http.StatusNotFound)
|
2020-04-11 09:40:24 +00:00
|
|
|
}
|
2021-09-07 19:39:22 +00:00
|
|
|
return resp, nil
|
2020-04-11 09:40:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 16:58:17 +00: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,
|
|
|
|
}
|
|
|
|
}
|