victorops.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package notifiers
  2. import (
  3. "encoding/json"
  4. "time"
  5. "github.com/grafana/grafana/pkg/bus"
  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. func init() {
  15. alerting.RegisterNotifier("victorops", NewVictoropsNotifier)
  16. }
  17. // NewVictoropsNotifier creates an instance of VictoropsNotifier that
  18. // handles posting notifications to Victorops REST API
  19. func NewVictoropsNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
  20. url := model.Settings.Get("url").MustString()
  21. if url == "" {
  22. return nil, alerting.ValidationError{Reason: "Could not find victorops url property in settings"}
  23. }
  24. alertOnExecError := model.Settings.Get("alertOnExecError").MustBool()
  25. return &VictoropsNotifier{
  26. NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  27. URL: url,
  28. AlertOnExecError: alertOnExecError,
  29. log: log.New("alerting.notifier.victorops"),
  30. }, nil
  31. }
  32. // VictoropsNotifier defines URL property for Victorops REST API
  33. // and handles notification process by formatting POST body according to
  34. // Victorops specifications (http://victorops.force.com/knowledgebase/articles/Integration/Alert-Ingestion-API-Documentation/)
  35. type VictoropsNotifier struct {
  36. NotifierBase
  37. URL string
  38. AlertOnExecError bool
  39. log log.Logger
  40. }
  41. // Notify sends notification to Victorops via POST to URL endpoint
  42. func (this *VictoropsNotifier) Notify(evalContext *alerting.EvalContext) error {
  43. this.log.Info("Executing victorops notification", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  44. metrics.M_Alerting_Notification_Sent_Victorops.Inc(1)
  45. ruleUrl, err := evalContext.GetRuleUrl()
  46. if err != nil {
  47. this.log.Error("Failed get rule link", "error", err)
  48. return err
  49. }
  50. fields := make([]map[string]interface{}, 0)
  51. fieldLimitCount := 4
  52. for index, evt := range evalContext.EvalMatches {
  53. fields = append(fields, map[string]interface{}{
  54. "title": evt.Metric,
  55. "value": evt.Value,
  56. "short": true,
  57. })
  58. if index > fieldLimitCount {
  59. break
  60. }
  61. }
  62. if evalContext.Error != nil {
  63. fields = append(fields, map[string]interface{}{
  64. "title": "Error message",
  65. "value": evalContext.Error.Error(),
  66. "short": false,
  67. })
  68. }
  69. messageType := evalContext.Rule.State
  70. if evalContext.Rule.State == models.AlertStateAlerting { // translate 'Alerting' to 'CRITICAL' (Victorops analog)
  71. messageType = AlertStateCritical
  72. }
  73. body := map[string]interface{}{
  74. "message_type": messageType,
  75. "entity_id": evalContext.Rule.Name,
  76. "timestamp": time.Now().Unix(),
  77. "state_start_time": evalContext.StartTime.Unix(),
  78. "state_message": evalContext.Rule.Message + "\n" + ruleUrl,
  79. "monitoring_tool": "Grafana v" + setting.BuildVersion,
  80. }
  81. data, _ := json.Marshal(&body)
  82. cmd := &models.SendWebhookSync{Url: this.URL, Body: string(data)}
  83. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  84. this.log.Error("Failed to send victorops notification", "error", err, "webhook", this.Name)
  85. }
  86. return nil
  87. }