alerting.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. m "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 *m.ReqContext) {
  13. id := c.ParamsInt64(":alertId")
  14. query := m.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 *m.ReqContext) Response {
  25. dashboardID := c.QueryInt64("dashboardId")
  26. if dashboardID == 0 {
  27. return Error(400, "Missing query parameter dashboardId", nil)
  28. }
  29. query := m.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 *m.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: m.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, []*m.AlertListItemDTO{})
  82. }
  83. }
  84. query := m.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 = m.GetDashboardUrl(alert.DashboardUid, alert.DashboardSlug)
  101. }
  102. return JSON(200, query.Result)
  103. }
  104. // POST /api/alerts/test
  105. func AlertTest(c *m.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 == m.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 *m.ReqContext) Response {
  144. id := c.ParamsInt64(":alertId")
  145. query := m.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 *m.ReqContext) Response {
  152. return JSON(200, alerting.GetNotifiers())
  153. }
  154. func GetAlertNotifications(c *m.ReqContext) Response {
  155. query := &m.GetAllAlertNotificationsQuery{OrgId: c.OrgId}
  156. if err := bus.Dispatch(query); err != nil {
  157. return Error(500, "Failed to get alert notifications", err)
  158. }
  159. result := make([]*dtos.AlertNotification, 0)
  160. for _, notification := range query.Result {
  161. result = append(result, dtos.NewAlertNotification(notification))
  162. }
  163. return JSON(200, result)
  164. }
  165. func GetAlertNotificationByID(c *m.ReqContext) Response {
  166. query := &m.GetAlertNotificationsQuery{
  167. OrgId: c.OrgId,
  168. Id: c.ParamsInt64("notificationId"),
  169. }
  170. if query.Id == 0 {
  171. return Error(404, "Alert notification not found", nil)
  172. }
  173. if err := bus.Dispatch(query); err != nil {
  174. return Error(500, "Failed to get alert notifications", err)
  175. }
  176. if query.Result == nil {
  177. return Error(404, "Alert notification not found", nil)
  178. }
  179. return JSON(200, dtos.NewAlertNotification(query.Result))
  180. }
  181. func GetAlertNotificationByUID(c *m.ReqContext) Response {
  182. query := &m.GetAlertNotificationsWithUidQuery{
  183. OrgId: c.OrgId,
  184. Uid: c.Params("uid"),
  185. }
  186. if query.Uid == "" {
  187. return Error(404, "Alert notification not found", nil)
  188. }
  189. if err := bus.Dispatch(query); err != nil {
  190. return Error(500, "Failed to get alert notifications", err)
  191. }
  192. if query.Result == nil {
  193. return Error(404, "Alert notification not found", nil)
  194. }
  195. return JSON(200, dtos.NewAlertNotification(query.Result))
  196. }
  197. func CreateAlertNotification(c *m.ReqContext, cmd m.CreateAlertNotificationCommand) Response {
  198. cmd.OrgId = c.OrgId
  199. if err := bus.Dispatch(&cmd); err != nil {
  200. return Error(500, "Failed to create alert notification", err)
  201. }
  202. return JSON(200, dtos.NewAlertNotification(cmd.Result))
  203. }
  204. func UpdateAlertNotification(c *m.ReqContext, cmd m.UpdateAlertNotificationCommand) Response {
  205. cmd.OrgId = c.OrgId
  206. if err := bus.Dispatch(&cmd); err != nil {
  207. return Error(500, "Failed to update alert notification", err)
  208. }
  209. return JSON(200, dtos.NewAlertNotification(cmd.Result))
  210. }
  211. func UpdateAlertNotificationByUID(c *m.ReqContext, cmd m.UpdateAlertNotificationWithUidCommand) Response {
  212. cmd.OrgId = c.OrgId
  213. cmd.Uid = c.Params("uid")
  214. if err := bus.Dispatch(&cmd); err != nil {
  215. return Error(500, "Failed to update alert notification", err)
  216. }
  217. return JSON(200, dtos.NewAlertNotification(cmd.Result))
  218. }
  219. func DeleteAlertNotification(c *m.ReqContext) Response {
  220. cmd := m.DeleteAlertNotificationCommand{
  221. OrgId: c.OrgId,
  222. Id: c.ParamsInt64("notificationId"),
  223. }
  224. if err := bus.Dispatch(&cmd); err != nil {
  225. return Error(500, "Failed to delete alert notification", err)
  226. }
  227. return Success("Notification deleted")
  228. }
  229. func DeleteAlertNotificationByUID(c *m.ReqContext) Response {
  230. cmd := m.DeleteAlertNotificationWithUidCommand{
  231. OrgId: c.OrgId,
  232. Uid: c.Params("uid"),
  233. }
  234. if err := bus.Dispatch(&cmd); err != nil {
  235. return Error(500, "Failed to delete alert notification", err)
  236. }
  237. return Success("Notification deleted")
  238. }
  239. //POST /api/alert-notifications/test
  240. func NotificationTest(c *m.ReqContext, dto dtos.NotificationTestCommand) Response {
  241. cmd := &alerting.NotificationTestCommand{
  242. Name: dto.Name,
  243. Type: dto.Type,
  244. Settings: dto.Settings,
  245. }
  246. if err := bus.Dispatch(cmd); err != nil {
  247. if err == m.ErrSmtpNotEnabled {
  248. return Error(412, err.Error(), err)
  249. }
  250. return Error(500, "Failed to send alert notifications", err)
  251. }
  252. return Success("Test notification sent")
  253. }
  254. //POST /api/alerts/:alertId/pause
  255. func PauseAlert(c *m.ReqContext, dto dtos.PauseAlertCommand) Response {
  256. alertID := c.ParamsInt64("alertId")
  257. query := m.GetAlertByIdQuery{Id: alertID}
  258. if err := bus.Dispatch(&query); err != nil {
  259. return Error(500, "Get Alert failed", err)
  260. }
  261. guardian := guardian.New(query.Result.DashboardId, c.OrgId, c.SignedInUser)
  262. if canEdit, err := guardian.CanEdit(); err != nil || !canEdit {
  263. if err != nil {
  264. return Error(500, "Error while checking permissions for Alert", err)
  265. }
  266. return Error(403, "Access denied to this dashboard and alert", nil)
  267. }
  268. cmd := m.PauseAlertCommand{
  269. OrgId: c.OrgId,
  270. AlertIds: []int64{alertID},
  271. Paused: dto.Paused,
  272. }
  273. if err := bus.Dispatch(&cmd); err != nil {
  274. return Error(500, "", err)
  275. }
  276. var response m.AlertStateType = m.AlertStateUnknown
  277. pausedState := "un-paused"
  278. if cmd.Paused {
  279. response = m.AlertStatePaused
  280. pausedState = "paused"
  281. }
  282. result := map[string]interface{}{
  283. "alertId": alertID,
  284. "state": response,
  285. "message": "Alert " + pausedState,
  286. }
  287. return JSON(200, result)
  288. }
  289. //POST /api/admin/pause-all-alerts
  290. func PauseAllAlerts(c *m.ReqContext, dto dtos.PauseAllAlertsCommand) Response {
  291. updateCmd := m.PauseAllAlertCommand{
  292. Paused: dto.Paused,
  293. }
  294. if err := bus.Dispatch(&updateCmd); err != nil {
  295. return Error(500, "Failed to pause alerts", err)
  296. }
  297. var response m.AlertStateType = m.AlertStatePending
  298. pausedState := "un paused"
  299. if updateCmd.Paused {
  300. response = m.AlertStatePaused
  301. pausedState = "paused"
  302. }
  303. result := map[string]interface{}{
  304. "state": response,
  305. "message": "alerts " + pausedState,
  306. "alertsAffected": updateCmd.ResultCount,
  307. }
  308. return JSON(200, result)
  309. }