2016-04-26 10:36:50 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-07-21 03:29:11 -05:00
|
|
|
"fmt"
|
|
|
|
|
2016-04-27 06:02:28 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2016-04-26 10:36:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-07-20 09:13:36 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/alerting"
|
2016-04-26 10:36:50 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func ValidateOrgAlert(c *middleware.Context) {
|
2016-05-02 09:07:19 -05:00
|
|
|
id := c.ParamsInt64(":alertId")
|
2016-04-28 01:23:50 -05:00
|
|
|
query := models.GetAlertByIdQuery{Id: id}
|
2016-04-26 10:36:50 -05:00
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
c.JsonApiErr(404, "Alert not found", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.OrgId != query.Result.OrgId {
|
|
|
|
c.JsonApiErr(403, "You are not allowed to edit/view alert", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-30 10:37:47 -05:00
|
|
|
func GetAlertStatesForDashboard(c *middleware.Context) Response {
|
|
|
|
dashboardId := c.QueryInt64("dashboardId")
|
|
|
|
|
|
|
|
if dashboardId == 0 {
|
|
|
|
return ApiError(400, "Missing query parameter dashboardId", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := models.GetAlertStatesForDashboardQuery{
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
DashboardId: c.QueryInt64("dashboardId"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
return ApiError(500, "Failed to fetch alert states", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, query.Result)
|
|
|
|
}
|
|
|
|
|
2016-09-14 01:36:44 -05:00
|
|
|
// GET /api/alerts
|
2016-04-26 10:36:50 -05:00
|
|
|
func GetAlerts(c *middleware.Context) Response {
|
|
|
|
query := models.GetAlertsQuery{
|
2016-05-10 02:45:56 -05:00
|
|
|
OrgId: c.OrgId,
|
|
|
|
DashboardId: c.QueryInt64("dashboardId"),
|
|
|
|
PanelId: c.QueryInt64("panelId"),
|
2016-09-14 01:36:44 -05:00
|
|
|
Limit: c.QueryInt64("limit"),
|
2016-04-26 10:36:50 -05:00
|
|
|
}
|
|
|
|
|
2016-09-14 07:12:19 -05:00
|
|
|
states := c.QueryStrings("state")
|
|
|
|
if len(states) > 0 {
|
|
|
|
query.State = states
|
|
|
|
}
|
|
|
|
|
2016-04-26 10:36:50 -05:00
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
return ApiError(500, "List alerts failed", err)
|
|
|
|
}
|
|
|
|
|
2016-04-27 06:02:28 -05:00
|
|
|
dashboardIds := make([]int64, 0)
|
2016-07-22 09:45:17 -05:00
|
|
|
alertDTOs := make([]*dtos.AlertRule, 0)
|
2016-04-27 06:02:28 -05:00
|
|
|
for _, alert := range query.Result {
|
|
|
|
dashboardIds = append(dashboardIds, alert.DashboardId)
|
2016-07-22 09:45:17 -05:00
|
|
|
alertDTOs = append(alertDTOs, &dtos.AlertRule{
|
2016-08-16 02:52:45 -05:00
|
|
|
Id: alert.Id,
|
|
|
|
DashboardId: alert.DashboardId,
|
|
|
|
PanelId: alert.PanelId,
|
|
|
|
Name: alert.Name,
|
|
|
|
Message: alert.Message,
|
|
|
|
State: alert.State,
|
|
|
|
EvalDate: alert.EvalDate,
|
|
|
|
NewStateDate: alert.NewStateDate,
|
|
|
|
ExecutionError: alert.ExecutionError,
|
2016-04-27 06:02:28 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
dashboardsQuery := models.GetDashboardsQuery{
|
|
|
|
DashboardIds: dashboardIds,
|
|
|
|
}
|
|
|
|
|
2016-05-09 07:30:28 -05:00
|
|
|
if len(alertDTOs) > 0 {
|
|
|
|
if err := bus.Dispatch(&dashboardsQuery); err != nil {
|
|
|
|
return ApiError(500, "List alerts failed", err)
|
|
|
|
}
|
2016-04-27 06:02:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: should be possible to speed this up with lookup table
|
|
|
|
for _, alert := range alertDTOs {
|
2016-06-23 09:30:12 -05:00
|
|
|
for _, dash := range dashboardsQuery.Result {
|
2016-04-27 06:02:28 -05:00
|
|
|
if alert.DashboardId == dash.Id {
|
|
|
|
alert.DashbboardUri = "db/" + dash.Slug
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, alertDTOs)
|
2016-04-26 10:36:50 -05:00
|
|
|
}
|
|
|
|
|
2016-07-20 09:13:36 -05:00
|
|
|
// POST /api/alerts/test
|
2016-07-21 03:29:11 -05:00
|
|
|
func AlertTest(c *middleware.Context, dto dtos.AlertTestCommand) Response {
|
2016-11-23 07:55:10 -06:00
|
|
|
if _, idErr := dto.Dashboard.Get("id").Int64(); idErr != nil {
|
|
|
|
return ApiError(400, "The dashboard needs to be saved at least once before you can test an alert rule", nil)
|
|
|
|
}
|
|
|
|
|
2016-07-21 03:29:11 -05:00
|
|
|
backendCmd := alerting.AlertTestCommand{
|
2016-07-20 09:13:36 -05:00
|
|
|
OrgId: c.OrgId,
|
|
|
|
Dashboard: dto.Dashboard,
|
|
|
|
PanelId: dto.PanelId,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&backendCmd); err != nil {
|
2016-07-27 09:29:28 -05:00
|
|
|
if validationErr, ok := err.(alerting.ValidationError); ok {
|
2016-07-21 06:09:12 -05:00
|
|
|
return ApiError(422, validationErr.Error(), nil)
|
|
|
|
}
|
2016-07-20 09:13:36 -05:00
|
|
|
return ApiError(500, "Failed to test rule", err)
|
|
|
|
}
|
|
|
|
|
2016-07-21 03:29:11 -05:00
|
|
|
res := backendCmd.Result
|
|
|
|
|
|
|
|
dtoRes := &dtos.AlertTestResult{
|
2016-11-17 08:48:15 -06:00
|
|
|
Firing: res.Firing,
|
|
|
|
ConditionEvals: res.ConditionEvals,
|
2016-07-21 03:29:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if res.Error != nil {
|
|
|
|
dtoRes.Error = res.Error.Error()
|
|
|
|
}
|
|
|
|
|
2016-07-21 06:09:12 -05:00
|
|
|
for _, log := range res.Logs {
|
|
|
|
dtoRes.Logs = append(dtoRes.Logs, &dtos.AlertTestResultLog{Message: log.Message, Data: log.Data})
|
|
|
|
}
|
2016-08-18 04:22:24 -05:00
|
|
|
for _, match := range res.EvalMatches {
|
|
|
|
dtoRes.EvalMatches = append(dtoRes.EvalMatches, &dtos.EvalMatch{Metric: match.Metric, Value: match.Value})
|
|
|
|
}
|
2016-07-21 06:09:12 -05:00
|
|
|
|
2016-07-26 05:29:52 -05:00
|
|
|
dtoRes.TimeMs = fmt.Sprintf("%1.3fms", res.GetDurationMs())
|
2016-07-21 03:29:11 -05:00
|
|
|
|
|
|
|
return Json(200, dtoRes)
|
2016-07-20 09:13:36 -05:00
|
|
|
}
|
|
|
|
|
2016-04-28 01:23:50 -05:00
|
|
|
// GET /api/alerts/:id
|
2016-04-26 10:36:50 -05:00
|
|
|
func GetAlert(c *middleware.Context) Response {
|
2016-05-02 11:00:10 -05:00
|
|
|
id := c.ParamsInt64(":alertId")
|
2016-04-28 01:23:50 -05:00
|
|
|
query := models.GetAlertByIdQuery{Id: id}
|
2016-04-26 10:36:50 -05:00
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
return ApiError(500, "List alerts failed", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, &query.Result)
|
|
|
|
}
|
2016-04-28 01:23:50 -05:00
|
|
|
|
2016-05-02 09:07:19 -05:00
|
|
|
// DEL /api/alerts/:id
|
|
|
|
func DelAlert(c *middleware.Context) Response {
|
|
|
|
alertId := c.ParamsInt64(":alertId")
|
|
|
|
|
|
|
|
if alertId == 0 {
|
|
|
|
return ApiError(401, "Failed to parse alertid", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := models.DeleteAlertCommand{AlertId: alertId}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
|
|
return ApiError(500, "Failed to delete alert", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp = map[string]interface{}{"alertId": alertId}
|
|
|
|
return Json(200, resp)
|
|
|
|
}
|
|
|
|
|
2016-06-15 03:48:04 -05:00
|
|
|
func GetAlertNotifications(c *middleware.Context) Response {
|
2016-09-06 01:42:35 -05:00
|
|
|
query := &models.GetAllAlertNotificationsQuery{OrgId: c.OrgId}
|
2016-06-15 03:48:04 -05:00
|
|
|
|
|
|
|
if err := bus.Dispatch(query); err != nil {
|
|
|
|
return ApiError(500, "Failed to get alert notifications", err)
|
|
|
|
}
|
|
|
|
|
2016-11-06 08:40:02 -06:00
|
|
|
result := make([]*dtos.AlertNotification, 0)
|
2016-06-20 04:31:20 -05:00
|
|
|
|
|
|
|
for _, notification := range query.Result {
|
2016-11-06 08:40:02 -06:00
|
|
|
result = append(result, &dtos.AlertNotification{
|
2016-09-05 14:33:05 -05:00
|
|
|
Id: notification.Id,
|
|
|
|
Name: notification.Name,
|
|
|
|
Type: notification.Type,
|
|
|
|
IsDefault: notification.IsDefault,
|
|
|
|
Created: notification.Created,
|
|
|
|
Updated: notification.Updated,
|
2016-06-20 04:31:20 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, result)
|
2016-06-15 03:48:04 -05:00
|
|
|
}
|
|
|
|
|
2016-06-16 07:29:20 -05:00
|
|
|
func GetAlertNotificationById(c *middleware.Context) Response {
|
2016-07-22 09:45:17 -05:00
|
|
|
query := &models.GetAlertNotificationsQuery{
|
|
|
|
OrgId: c.OrgId,
|
2016-06-16 07:29:20 -05:00
|
|
|
Id: c.ParamsInt64("notificationId"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(query); err != nil {
|
|
|
|
return ApiError(500, "Failed to get alert notifications", err)
|
|
|
|
}
|
|
|
|
|
2016-09-06 01:42:35 -05:00
|
|
|
return Json(200, query.Result)
|
2016-06-16 07:29:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateAlertNotification(c *middleware.Context, cmd models.CreateAlertNotificationCommand) Response {
|
2016-07-22 09:45:17 -05:00
|
|
|
cmd.OrgId = c.OrgId
|
2016-06-15 03:48:04 -05:00
|
|
|
|
2016-06-16 07:29:20 -05:00
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2016-06-15 03:48:04 -05:00
|
|
|
return ApiError(500, "Failed to create alert notification", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, cmd.Result)
|
|
|
|
}
|
|
|
|
|
2016-06-16 07:29:20 -05:00
|
|
|
func UpdateAlertNotification(c *middleware.Context, cmd models.UpdateAlertNotificationCommand) Response {
|
2016-07-22 09:45:17 -05:00
|
|
|
cmd.OrgId = c.OrgId
|
2016-06-15 03:48:04 -05:00
|
|
|
|
2016-06-16 07:29:20 -05:00
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2016-06-15 03:48:04 -05:00
|
|
|
return ApiError(500, "Failed to update alert notification", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, cmd.Result)
|
|
|
|
}
|
2016-06-16 08:21:44 -05:00
|
|
|
|
|
|
|
func DeleteAlertNotification(c *middleware.Context) Response {
|
|
|
|
cmd := models.DeleteAlertNotificationCommand{
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
Id: c.ParamsInt64("notificationId"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
|
|
return ApiError(500, "Failed to delete alert notification", err)
|
|
|
|
}
|
|
|
|
|
2016-07-22 09:45:17 -05:00
|
|
|
return ApiSuccess("Notification deleted")
|
2016-06-16 08:21:44 -05:00
|
|
|
}
|
2016-08-30 02:32:56 -05:00
|
|
|
|
2016-09-05 07:43:53 -05:00
|
|
|
//POST /api/alert-notifications/test
|
|
|
|
func NotificationTest(c *middleware.Context, dto dtos.NotificationTestCommand) Response {
|
|
|
|
cmd := &alerting.NotificationTestCommand{
|
|
|
|
Name: dto.Name,
|
|
|
|
Type: dto.Type,
|
|
|
|
Settings: dto.Settings,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(cmd); err != nil {
|
|
|
|
return ApiError(500, "Failed to send alert notifications", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ApiSuccess("Test notification sent")
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:06:29 -05:00
|
|
|
//POST /api/alerts/:alertId/pause
|
2016-10-11 01:40:38 -05:00
|
|
|
func PauseAlert(c *middleware.Context, dto dtos.PauseAlertCommand) Response {
|
|
|
|
cmd := models.PauseAlertCommand{
|
2016-12-19 09:05:24 -06:00
|
|
|
OrgId: c.OrgId,
|
|
|
|
AlertId: c.ParamsInt64("alertId"),
|
|
|
|
Paused: dto.Paused,
|
2016-10-10 07:26:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
|
|
|
return ApiError(500, "", err)
|
|
|
|
}
|
|
|
|
|
2016-11-01 09:58:38 -05:00
|
|
|
var response models.AlertStateType = models.AlertStatePending
|
2016-10-11 02:45:38 -05:00
|
|
|
pausedState := "un paused"
|
2016-10-10 07:26:09 -05:00
|
|
|
if cmd.Paused {
|
2016-10-10 08:17:14 -05:00
|
|
|
response = models.AlertStatePaused
|
2016-10-11 02:45:38 -05:00
|
|
|
pausedState = "paused"
|
2016-10-10 07:26:09 -05:00
|
|
|
}
|
|
|
|
|
2016-10-10 08:17:14 -05:00
|
|
|
result := map[string]interface{}{
|
2016-12-19 09:05:24 -06:00
|
|
|
"alertId": c.ParamsInt64("alertId"),
|
2016-10-10 08:17:14 -05:00
|
|
|
"state": response,
|
2016-10-11 02:45:38 -05:00
|
|
|
"message": "alert " + pausedState,
|
2016-10-10 08:17:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, result)
|
2016-10-10 07:26:09 -05:00
|
|
|
}
|
2016-12-15 10:01:45 -06:00
|
|
|
|
2016-12-19 07:36:44 -06:00
|
|
|
//POST /api/admin/pause-all-alerts
|
|
|
|
func PauseAllAlerts(c *middleware.Context, dto dtos.PauseAllAlertsCommand) Response {
|
2016-12-19 06:24:45 -06:00
|
|
|
updateCmd := models.PauseAllAlertCommand{
|
2016-12-16 09:07:23 -06:00
|
|
|
Paused: dto.Paused,
|
2016-12-15 10:01:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&updateCmd); err != nil {
|
2016-12-16 08:19:13 -06:00
|
|
|
return ApiError(500, "Failed to pause alerts", err)
|
2016-12-15 10:01:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var response models.AlertStateType = models.AlertStatePending
|
|
|
|
pausedState := "un paused"
|
|
|
|
if updateCmd.Paused {
|
|
|
|
response = models.AlertStatePaused
|
|
|
|
pausedState = "paused"
|
|
|
|
}
|
|
|
|
|
|
|
|
result := map[string]interface{}{
|
|
|
|
"state": response,
|
|
|
|
"message": "alert " + pausedState,
|
|
|
|
"alertsAffected": updateCmd.ResultCount,
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, result)
|
|
|
|
}
|