victorops.go 3.1 KB

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