victorops.go 3.5 KB

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