victorops.go 4.3 KB

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