alerting.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package api
  2. import (
  3. "fmt"
  4. "strconv"
  5. "github.com/grafana/grafana/pkg/api/dtos"
  6. "github.com/grafana/grafana/pkg/bus"
  7. "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/services/alerting"
  9. "github.com/grafana/grafana/pkg/services/guardian"
  10. "github.com/grafana/grafana/pkg/services/search"
  11. )
  12. func ValidateOrgAlert(c *models.ReqContext) {
  13. id := c.ParamsInt64(":alertId")
  14. query := models.GetAlertByIdQuery{Id: id}
  15. if err := bus.Dispatch(&query); err != nil {
  16. c.JsonApiErr(404, "Alert not found", nil)
  17. return
  18. }
  19. if c.OrgId != query.Result.OrgId {
  20. c.JsonApiErr(403, "You are not allowed to edit/view alert", nil)
  21. return
  22. }
  23. }
  24. func GetAlertStatesForDashboard(c *models.ReqContext) Response {
  25. dashboardID := c.QueryInt64("dashboardId")
  26. if dashboardID == 0 {
  27. return Error(400, "Missing query parameter dashboardId", nil)
  28. }
  29. query := models.GetAlertStatesForDashboardQuery{
  30. OrgId: c.OrgId,
  31. DashboardId: c.QueryInt64("dashboardId"),
  32. }
  33. if err := bus.Dispatch(&query); err != nil {
  34. return Error(500, "Failed to fetch alert states", err)
  35. }
  36. return JSON(200, query.Result)
  37. }
  38. // GET /api/alerts
  39. func GetAlerts(c *models.ReqContext) Response {
  40. dashboardQuery := c.Query("dashboardQuery")
  41. dashboardTags := c.QueryStrings("dashboardTag")
  42. stringDashboardIDs := c.QueryStrings("dashboardId")
  43. stringFolderIDs := c.QueryStrings("folderId")
  44. dashboardIDs := make([]int64, 0)
  45. for _, id := range stringDashboardIDs {
  46. dashboardID, err := strconv.ParseInt(id, 10, 64)
  47. if err == nil {
  48. dashboardIDs = append(dashboardIDs, dashboardID)
  49. }
  50. }
  51. if dashboardQuery != "" || len(dashboardTags) > 0 || len(stringFolderIDs) > 0 {
  52. folderIDs := make([]int64, 0)
  53. for _, id := range stringFolderIDs {
  54. folderID, err := strconv.ParseInt(id, 10, 64)
  55. if err == nil {
  56. folderIDs = append(folderIDs, folderID)
  57. }
  58. }
  59. searchQuery := search.Query{
  60. Title: dashboardQuery,
  61. Tags: dashboardTags,
  62. SignedInUser: c.SignedInUser,
  63. Limit: 1000,
  64. OrgId: c.OrgId,
  65. DashboardIds: dashboardIDs,
  66. Type: string(search.DashHitDB),
  67. FolderIds: folderIDs,
  68. Permission: models.PERMISSION_VIEW,
  69. }
  70. err := bus.Dispatch(&searchQuery)
  71. if err != nil {
  72. return Error(500, "List alerts failed", err)
  73. }
  74. for _, d := range searchQuery.Result {
  75. if d.Type == search.DashHitDB && d.Id > 0 {
  76. dashboardIDs = append(dashboardIDs, d.Id)
  77. }
  78. }
  79. // if we didn't find any dashboards, return empty result
  80. if len(dashboardIDs) == 0 {
  81. return JSON(200, []*models.AlertListItemDTO{})
  82. }
  83. }
  84. query := models.GetAlertsQuery{
  85. OrgId: c.OrgId,
  86. DashboardIDs: dashboardIDs,
  87. PanelId: c.QueryInt64("panelId"),
  88. Limit: c.QueryInt64("limit"),
  89. User: c.SignedInUser,
  90. Query: c.Query("query"),
  91. }
  92. states := c.QueryStrings("state")
  93. if len(states) > 0 {
  94. query.State = states
  95. }
  96. if err := bus.Dispatch(&query); err != nil {
  97. return Error(500, "List alerts failed", err)
  98. }
  99. for _, alert := range query.Result {
  100. alert.Url = models.GetDashboardUrl(alert.DashboardUid, alert.DashboardSlug)
  101. }
  102. return JSON(200, query.Result)
  103. }
  104. // POST /api/alerts/test
  105. func AlertTest(c *models.ReqContext, dto dtos.AlertTestCommand) Response {
  106. if _, idErr := dto.Dashboard.Get("id").Int64(); idErr != nil {
  107. return Error(400, "The dashboard needs to be saved at least once before you can test an alert rule", nil)
  108. }
  109. backendCmd := alerting.AlertTestCommand{
  110. OrgID: c.OrgId,
  111. Dashboard: dto.Dashboard,
  112. PanelID: dto.PanelId,
  113. User: c.SignedInUser,
  114. }
  115. if err := bus.Dispatch(&backendCmd); err != nil {
  116. if validationErr, ok := err.(alerting.ValidationError); ok {
  117. return Error(422, validationErr.Error(), nil)
  118. }
  119. if err == models.ErrDataSourceAccessDenied {
  120. return Error(403, "Access denied to datasource", err)
  121. }
  122. return Error(500, "Failed to test rule", err)
  123. }
  124. res := backendCmd.Result
  125. dtoRes := &dtos.AlertTestResult{
  126. Firing: res.Firing,
  127. ConditionEvals: res.ConditionEvals,
  128. State: res.Rule.State,
  129. }
  130. if res.Error != nil {
  131. dtoRes.Error = res.Error.Error()
  132. }
  133. for _, log := range res.Logs {
  134. dtoRes.Logs = append(dtoRes.Logs, &dtos.AlertTestResultLog{Message: log.Message, Data: log.Data})
  135. }
  136. for _, match := range res.EvalMatches {
  137. dtoRes.EvalMatches = append(dtoRes.EvalMatches, &dtos.EvalMatch{Metric: match.Metric, Value: match.Value})
  138. }
  139. dtoRes.TimeMs = fmt.Sprintf("%1.3fms", res.GetDurationMs())
  140. return JSON(200, dtoRes)
  141. }
  142. // GET /api/alerts/:id
  143. func GetAlert(c *models.ReqContext) Response {
  144. id := c.ParamsInt64(":alertId")
  145. query := models.GetAlertByIdQuery{Id: id}
  146. if err := bus.Dispatch(&query); err != nil {
  147. return Error(500, "List alerts failed", err)
  148. }
  149. return JSON(200, &query.Result)
  150. }
  151. func GetAlertNotifiers(c *models.ReqContext) Response {
  152. return JSON(200, alerting.GetNotifiers())
  153. }
  154. func GetAlertNotificationLookup(c *models.ReqContext) Response {
  155. alertNotifications, err := getAlertNotificationsInternal(c)
  156. if err != nil {
  157. return Error(500, "Failed to get alert notifications", err)
  158. }
  159. result := make([]*dtos.AlertNotificationLookup, 0)
  160. for _, notification := range alertNotifications {
  161. result = append(result, dtos.NewAlertNotificationLookup(notification))
  162. }
  163. return JSON(200, result)
  164. }
  165. func GetAlertNotifications(c *models.ReqContext) Response {
  166. alertNotifications, err := getAlertNotificationsInternal(c)
  167. if err != nil {
  168. return Error(500, "Failed to get alert notifications", err)
  169. }
  170. result := make([]*dtos.AlertNotification, 0)
  171. for _, notification := range alertNotifications {
  172. result = append(result, dtos.NewAlertNotification(notification))
  173. }
  174. return JSON(200, result)
  175. }
  176. func getAlertNotificationsInternal(c *models.ReqContext) ([]*models.AlertNotification, error) {
  177. query := &models.GetAllAlertNotificationsQuery{OrgId: c.OrgId}
  178. if err := bus.Dispatch(query); err != nil {
  179. return nil, err
  180. }
  181. return query.Result, nil
  182. }
  183. func GetAlertNotificationByID(c *models.ReqContext) Response {
  184. query := &models.GetAlertNotificationsQuery{
  185. OrgId: c.OrgId,
  186. Id: c.ParamsInt64("notificationId"),
  187. }
  188. if query.Id == 0 {
  189. return Error(404, "Alert notification not found", nil)
  190. }
  191. if err := bus.Dispatch(query); err != nil {
  192. return Error(500, "Failed to get alert notifications", err)
  193. }
  194. if query.Result == nil {
  195. return Error(404, "Alert notification not found", nil)
  196. }
  197. return JSON(200, dtos.NewAlertNotification(query.Result))
  198. }
  199. func GetAlertNotificationByUID(c *models.ReqContext) Response {
  200. query := &models.GetAlertNotificationsWithUidQuery{
  201. OrgId: c.OrgId,
  202. Uid: c.Params("uid"),
  203. }
  204. if query.Uid == "" {
  205. return Error(404, "Alert notification not found", nil)
  206. }
  207. if err := bus.Dispatch(query); err != nil {
  208. return Error(500, "Failed to get alert notifications", err)
  209. }
  210. if query.Result == nil {
  211. return Error(404, "Alert notification not found", nil)
  212. }
  213. return JSON(200, dtos.NewAlertNotification(query.Result))
  214. }
  215. func CreateAlertNotification(c *models.ReqContext, cmd models.CreateAlertNotificationCommand) Response {
  216. cmd.OrgId = c.OrgId
  217. if err := bus.Dispatch(&cmd); err != nil {
  218. return Error(500, "Failed to create alert notification", err)
  219. }
  220. return JSON(200, dtos.NewAlertNotification(cmd.Result))
  221. }
  222. func UpdateAlertNotification(c *models.ReqContext, cmd models.UpdateAlertNotificationCommand) Response {
  223. cmd.OrgId = c.OrgId
  224. if err := bus.Dispatch(&cmd); err != nil {
  225. return Error(500, "Failed to update alert notification", err)
  226. }
  227. if cmd.Result == nil {
  228. return Error(404, "Alert notification not found", nil)
  229. }
  230. return JSON(200, dtos.NewAlertNotification(cmd.Result))
  231. }
  232. func UpdateAlertNotificationByUID(c *models.ReqContext, cmd models.UpdateAlertNotificationWithUidCommand) Response {
  233. cmd.OrgId = c.OrgId
  234. cmd.Uid = c.Params("uid")
  235. if err := bus.Dispatch(&cmd); err != nil {
  236. return Error(500, "Failed to update alert notification", err)
  237. }
  238. if cmd.Result == nil {
  239. return Error(404, "Alert notification not found", nil)
  240. }
  241. return JSON(200, dtos.NewAlertNotification(cmd.Result))
  242. }
  243. func DeleteAlertNotification(c *models.ReqContext) Response {
  244. cmd := models.DeleteAlertNotificationCommand{
  245. OrgId: c.OrgId,
  246. Id: c.ParamsInt64("notificationId"),
  247. }
  248. if err := bus.Dispatch(&cmd); err != nil {
  249. return Error(500, "Failed to delete alert notification", err)
  250. }
  251. return Success("Notification deleted")
  252. }
  253. func DeleteAlertNotificationByUID(c *models.ReqContext) Response {
  254. cmd := models.DeleteAlertNotificationWithUidCommand{
  255. OrgId: c.OrgId,
  256. Uid: c.Params("uid"),
  257. }
  258. if err := bus.Dispatch(&cmd); err != nil {
  259. return Error(500, "Failed to delete alert notification", err)
  260. }
  261. return Success("Notification deleted")
  262. }
  263. //POST /api/alert-notifications/test
  264. func NotificationTest(c *models.ReqContext, dto dtos.NotificationTestCommand) Response {
  265. cmd := &alerting.NotificationTestCommand{
  266. Name: dto.Name,
  267. Type: dto.Type,
  268. Settings: dto.Settings,
  269. }
  270. if err := bus.Dispatch(cmd); err != nil {
  271. if err == models.ErrSmtpNotEnabled {
  272. return Error(412, err.Error(), err)
  273. }
  274. return Error(500, "Failed to send alert notifications", err)
  275. }
  276. return Success("Test notification sent")
  277. }
  278. //POST /api/alerts/:alertId/pause
  279. func PauseAlert(c *models.ReqContext, dto dtos.PauseAlertCommand) Response {
  280. alertID := c.ParamsInt64("alertId")
  281. query := models.GetAlertByIdQuery{Id: alertID}
  282. if err := bus.Dispatch(&query); err != nil {
  283. return Error(500, "Get Alert failed", err)
  284. }
  285. guardian := guardian.New(query.Result.DashboardId, c.OrgId, c.SignedInUser)
  286. if canEdit, err := guardian.CanEdit(); err != nil || !canEdit {
  287. if err != nil {
  288. return Error(500, "Error while checking permissions for Alert", err)
  289. }
  290. return Error(403, "Access denied to this dashboard and alert", nil)
  291. }
  292. cmd := models.PauseAlertCommand{
  293. OrgId: c.OrgId,
  294. AlertIds: []int64{alertID},
  295. Paused: dto.Paused,
  296. }
  297. if err := bus.Dispatch(&cmd); err != nil {
  298. return Error(500, "", err)
  299. }
  300. var response models.AlertStateType = models.AlertStateUnknown
  301. pausedState := "un-paused"
  302. if cmd.Paused {
  303. response = models.AlertStatePaused
  304. pausedState = "paused"
  305. }
  306. result := map[string]interface{}{
  307. "alertId": alertID,
  308. "state": response,
  309. "message": "Alert " + pausedState,
  310. }
  311. return JSON(200, result)
  312. }
  313. //POST /api/admin/pause-all-alerts
  314. func PauseAllAlerts(c *models.ReqContext, dto dtos.PauseAllAlertsCommand) Response {
  315. updateCmd := models.PauseAllAlertCommand{
  316. Paused: dto.Paused,
  317. }
  318. if err := bus.Dispatch(&updateCmd); err != nil {
  319. return Error(500, "Failed to pause alerts", err)
  320. }
  321. var response models.AlertStateType = models.AlertStatePending
  322. pausedState := "un paused"
  323. if updateCmd.Paused {
  324. response = models.AlertStatePaused
  325. pausedState = "paused"
  326. }
  327. result := map[string]interface{}{
  328. "state": response,
  329. "message": "alerts " + pausedState,
  330. "alertsAffected": updateCmd.ResultCount,
  331. }
  332. return JSON(200, result)
  333. }