2021-08-06 07:06:56 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-07-20 09:50:49 -05:00
|
|
|
"context"
|
2021-08-06 07:06:56 -05:00
|
|
|
"errors"
|
2022-07-20 09:50:49 -05:00
|
|
|
"fmt"
|
2021-08-06 07:06:56 -05:00
|
|
|
"net/http"
|
|
|
|
|
2023-01-30 02:55:35 -06:00
|
|
|
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
|
|
|
|
2021-08-06 07:06:56 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2023-01-27 01:50:36 -06:00
|
|
|
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
|
2022-07-20 09:50:49 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
2021-08-06 07:06:56 -05:00
|
|
|
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
|
|
|
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
2021-08-13 07:14:36 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2021-08-06 07:06:56 -05:00
|
|
|
)
|
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
type ConfigSrv struct {
|
2022-07-20 09:50:49 -05:00
|
|
|
datasourceService datasources.DataSourceService
|
2022-07-12 14:13:04 -05:00
|
|
|
alertmanagerProvider ExternalAlertmanagerProvider
|
|
|
|
store store.AdminConfigurationStore
|
|
|
|
log log.Logger
|
2021-08-13 07:14:36 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (srv ConfigSrv) RouteGetAlertmanagers(c *contextmodel.ReqContext) response.Response {
|
2022-08-11 06:28:55 -05:00
|
|
|
urls := srv.alertmanagerProvider.AlertmanagersFor(c.OrgID)
|
|
|
|
droppedURLs := srv.alertmanagerProvider.DroppedAlertmanagersFor(c.OrgID)
|
2021-08-13 07:14:36 -05:00
|
|
|
ams := v1.AlertManagersResult{Active: make([]v1.AlertManager, len(urls)), Dropped: make([]v1.AlertManager, len(droppedURLs))}
|
|
|
|
for i, url := range urls {
|
|
|
|
ams.Active[i].URL = url.String()
|
|
|
|
}
|
|
|
|
for i, url := range droppedURLs {
|
|
|
|
ams.Dropped[i].URL = url.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSON(http.StatusOK, apimodels.GettableAlertmanagers{
|
|
|
|
Status: "success",
|
|
|
|
Data: ams,
|
|
|
|
})
|
2021-08-06 07:06:56 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (srv ConfigSrv) RouteGetNGalertConfig(c *contextmodel.ReqContext) response.Response {
|
2022-08-10 04:56:48 -05:00
|
|
|
if c.OrgRole != org.RoleAdmin {
|
2021-08-06 07:06:56 -05:00
|
|
|
return accessForbiddenResp()
|
|
|
|
}
|
|
|
|
|
2022-08-11 06:28:55 -05:00
|
|
|
cfg, err := srv.store.GetAdminConfiguration(c.OrgID)
|
2021-08-06 07:06:56 -05:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, store.ErrNoAdminConfiguration) {
|
|
|
|
return ErrResp(http.StatusNotFound, err, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := "failed to fetch admin configuration from the database"
|
2022-10-19 16:36:54 -05:00
|
|
|
srv.log.Error(msg, "error", err)
|
2021-08-06 07:06:56 -05:00
|
|
|
return ErrResp(http.StatusInternalServerError, err, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := apimodels.GettableNGalertConfig{
|
Alerting: send alerts to external, internal, or both alertmanagers (#40341)
* (WIP) send alerts to external, internal, or both alertmanagers
* Modify admin configuration endpoint, update swagger docs
* Integration test for admin config updated
* Code review changes
* Fix alertmanagers choice not changing bug, add unit test
* Add AlertmanagersChoice as enum in swagger, code review changes
* Fix API and tests errors
* Change enum from int to string, use 'SendAlertsTo' instead of 'AlertmanagerChoice' where necessary
* Fix tests to reflect last changes
* Keep senders running when alerts are handled just internally
* Check if any external AM has been discovered before sending alerts, update tests
* remove duplicate data from logs
* update comment
* represent alertmanagers choice as an int instead of a string
* default alertmanagers choice to all alertmanagers, test cases
* update definitions and generate spec
2022-02-01 17:36:55 -06:00
|
|
|
AlertmanagersChoice: apimodels.AlertmanagersChoice(cfg.SendAlertsTo.String()),
|
2021-08-06 07:06:56 -05:00
|
|
|
}
|
|
|
|
return response.JSON(http.StatusOK, resp)
|
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (srv ConfigSrv) RoutePostNGalertConfig(c *contextmodel.ReqContext, body apimodels.PostableNGalertConfig) response.Response {
|
2022-08-10 04:56:48 -05:00
|
|
|
if c.OrgRole != org.RoleAdmin {
|
2021-08-06 07:06:56 -05:00
|
|
|
return accessForbiddenResp()
|
|
|
|
}
|
|
|
|
|
Alerting: send alerts to external, internal, or both alertmanagers (#40341)
* (WIP) send alerts to external, internal, or both alertmanagers
* Modify admin configuration endpoint, update swagger docs
* Integration test for admin config updated
* Code review changes
* Fix alertmanagers choice not changing bug, add unit test
* Add AlertmanagersChoice as enum in swagger, code review changes
* Fix API and tests errors
* Change enum from int to string, use 'SendAlertsTo' instead of 'AlertmanagerChoice' where necessary
* Fix tests to reflect last changes
* Keep senders running when alerts are handled just internally
* Check if any external AM has been discovered before sending alerts, update tests
* remove duplicate data from logs
* update comment
* represent alertmanagers choice as an int instead of a string
* default alertmanagers choice to all alertmanagers, test cases
* update definitions and generate spec
2022-02-01 17:36:55 -06:00
|
|
|
sendAlertsTo, err := ngmodels.StringToAlertmanagersChoice(string(body.AlertmanagersChoice))
|
|
|
|
if err != nil {
|
2022-07-20 09:50:49 -05:00
|
|
|
return response.Error(400, "Invalid alertmanager choice specified", err)
|
Alerting: send alerts to external, internal, or both alertmanagers (#40341)
* (WIP) send alerts to external, internal, or both alertmanagers
* Modify admin configuration endpoint, update swagger docs
* Integration test for admin config updated
* Code review changes
* Fix alertmanagers choice not changing bug, add unit test
* Add AlertmanagersChoice as enum in swagger, code review changes
* Fix API and tests errors
* Change enum from int to string, use 'SendAlertsTo' instead of 'AlertmanagerChoice' where necessary
* Fix tests to reflect last changes
* Keep senders running when alerts are handled just internally
* Check if any external AM has been discovered before sending alerts, update tests
* remove duplicate data from logs
* update comment
* represent alertmanagers choice as an int instead of a string
* default alertmanagers choice to all alertmanagers, test cases
* update definitions and generate spec
2022-02-01 17:36:55 -06:00
|
|
|
}
|
|
|
|
|
2022-08-11 06:28:55 -05:00
|
|
|
externalAlertmanagers, err := srv.externalAlertmanagers(c.Req.Context(), c.OrgID)
|
2022-07-20 09:50:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return response.Error(500, "Couldn't fetch the external Alertmanagers from datasources", err)
|
|
|
|
}
|
|
|
|
|
2022-11-10 09:34:13 -06:00
|
|
|
if sendAlertsTo == ngmodels.ExternalAlertmanagers && len(externalAlertmanagers) < 1 {
|
2022-07-20 09:50:49 -05:00
|
|
|
return response.Error(400, "At least one Alertmanager must be provided or configured as a datasource that handles alerts to choose this option", nil)
|
Alerting: send alerts to external, internal, or both alertmanagers (#40341)
* (WIP) send alerts to external, internal, or both alertmanagers
* Modify admin configuration endpoint, update swagger docs
* Integration test for admin config updated
* Code review changes
* Fix alertmanagers choice not changing bug, add unit test
* Add AlertmanagersChoice as enum in swagger, code review changes
* Fix API and tests errors
* Change enum from int to string, use 'SendAlertsTo' instead of 'AlertmanagerChoice' where necessary
* Fix tests to reflect last changes
* Keep senders running when alerts are handled just internally
* Check if any external AM has been discovered before sending alerts, update tests
* remove duplicate data from logs
* update comment
* represent alertmanagers choice as an int instead of a string
* default alertmanagers choice to all alertmanagers, test cases
* update definitions and generate spec
2022-02-01 17:36:55 -06:00
|
|
|
}
|
|
|
|
|
2021-08-06 07:06:56 -05:00
|
|
|
cfg := &ngmodels.AdminConfiguration{
|
2022-11-10 09:34:13 -06:00
|
|
|
SendAlertsTo: sendAlertsTo,
|
|
|
|
OrgID: c.OrgID,
|
2021-11-12 15:19:16 -06:00
|
|
|
}
|
|
|
|
|
2021-08-06 07:06:56 -05:00
|
|
|
cmd := store.UpdateAdminConfigurationCmd{AdminConfiguration: cfg}
|
|
|
|
if err := srv.store.UpdateAdminConfiguration(cmd); err != nil {
|
|
|
|
msg := "failed to save the admin configuration to the database"
|
2022-10-19 16:36:54 -05:00
|
|
|
srv.log.Error(msg, "error", err)
|
2021-08-06 07:06:56 -05:00
|
|
|
return ErrResp(http.StatusBadRequest, err, msg)
|
|
|
|
}
|
|
|
|
|
2021-08-13 07:14:36 -05:00
|
|
|
return response.JSON(http.StatusCreated, util.DynMap{"message": "admin configuration updated"})
|
2021-08-06 07:06:56 -05:00
|
|
|
}
|
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (srv ConfigSrv) RouteDeleteNGalertConfig(c *contextmodel.ReqContext) response.Response {
|
2022-08-10 04:56:48 -05:00
|
|
|
if c.OrgRole != org.RoleAdmin {
|
2021-08-06 07:06:56 -05:00
|
|
|
return accessForbiddenResp()
|
|
|
|
}
|
|
|
|
|
2022-08-11 06:28:55 -05:00
|
|
|
err := srv.store.DeleteAdminConfiguration(c.OrgID)
|
2021-08-06 07:06:56 -05:00
|
|
|
if err != nil {
|
2022-10-19 16:36:54 -05:00
|
|
|
srv.log.Error("unable to delete configuration", "error", err)
|
2021-08-06 07:06:56 -05:00
|
|
|
return ErrResp(http.StatusInternalServerError, err, "")
|
|
|
|
}
|
|
|
|
|
2021-08-13 07:14:36 -05:00
|
|
|
return response.JSON(http.StatusOK, util.DynMap{"message": "admin configuration deleted"})
|
2021-08-06 07:06:56 -05:00
|
|
|
}
|
2022-07-20 09:50:49 -05:00
|
|
|
|
|
|
|
// externalAlertmanagers returns the URL of any external alertmanager that is
|
|
|
|
// configured as datasource. The URL does not contain any auth.
|
|
|
|
func (srv ConfigSrv) externalAlertmanagers(ctx context.Context, orgID int64) ([]string, error) {
|
|
|
|
var alertmanagers []string
|
|
|
|
query := &datasources.GetDataSourcesByTypeQuery{
|
2023-02-02 10:22:43 -06:00
|
|
|
OrgID: orgID,
|
2022-07-20 09:50:49 -05:00
|
|
|
Type: datasources.DS_ALERTMANAGER,
|
|
|
|
}
|
2023-02-09 08:49:44 -06:00
|
|
|
dataSources, err := srv.datasourceService.GetDataSourcesByType(ctx, query)
|
2022-07-20 09:50:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to fetch datasources for org: %w", err)
|
|
|
|
}
|
2023-02-09 08:49:44 -06:00
|
|
|
for _, ds := range dataSources {
|
2022-07-20 09:50:49 -05:00
|
|
|
if ds.JsonData.Get(apimodels.HandleGrafanaManagedAlerts).MustBool(false) {
|
|
|
|
// we don't need to build the exact URL as we only need
|
|
|
|
// to know if any is set
|
2023-02-02 10:22:43 -06:00
|
|
|
alertmanagers = append(alertmanagers, ds.UID)
|
2022-07-20 09:50:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return alertmanagers, nil
|
|
|
|
}
|
2022-09-14 13:03:10 -05:00
|
|
|
|
2023-01-27 01:50:36 -06:00
|
|
|
func (srv ConfigSrv) RouteGetAlertingStatus(c *contextmodel.ReqContext) response.Response {
|
2022-09-14 13:03:10 -05:00
|
|
|
sendsAlertsTo := ngmodels.InternalAlertmanager
|
|
|
|
|
|
|
|
cfg, err := srv.store.GetAdminConfiguration(c.OrgID)
|
|
|
|
if err != nil && !errors.Is(err, store.ErrNoAdminConfiguration) {
|
|
|
|
msg := "failed to fetch configuration from the database"
|
2022-10-19 16:36:54 -05:00
|
|
|
srv.log.Error(msg, "error", err)
|
2022-09-14 13:03:10 -05:00
|
|
|
return ErrResp(http.StatusInternalServerError, err, msg)
|
|
|
|
}
|
|
|
|
if cfg != nil {
|
|
|
|
sendsAlertsTo = cfg.SendAlertsTo
|
|
|
|
}
|
|
|
|
|
2023-02-01 08:51:05 -06:00
|
|
|
// handle errors
|
|
|
|
externalAlertManagers, err := srv.externalAlertmanagers(c.Req.Context(), c.OrgID)
|
|
|
|
if err != nil {
|
|
|
|
return ErrResp(http.StatusInternalServerError, err, "")
|
|
|
|
}
|
|
|
|
|
2022-09-14 13:03:10 -05:00
|
|
|
resp := apimodels.AlertingStatus{
|
2023-02-01 08:51:05 -06:00
|
|
|
AlertmanagersChoice: apimodels.AlertmanagersChoice(sendsAlertsTo.String()),
|
|
|
|
NumExternalAlertmanagers: len(externalAlertManagers),
|
2022-09-14 13:03:10 -05:00
|
|
|
}
|
|
|
|
return response.JSON(http.StatusOK, resp)
|
|
|
|
}
|