2020-04-11 11:40:24 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-05-31 01:55:28 +02:00
|
|
|
"embed"
|
2020-04-11 11:40:24 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-04-11 17:49:23 +02:00
|
|
|
"sort"
|
2020-04-11 11:40:24 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2022-02-02 13:11:41 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/notifier"
|
2021-09-21 13:41:01 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/tpl"
|
2020-04-11 11:40:24 +02:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2021-09-21 13:41:01 +02:00
|
|
|
var (
|
|
|
|
apiLinks = [][2]string{
|
2022-07-08 10:26:13 +02:00
|
|
|
// api links are relative since they can be used by external clients,
|
|
|
|
// such as Grafana, and proxied via vmselect.
|
2022-07-06 10:46:01 +02:00
|
|
|
{"api/v1/rules", "list all loaded groups and rules"},
|
|
|
|
{"api/v1/alerts", "list all active alerts"},
|
2022-07-08 10:26:13 +02:00
|
|
|
{fmt.Sprintf("api/v1/alert?%s=<int>&%s=<int>", paramGroupID, paramAlertID), "get alert status by group and alert ID"},
|
2022-12-09 20:43:54 +01:00
|
|
|
}
|
|
|
|
systemLinks = [][2]string{
|
2022-07-06 10:46:01 +02:00
|
|
|
{"/flags", "command-line flags"},
|
|
|
|
{"/metrics", "list of application metrics"},
|
|
|
|
{"/-/reload", "reload configuration"},
|
2021-09-21 13:41:01 +02:00
|
|
|
}
|
|
|
|
navItems = []tpl.NavItem{
|
2022-07-11 18:52:22 +02:00
|
|
|
{Name: "vmalert", Url: "."},
|
2022-07-06 10:46:01 +02:00
|
|
|
{Name: "Groups", Url: "groups"},
|
|
|
|
{Name: "Alerts", Url: "alerts"},
|
|
|
|
{Name: "Notifiers", Url: "notifiers"},
|
2021-09-21 13:41:01 +02:00
|
|
|
{Name: "Docs", Url: "https://docs.victoriametrics.com/vmalert.html"},
|
|
|
|
}
|
2022-12-09 20:43:54 +01:00
|
|
|
)
|
2021-09-21 13:41:01 +02:00
|
|
|
|
2020-04-11 11:40:24 +02:00
|
|
|
type requestHandler struct {
|
2020-05-10 18:58:17 +02:00
|
|
|
m *manager
|
2020-04-11 11:40:24 +02:00
|
|
|
}
|
|
|
|
|
2022-07-06 10:46:01 +02:00
|
|
|
var (
|
|
|
|
//go:embed static
|
|
|
|
staticFiles embed.FS
|
|
|
|
staticHandler = http.FileServer(http.FS(staticFiles))
|
|
|
|
staticServer = http.StripPrefix("/vmalert", staticHandler)
|
|
|
|
)
|
|
|
|
|
2020-04-11 11:40:24 +02:00
|
|
|
func (rh *requestHandler) handler(w http.ResponseWriter, r *http.Request) bool {
|
2022-07-06 10:46:01 +02:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/vmalert/static") {
|
|
|
|
staticServer.ServeHTTP(w, r)
|
|
|
|
return true
|
2022-02-08 20:03:42 +01:00
|
|
|
}
|
|
|
|
|
2020-04-11 11:40:24 +02:00
|
|
|
switch r.URL.Path {
|
2022-07-11 18:52:22 +02:00
|
|
|
case "/", "/vmalert", "/vmalert/":
|
2023-02-23 03:58:44 +01:00
|
|
|
if r.Method != http.MethodGet {
|
2022-07-11 18:52:22 +02:00
|
|
|
httpserver.Errorf(w, r, "path %q supports only GET method", r.URL.Path)
|
2021-04-02 21:54:06 +02:00
|
|
|
return false
|
|
|
|
}
|
2022-07-06 10:46:01 +02:00
|
|
|
WriteWelcome(w, r)
|
|
|
|
return true
|
|
|
|
case "/vmalert/alerts":
|
|
|
|
WriteListAlerts(w, r, rh.groupAlerts())
|
2020-04-11 17:49:23 +02:00
|
|
|
return true
|
2022-07-08 10:26:13 +02:00
|
|
|
case "/vmalert/alert":
|
|
|
|
alert, err := rh.getAlert(r)
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
WriteAlert(w, r, alert)
|
|
|
|
return true
|
2022-09-14 14:04:24 +02:00
|
|
|
case "/vmalert/rule":
|
|
|
|
rule, err := rh.getRule(r)
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
WriteRuleDetails(w, r, rule)
|
|
|
|
return true
|
2022-07-06 10:46:01 +02:00
|
|
|
case "/vmalert/groups":
|
|
|
|
WriteListGroups(w, r, rh.groups())
|
2021-09-07 21:39:22 +02:00
|
|
|
return true
|
2022-07-06 10:46:01 +02:00
|
|
|
case "/vmalert/notifiers":
|
|
|
|
WriteListTargets(w, r, notifier.GetTargets())
|
2021-09-07 21:39:22 +02:00
|
|
|
return true
|
2022-07-06 10:46:01 +02:00
|
|
|
|
|
|
|
// special cases for Grafana requests,
|
|
|
|
// served without `vmalert` prefix:
|
|
|
|
case "/rules":
|
|
|
|
// Grafana makes an extra request to `/rules`
|
|
|
|
// handler in addition to `/api/v1/rules` calls in alerts UI,
|
|
|
|
WriteListGroups(w, r, rh.groups())
|
2022-02-02 13:11:41 +01:00
|
|
|
return true
|
2022-07-06 10:46:01 +02:00
|
|
|
|
|
|
|
case "/vmalert/api/v1/rules", "/api/v1/rules":
|
|
|
|
// path used by Grafana for ng alerting
|
2020-07-20 13:00:33 +02:00
|
|
|
data, err := rh.listGroups()
|
|
|
|
if err != nil {
|
2021-07-07 11:59:03 +02:00
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
2020-07-20 13:00:33 +02:00
|
|
|
return true
|
|
|
|
}
|
2021-11-09 17:03:50 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2020-07-20 13:00:33 +02:00
|
|
|
w.Write(data)
|
2020-06-01 12:46:37 +02:00
|
|
|
return true
|
2022-07-06 10:46:01 +02:00
|
|
|
case "/vmalert/api/v1/alerts", "/api/v1/alerts":
|
|
|
|
// path used by Grafana for ng alerting
|
2020-07-20 13:00:33 +02:00
|
|
|
data, err := rh.listAlerts()
|
|
|
|
if err != nil {
|
2021-07-07 11:59:03 +02:00
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
2020-07-20 13:00:33 +02:00
|
|
|
return true
|
|
|
|
}
|
2021-11-09 17:03:50 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2020-07-20 13:00:33 +02:00
|
|
|
w.Write(data)
|
2020-04-11 17:49:23 +02:00
|
|
|
return true
|
2022-07-08 10:26:13 +02:00
|
|
|
case "/vmalert/api/v1/alert", "/api/v1/alert":
|
|
|
|
alert, err := rh.getAlert(r)
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
data, err := json.Marshal(alert)
|
|
|
|
if err != nil {
|
|
|
|
httpserver.Errorf(w, r, "failed to marshal alert: %s", err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(data)
|
|
|
|
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
|
2022-05-31 01:55:28 +02:00
|
|
|
|
2022-07-06 10:46:01 +02:00
|
|
|
default:
|
2022-07-08 10:26:13 +02:00
|
|
|
// Support of deprecated links:
|
|
|
|
// * /api/v1/<groupID>/<alertID>/status
|
|
|
|
// * <groupID>/<alertID>/status
|
|
|
|
// TODO: to remove in next versions
|
|
|
|
|
2020-07-20 13:00:33 +02:00
|
|
|
if !strings.HasSuffix(r.URL.Path, "/status") {
|
2022-07-11 18:52:22 +02:00
|
|
|
httpserver.Errorf(w, r, "unsupported path requested: %q ", r.URL.Path)
|
2020-07-20 13:00:33 +02:00
|
|
|
return false
|
|
|
|
}
|
2021-09-07 21:39:22 +02:00
|
|
|
alert, err := rh.alertByPath(strings.TrimPrefix(r.URL.Path, "/api/v1/"))
|
2020-07-20 13:00:33 +02:00
|
|
|
if err != nil {
|
2021-07-07 11:59:03 +02:00
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
2020-04-11 11:40:24 +02:00
|
|
|
return true
|
|
|
|
}
|
2021-09-07 21:39:22 +02:00
|
|
|
|
2022-07-08 10:26:13 +02:00
|
|
|
redirectURL := alert.WebLink()
|
2021-09-07 21:39:22 +02:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v1/") {
|
2022-07-08 10:26:13 +02:00
|
|
|
redirectURL = alert.APILink()
|
2021-09-07 21:39:22 +02:00
|
|
|
}
|
2022-10-01 15:53:33 +02:00
|
|
|
httpserver.Redirect(w, "/"+redirectURL)
|
2020-07-20 13:00:33 +02:00
|
|
|
return true
|
2020-04-11 11:40:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 10:26:13 +02:00
|
|
|
const (
|
|
|
|
paramGroupID = "group_id"
|
|
|
|
paramAlertID = "alert_id"
|
2022-09-14 14:04:24 +02:00
|
|
|
paramRuleID = "rule_id"
|
2022-07-08 10:26:13 +02:00
|
|
|
)
|
|
|
|
|
2022-09-14 14:04:24 +02:00
|
|
|
func (rh *requestHandler) getRule(r *http.Request) (APIRule, error) {
|
|
|
|
groupID, err := strconv.ParseUint(r.FormValue(paramGroupID), 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return APIRule{}, fmt.Errorf("failed to read %q param: %s", paramGroupID, err)
|
|
|
|
}
|
|
|
|
ruleID, err := strconv.ParseUint(r.FormValue(paramRuleID), 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return APIRule{}, fmt.Errorf("failed to read %q param: %s", paramRuleID, err)
|
|
|
|
}
|
|
|
|
rule, err := rh.m.RuleAPI(groupID, ruleID)
|
|
|
|
if err != nil {
|
|
|
|
return APIRule{}, errResponse(err, http.StatusNotFound)
|
|
|
|
}
|
|
|
|
return rule, nil
|
|
|
|
}
|
|
|
|
|
2022-07-08 10:26:13 +02:00
|
|
|
func (rh *requestHandler) getAlert(r *http.Request) (*APIAlert, error) {
|
|
|
|
groupID, err := strconv.ParseUint(r.FormValue(paramGroupID), 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read %q param: %s", paramGroupID, err)
|
|
|
|
}
|
|
|
|
alertID, err := strconv.ParseUint(r.FormValue(paramAlertID), 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read %q param: %s", paramAlertID, err)
|
|
|
|
}
|
|
|
|
a, err := rh.m.AlertAPI(groupID, alertID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errResponse(err, http.StatusNotFound)
|
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:46:37 +02:00
|
|
|
type listGroupsResponse struct {
|
2022-03-15 12:54:53 +01:00
|
|
|
Status string `json:"status"`
|
|
|
|
Data struct {
|
2020-06-01 12:46:37 +02:00
|
|
|
Groups []APIGroup `json:"groups"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
2021-09-07 21:39:22 +02:00
|
|
|
func (rh *requestHandler) groups() []APIGroup {
|
2020-06-01 12:46:37 +02:00
|
|
|
rh.m.groupsMu.RLock()
|
|
|
|
defer rh.m.groupsMu.RUnlock()
|
|
|
|
|
2021-09-07 21:39:22 +02:00
|
|
|
var groups []APIGroup
|
2020-06-01 12:46:37 +02:00
|
|
|
for _, g := range rh.m.groups {
|
2021-09-07 21:39:22 +02:00
|
|
|
groups = append(groups, g.toAPI())
|
2020-06-01 12:46:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// sort list of alerts for deterministic output
|
2021-09-07 21:39:22 +02:00
|
|
|
sort.Slice(groups, func(i, j int) bool {
|
|
|
|
return groups[i].Name < groups[j].Name
|
2020-06-01 12:46:37 +02:00
|
|
|
})
|
|
|
|
|
2021-09-07 21:39:22 +02:00
|
|
|
return groups
|
|
|
|
}
|
2022-03-15 12:54:53 +01:00
|
|
|
|
2021-09-07 21:39:22 +02:00
|
|
|
func (rh *requestHandler) listGroups() ([]byte, error) {
|
|
|
|
lr := listGroupsResponse{Status: "success"}
|
|
|
|
lr.Data.Groups = rh.groups()
|
2020-06-01 12:46:37 +02:00
|
|
|
b, err := json.Marshal(lr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &httpserver.ErrorWithStatusCode{
|
2020-06-30 21:58:18 +02:00
|
|
|
Err: fmt.Errorf(`error encoding list of active alerts: %w`, err),
|
2020-06-01 12:46:37 +02:00
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2020-04-12 13:51:03 +02:00
|
|
|
type listAlertsResponse struct {
|
2022-03-15 12:54:53 +01:00
|
|
|
Status string `json:"status"`
|
|
|
|
Data struct {
|
2020-04-12 14:08:11 +02:00
|
|
|
Alerts []*APIAlert `json:"alerts"`
|
2020-04-12 13:51:03 +02:00
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
2021-09-07 21:39:22 +02: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 12:54:53 +01:00
|
|
|
alerts = append(alerts, a.AlertsToAPI()...)
|
2021-09-07 21:39:22 +02:00
|
|
|
}
|
|
|
|
if len(alerts) > 0 {
|
|
|
|
groupAlerts = append(groupAlerts, GroupAlerts{
|
|
|
|
Group: g.toAPI(),
|
|
|
|
Alerts: alerts,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-08-09 09:51:29 +02:00
|
|
|
sort.Slice(groupAlerts, func(i, j int) bool {
|
|
|
|
return groupAlerts[i].Group.Name < groupAlerts[j].Group.Name
|
|
|
|
})
|
2021-09-07 21:39:22 +02:00
|
|
|
return groupAlerts
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:46:37 +02:00
|
|
|
func (rh *requestHandler) listAlerts() ([]byte, error) {
|
2020-05-10 18:58:17 +02:00
|
|
|
rh.m.groupsMu.RLock()
|
|
|
|
defer rh.m.groupsMu.RUnlock()
|
2020-06-01 12:46:37 +02:00
|
|
|
|
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 {
|
2020-06-01 12:46:37 +02:00
|
|
|
a, ok := r.(*AlertingRule)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-03-15 12:54:53 +01:00
|
|
|
lr.Data.Alerts = append(lr.Data.Alerts, a.AlertsToAPI()...)
|
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{
|
2020-06-30 21:58:18 +02:00
|
|
|
Err: fmt.Errorf(`error encoding list of active alerts: %w`, err),
|
2020-04-11 11:40:24 +02:00
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2021-09-07 21:39:22 +02:00
|
|
|
func (rh *requestHandler) alertByPath(path string) (*APIAlert, error) {
|
2022-07-08 10:26:13 +02:00
|
|
|
if strings.HasPrefix(path, "/vmalert") {
|
|
|
|
path = strings.TrimLeft(path, "/vmalert")
|
|
|
|
}
|
|
|
|
parts := strings.SplitN(strings.TrimLeft(path, "/"), "/", -1)
|
2020-04-11 11:40:24 +02:00
|
|
|
if len(parts) != 3 {
|
|
|
|
return nil, &httpserver.ErrorWithStatusCode{
|
2021-09-07 21:39:22 +02:00
|
|
|
Err: fmt.Errorf(`path %q cointains /status suffix but doesn't match pattern "/groupID/alertID/status"`, path),
|
2020-04-11 11:40:24 +02:00
|
|
|
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-06-30 21:58:18 +02:00
|
|
|
return nil, badRequest(fmt.Errorf(`cannot parse groupID: %w`, 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 {
|
2020-06-30 21:58:18 +02:00
|
|
|
return nil, badRequest(fmt.Errorf(`cannot parse alertID: %w`, 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
|
|
|
}
|
2021-09-07 21:39:22 +02:00
|
|
|
return resp, nil
|
2020-04-11 11:40:24 +02:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|