victorops.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package notifiers
  2. import (
  3. "time"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/components/simplejson"
  6. "github.com/grafana/grafana/pkg/log"
  7. "github.com/grafana/grafana/pkg/metrics"
  8. "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/services/alerting"
  10. "github.com/grafana/grafana/pkg/setting"
  11. )
  12. // AlertStateCritical - Victorops uses "CRITICAL" string to indicate "Alerting" state
  13. const AlertStateCritical = "CRITICAL"
  14. const AlertStateRecovery = "RECOVERY"
  15. func init() {
  16. alerting.RegisterNotifier(&alerting.NotifierPlugin{
  17. Type: "victorops",
  18. Name: "VictorOps",
  19. Description: "Sends notifications to VictorOps",
  20. Factory: NewVictoropsNotifier,
  21. OptionsTemplate: `
  22. <h3 class="page-heading">VictorOps settings</h3>
  23. <div class="gf-form">
  24. <span class="gf-form-label width-6">Url</span>
  25. <input type="text" required class="gf-form-input max-width-30" ng-model="ctrl.model.settings.url" placeholder="VictorOps url"></input>
  26. </div>
  27. <div class="gf-form">
  28. <gf-form-switch
  29. class="gf-form"
  30. label="Auto resolve incidents"
  31. label-class="width-14"
  32. checked="ctrl.model.settings.autoResolve"
  33. tooltip="Resolve incidents in VictorOps once the alert goes back to ok.">
  34. </gf-form-switch>
  35. </div>
  36. `,
  37. })
  38. }
  39. // NewVictoropsNotifier creates an instance of VictoropsNotifier that
  40. // handles posting notifications to Victorops REST API
  41. func NewVictoropsNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
  42. autoResolve := model.Settings.Get("autoResolve").MustBool(true)
  43. url := model.Settings.Get("url").MustString()
  44. if url == "" {
  45. return nil, alerting.ValidationError{Reason: "Could not find victorops url property in settings"}
  46. }
  47. return &VictoropsNotifier{
  48. NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  49. URL: url,
  50. AutoResolve: autoResolve,
  51. log: log.New("alerting.notifier.victorops"),
  52. }, nil
  53. }
  54. // VictoropsNotifier defines URL property for Victorops REST API
  55. // and handles notification process by formatting POST body according to
  56. // Victorops specifications (http://victorops.force.com/knowledgebase/articles/Integration/Alert-Ingestion-API-Documentation/)
  57. type VictoropsNotifier struct {
  58. NotifierBase
  59. URL string
  60. AutoResolve bool
  61. log log.Logger
  62. }
  63. // Notify sends notification to Victorops via POST to URL endpoint
  64. func (this *VictoropsNotifier) Notify(evalContext *alerting.EvalContext) error {
  65. this.log.Info("Executing victorops notification", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  66. metrics.M_Alerting_Notification_Sent_Victorops.Inc(1)
  67. ruleUrl, err := evalContext.GetRuleUrl()
  68. if err != nil {
  69. this.log.Error("Failed get rule link", "error", err)
  70. return err
  71. }
  72. if evalContext.Rule.State == models.AlertStateOK && !this.AutoResolve {
  73. this.log.Info("Not alerting VictorOps", "state", evalContext.Rule.State, "auto resolve", this.AutoResolve)
  74. return nil
  75. }
  76. fields := make([]map[string]interface{}, 0)
  77. fieldLimitCount := 4
  78. for index, evt := range evalContext.EvalMatches {
  79. fields = append(fields, map[string]interface{}{
  80. "title": evt.Metric,
  81. "value": evt.Value,
  82. "short": true,
  83. })
  84. if index > fieldLimitCount {
  85. break
  86. }
  87. }
  88. if evalContext.Error != nil {
  89. fields = append(fields, map[string]interface{}{
  90. "title": "Error message",
  91. "value": evalContext.Error.Error(),
  92. "short": false,
  93. })
  94. }
  95. messageType := evalContext.Rule.State
  96. if evalContext.Rule.State == models.AlertStateAlerting { // translate 'Alerting' to 'CRITICAL' (Victorops analog)
  97. messageType = AlertStateCritical
  98. }
  99. if evalContext.Rule.State == models.AlertStateOK {
  100. messageType = AlertStateRecovery
  101. }
  102. bodyJSON := simplejson.New()
  103. bodyJSON.Set("message_type", messageType)
  104. bodyJSON.Set("entity_id", evalContext.Rule.Name)
  105. bodyJSON.Set("timestamp", time.Now().Unix())
  106. bodyJSON.Set("state_start_time", evalContext.StartTime.Unix())
  107. bodyJSON.Set("state_message", evalContext.Rule.Message)
  108. bodyJSON.Set("monitoring_tool", "Grafana v"+setting.BuildVersion)
  109. bodyJSON.Set("alert_url", ruleUrl)
  110. if evalContext.ImagePublicUrl != "" {
  111. bodyJSON.Set("image_url", evalContext.ImagePublicUrl)
  112. }
  113. data, _ := bodyJSON.MarshalJSON()
  114. cmd := &models.SendWebhookSync{Url: this.URL, Body: string(data)}
  115. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  116. this.log.Error("Failed to send Victorops notification", "error", err, "webhook", this.Name)
  117. return err
  118. }
  119. return nil
  120. }