notifier.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package alerting
  2. // type NotifierImpl struct {
  3. // log log.Logger
  4. // getNotifications func(orgId int64, notificationGroups []int64) []*Notification
  5. // }
  6. //
  7. // func NewNotifier() *NotifierImpl {
  8. // log := log.New("alerting.notifier")
  9. // return &NotifierImpl{
  10. // log: log,
  11. // getNotifications: buildGetNotifiers(log),
  12. // }
  13. // }
  14. // func (n NotifierImpl) ShouldDispath(alertResult *AlertResultContext, notifier *Notification) bool {
  15. // warn := alertResult.State == alertstates.Warn && notifier.SendWarning
  16. // crit := alertResult.State == alertstates.Critical && notifier.SendCritical
  17. // return (warn || crit) || alertResult.State == alertstates.Ok
  18. // }
  19. //
  20. // func (n *NotifierImpl) Notify(alertResult *AlertResultContext) {
  21. // notifiers := n.getNotifications(alertResult.Rule.OrgId, alertResult.Rule.Notifications)
  22. //
  23. // for _, notifier := range notifiers {
  24. // if n.ShouldDispath(alertResult, notifier) {
  25. // n.log.Info("Sending notification", "state", alertResult.State, "type", notifier.Type)
  26. // go notifier.Notifierr.Dispatch(alertResult)
  27. // }
  28. // }
  29. // }
  30. //
  31. // type Notification struct {
  32. // Name string
  33. // Type string
  34. // SendWarning bool
  35. // SendCritical bool
  36. //
  37. // Notifierr NotificationDispatcher
  38. // }
  39. //
  40. // type EmailNotifier struct {
  41. // To string
  42. // log log.Logger
  43. // }
  44. //
  45. // func (this *EmailNotifier) Dispatch(alertResult *AlertResult) {
  46. // this.log.Info("Sending email")
  47. // grafanaUrl := fmt.Sprintf("%s:%s", setting.HttpAddr, setting.HttpPort)
  48. // if setting.AppSubUrl != "" {
  49. // grafanaUrl += "/" + setting.AppSubUrl
  50. // }
  51. //
  52. // query := &m.GetDashboardsQuery{
  53. // DashboardIds: []int64{alertResult.AlertJob.Rule.DashboardId},
  54. // }
  55. //
  56. // if err := bus.Dispatch(query); err != nil {
  57. // this.log.Error("Failed to load dashboard", "error", err)
  58. // return
  59. // }
  60. //
  61. // if len(query.Result) != 1 {
  62. // this.log.Error("Can only support one dashboard", "result", len(query.Result))
  63. // return
  64. // }
  65. //
  66. // dashboard := query.Result[0]
  67. //
  68. // panelId := strconv.Itoa(int(alertResult.AlertJob.Rule.PanelId))
  69. //
  70. // //TODO: get from alertrule and transforms to seconds
  71. // from := "1466169458375"
  72. // to := "1466171258375"
  73. //
  74. // renderUrl := fmt.Sprintf("%s/render/dashboard-solo/db/%s?from=%s&to=%s&panelId=%s&width=1000&height=500", grafanaUrl, dashboard.Slug, from, to, panelId)
  75. // cmd := &m.SendEmailCommand{
  76. // Data: map[string]interface{}{
  77. // "Name": "Name",
  78. // "State": alertResult.State,
  79. // "Description": alertResult.Description,
  80. // "TriggeredAlerts": alertResult.TriggeredAlerts,
  81. // "DashboardLink": grafanaUrl + "/dashboard/db/" + dashboard.Slug,
  82. // "AlertPageUrl": grafanaUrl + "/alerting",
  83. // "DashboardImage": renderUrl,
  84. // },
  85. // To: []string{this.To},
  86. // Template: "alert_notification.html",
  87. // }
  88. //
  89. // err := bus.Dispatch(cmd)
  90. // if err != nil {
  91. // this.log.Error("Could not send alert notification as email", "error", err)
  92. // }
  93. // }
  94. //
  95. // type WebhookNotifier struct {
  96. // Url string
  97. // User string
  98. // Password string
  99. // log log.Logger
  100. // }
  101. //
  102. // func (this *WebhookNotifier) Dispatch(alertResult *AlertResultContext) {
  103. // this.log.Info("Sending webhook")
  104. //
  105. // bodyJSON := simplejson.New()
  106. // bodyJSON.Set("name", alertResult.AlertJob.Rule.Name)
  107. // bodyJSON.Set("state", alertResult.State)
  108. // bodyJSON.Set("trigged", alertResult.TriggeredAlerts)
  109. //
  110. // body, _ := bodyJSON.MarshalJSON()
  111. //
  112. // cmd := &m.SendWebhook{
  113. // Url: this.Url,
  114. // User: this.User,
  115. // Password: this.Password,
  116. // Body: string(body),
  117. // }
  118. //
  119. // bus.Dispatch(cmd)
  120. // }
  121. //
  122. // type NotificationDispatcher interface {
  123. // Dispatch(alertResult *AlertResult)
  124. // }
  125. //
  126. // func buildGetNotifiers(log log.Logger) func(orgId int64, notificationGroups []int64) []*Notification {
  127. // return func(orgId int64, notificationGroups []int64) []*Notification {
  128. // query := &m.GetAlertNotificationQuery{
  129. // OrgID: orgId,
  130. // Ids: notificationGroups,
  131. // IncludeAlwaysExecute: true,
  132. // }
  133. // err := bus.Dispatch(query)
  134. // if err != nil {
  135. // log.Error("Failed to read notifications", "error", err)
  136. // }
  137. //
  138. // var result []*Notification
  139. // for _, notification := range query.Result {
  140. // not, err := NewNotificationFromDBModel(notification)
  141. // if err == nil {
  142. // result = append(result, not)
  143. // } else {
  144. // log.Error("Failed to read notification model", "error", err)
  145. // }
  146. // }
  147. //
  148. // return result
  149. // }
  150. // }
  151. //
  152. // func NewNotificationFromDBModel(model *m.AlertNotification) (*Notification, error) {
  153. // notifier, err := createNotifier(model.Type, model.Settings)
  154. //
  155. // if err != nil {
  156. // return nil, err
  157. // }
  158. //
  159. // return &Notification{
  160. // Name: model.Name,
  161. // Type: model.Type,
  162. // Notifierr: notifier,
  163. // SendCritical: model.Settings.Get("sendCrit").MustBool(),
  164. // SendWarning: model.Settings.Get("sendWarn").MustBool(),
  165. // }, nil
  166. // }
  167. //
  168. // var createNotifier = func(notificationType string, settings *simplejson.Json) (NotificationDispatcher, error) {
  169. // if notificationType == "email" {
  170. // to := settings.Get("to").MustString()
  171. //
  172. // if to == "" {
  173. // return nil, fmt.Errorf("Could not find to propertie in settings")
  174. // }
  175. //
  176. // return &EmailNotifier{
  177. // To: to,
  178. // log: log.New("alerting.notification.email"),
  179. // }, nil
  180. // }
  181. //
  182. // url := settings.Get("url").MustString()
  183. // if url == "" {
  184. // return nil, fmt.Errorf("Could not find url propertie in settings")
  185. // }
  186. //
  187. // return &WebhookNotifier{
  188. // Url: url,
  189. // User: settings.Get("user").MustString(),
  190. // Password: settings.Get("password").MustString(),
  191. // log: log.New("alerting.notification.webhook"),
  192. // }, nil
  193. // }