hipchat.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package notifiers
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "strings"
  6. "fmt"
  7. "github.com/grafana/grafana/pkg/bus"
  8. "github.com/grafana/grafana/pkg/log"
  9. "github.com/grafana/grafana/pkg/models"
  10. "github.com/grafana/grafana/pkg/services/alerting"
  11. )
  12. func init() {
  13. alerting.RegisterNotifier(&alerting.NotifierPlugin{
  14. Type: "hipchat",
  15. Name: "HipChat",
  16. Description: "Sends notifications uto a HipChat Room",
  17. Factory: NewHipChatNotifier,
  18. OptionsTemplate: `
  19. <h3 class="page-heading">HipChat settings</h3>
  20. <div class="gf-form max-width-30">
  21. <span class="gf-form-label width-8">Hip Chat Url</span>
  22. <input type="text" required class="gf-form-input max-width-30" ng-model="ctrl.model.settings.url" placeholder="HipChat URL (ex https://grafana.hipchat.com)"></input>
  23. </div>
  24. <div class="gf-form max-width-30">
  25. <span class="gf-form-label width-8">API Key</span>
  26. <input type="text" required class="gf-form-input max-width-30" ng-model="ctrl.model.settings.apikey" placeholder="HipChat API Key"></input>
  27. </div>
  28. <div class="gf-form max-width-30">
  29. <span class="gf-form-label width-8">Room ID</span>
  30. <input type="text"
  31. class="gf-form-input max-width-30"
  32. ng-model="ctrl.model.settings.roomid"
  33. data-placement="right">
  34. </input>
  35. </div>
  36. `,
  37. })
  38. }
  39. const (
  40. maxFieldCount int = 4
  41. )
  42. func NewHipChatNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
  43. url := model.Settings.Get("url").MustString()
  44. if strings.HasSuffix(url, "/") {
  45. url = url[:len(url)-1]
  46. }
  47. if url == "" {
  48. return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
  49. }
  50. apikey := model.Settings.Get("apikey").MustString()
  51. roomId := model.Settings.Get("roomid").MustString()
  52. return &HipChatNotifier{
  53. NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  54. Url: url,
  55. ApiKey: apikey,
  56. RoomId: roomId,
  57. log: log.New("alerting.notifier.hipchat"),
  58. }, nil
  59. }
  60. type HipChatNotifier struct {
  61. NotifierBase
  62. Url string
  63. ApiKey string
  64. RoomId string
  65. log log.Logger
  66. }
  67. func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error {
  68. this.log.Info("Executing hipchat notification", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  69. ruleUrl, err := evalContext.GetRuleUrl()
  70. if err != nil {
  71. this.log.Error("Failed get rule link", "error", err)
  72. return err
  73. }
  74. message := evalContext.GetNotificationTitle() + " in state " + evalContext.GetStateModel().Text + "<br><a href=" + ruleUrl + ">Check Dasboard</a>"
  75. fields := make([]map[string]interface{}, 0)
  76. message += "<br>"
  77. for index, evt := range evalContext.EvalMatches {
  78. message += evt.Metric + " :: " + strconv.FormatFloat(evt.Value.Float64, 'f', -1, 64) + "<br>"
  79. fields = append(fields, map[string]interface{}{
  80. "title": evt.Metric,
  81. "value": evt.Value,
  82. "short": true,
  83. })
  84. if index > maxFieldCount {
  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. if evalContext.Rule.State != models.AlertStateOK { //dont add message when going back to alert state ok.
  96. message += " " + evalContext.Rule.Message
  97. }
  98. //HipChat has a set list of colors
  99. var color string
  100. switch evalContext.Rule.State {
  101. case models.AlertStateOK:
  102. color = "green"
  103. case models.AlertStateNoData:
  104. color = "grey"
  105. case models.AlertStateAlerting:
  106. color = "red"
  107. }
  108. // Add a card with link to the dashboard
  109. card := map[string]interface{}{
  110. "style": "link",
  111. "url": ruleUrl,
  112. "id": "1",
  113. "title": evalContext.GetNotificationTitle(),
  114. "description": evalContext.GetNotificationTitle() + " in state " + evalContext.GetStateModel().Text,
  115. "icon": map[string]interface{}{
  116. "url": "https://grafana.com/assets/img/fav32.png",
  117. },
  118. "date": evalContext.EndTime.Unix(),
  119. }
  120. body := map[string]interface{}{
  121. "message": message,
  122. "notify": "true",
  123. "message_format": "html",
  124. "color": color,
  125. "card": card,
  126. }
  127. hipUrl := fmt.Sprintf("%s/v2/room/%s/notification?auth_token=%s", this.Url, this.RoomId, this.ApiKey)
  128. data, _ := json.Marshal(&body)
  129. cmd := &models.SendWebhookSync{Url: hipUrl, Body: string(data)}
  130. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  131. this.log.Error("Failed to send hipchat notification", "error", err, "webhook", this.Name)
  132. return err
  133. }
  134. return nil
  135. }