alertmanager.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package notifiers
  2. import (
  3. "context"
  4. "time"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. "github.com/grafana/grafana/pkg/infra/log"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/services/alerting"
  10. )
  11. func init() {
  12. alerting.RegisterNotifier(&alerting.NotifierPlugin{
  13. Type: "prometheus-alertmanager",
  14. Name: "Prometheus Alertmanager",
  15. Description: "Sends alert to Prometheus Alertmanager",
  16. Factory: NewAlertmanagerNotifier,
  17. OptionsTemplate: `
  18. <h3 class="page-heading">Alertmanager settings</h3>
  19. <div class="gf-form">
  20. <span class="gf-form-label width-10">Url</span>
  21. <input type="text" required class="gf-form-input max-width-26" ng-model="ctrl.model.settings.url" placeholder="http://localhost:9093"></input>
  22. </div>
  23. `,
  24. })
  25. }
  26. func NewAlertmanagerNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  27. url := model.Settings.Get("url").MustString()
  28. if url == "" {
  29. return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
  30. }
  31. return &AlertmanagerNotifier{
  32. NotifierBase: NewNotifierBase(model),
  33. Url: url,
  34. log: log.New("alerting.notifier.prometheus-alertmanager"),
  35. }, nil
  36. }
  37. type AlertmanagerNotifier struct {
  38. NotifierBase
  39. Url string
  40. log log.Logger
  41. }
  42. func (this *AlertmanagerNotifier) ShouldNotify(ctx context.Context, evalContext *alerting.EvalContext, notificationState *m.AlertNotificationState) bool {
  43. this.log.Debug("Should notify", "ruleId", evalContext.Rule.Id, "state", evalContext.Rule.State, "previousState", evalContext.PrevAlertState)
  44. // Do not notify when we become OK for the first time.
  45. if (evalContext.PrevAlertState == m.AlertStatePending) && (evalContext.Rule.State == m.AlertStateOK) {
  46. return false
  47. }
  48. // Notify on Alerting -> OK to resolve before alertmanager timeout.
  49. if (evalContext.PrevAlertState == m.AlertStateAlerting) && (evalContext.Rule.State == m.AlertStateOK) {
  50. return true
  51. }
  52. return evalContext.Rule.State == m.AlertStateAlerting
  53. }
  54. func (this *AlertmanagerNotifier) createAlert(evalContext *alerting.EvalContext, match *alerting.EvalMatch, ruleUrl string) *simplejson.Json {
  55. alertJSON := simplejson.New()
  56. alertJSON.Set("startsAt", evalContext.StartTime.UTC().Format(time.RFC3339))
  57. if evalContext.Rule.State == m.AlertStateOK {
  58. alertJSON.Set("endsAt", time.Now().UTC().Format(time.RFC3339))
  59. }
  60. alertJSON.Set("generatorURL", ruleUrl)
  61. // Annotations (summary and description are very commonly used).
  62. alertJSON.SetPath([]string{"annotations", "summary"}, evalContext.Rule.Name)
  63. description := ""
  64. if evalContext.Rule.Message != "" {
  65. description += evalContext.Rule.Message
  66. }
  67. if evalContext.Error != nil {
  68. if description != "" {
  69. description += "\n"
  70. }
  71. description += "Error: " + evalContext.Error.Error()
  72. }
  73. if description != "" {
  74. alertJSON.SetPath([]string{"annotations", "description"}, description)
  75. }
  76. if evalContext.ImagePublicUrl != "" {
  77. alertJSON.SetPath([]string{"annotations", "image"}, evalContext.ImagePublicUrl)
  78. }
  79. // Labels (from metrics tags + mandatory alertname).
  80. tags := make(map[string]string)
  81. if match != nil {
  82. if len(match.Tags) == 0 {
  83. tags["metric"] = match.Metric
  84. } else {
  85. for k, v := range match.Tags {
  86. tags[k] = v
  87. }
  88. }
  89. }
  90. tags["alertname"] = evalContext.Rule.Name
  91. alertJSON.Set("labels", tags)
  92. return alertJSON
  93. }
  94. func (this *AlertmanagerNotifier) Notify(evalContext *alerting.EvalContext) error {
  95. this.log.Info("Sending Alertmanager alert", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  96. ruleUrl, err := evalContext.GetRuleUrl()
  97. if err != nil {
  98. this.log.Error("Failed get rule link", "error", err)
  99. return err
  100. }
  101. // Send one alert per matching series.
  102. alerts := make([]interface{}, 0)
  103. for _, match := range evalContext.EvalMatches {
  104. alert := this.createAlert(evalContext, match, ruleUrl)
  105. alerts = append(alerts, alert)
  106. }
  107. // This happens on ExecutionError or NoData
  108. if len(alerts) == 0 {
  109. alert := this.createAlert(evalContext, nil, ruleUrl)
  110. alerts = append(alerts, alert)
  111. }
  112. bodyJSON := simplejson.NewFromAny(alerts)
  113. body, _ := bodyJSON.MarshalJSON()
  114. cmd := &m.SendWebhookSync{
  115. Url: this.Url + "/api/v1/alerts",
  116. HttpMethod: "POST",
  117. Body: string(body),
  118. }
  119. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  120. this.log.Error("Failed to send alertmanager", "error", err, "alertmanager", this.Name)
  121. return err
  122. }
  123. return nil
  124. }