hipchat.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/infra/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),
  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. attributes := make([]map[string]interface{}, 0)
  75. for index, evt := range evalContext.EvalMatches {
  76. metricName := evt.Metric
  77. if len(metricName) > 50 {
  78. metricName = metricName[:50]
  79. }
  80. attributes = append(attributes, map[string]interface{}{
  81. "label": metricName,
  82. "value": map[string]interface{}{
  83. "label": strconv.FormatFloat(evt.Value.Float64, 'f', -1, 64),
  84. },
  85. })
  86. if index > maxFieldCount {
  87. break
  88. }
  89. }
  90. if evalContext.Error != nil {
  91. attributes = append(attributes, map[string]interface{}{
  92. "label": "Error message",
  93. "value": map[string]interface{}{
  94. "label": evalContext.Error.Error(),
  95. },
  96. })
  97. }
  98. message := ""
  99. if evalContext.Rule.State != models.AlertStateOK { //don't add message when going back to alert state ok.
  100. message += " " + evalContext.Rule.Message
  101. }
  102. if message == "" {
  103. message = evalContext.GetNotificationTitle() + " in state " + evalContext.GetStateModel().Text
  104. }
  105. //HipChat has a set list of colors
  106. var color string
  107. switch evalContext.Rule.State {
  108. case models.AlertStateOK:
  109. color = "green"
  110. case models.AlertStateNoData:
  111. color = "gray"
  112. case models.AlertStateAlerting:
  113. color = "red"
  114. }
  115. // Add a card with link to the dashboard
  116. card := map[string]interface{}{
  117. "style": "application",
  118. "url": ruleUrl,
  119. "id": "1",
  120. "title": evalContext.GetNotificationTitle(),
  121. "description": message,
  122. "icon": map[string]interface{}{
  123. "url": "https://grafana.com/assets/img/fav32.png",
  124. },
  125. "date": evalContext.EndTime.Unix(),
  126. "attributes": attributes,
  127. }
  128. if evalContext.ImagePublicUrl != "" {
  129. card["thumbnail"] = map[string]interface{}{
  130. "url": evalContext.ImagePublicUrl,
  131. "url@2x": evalContext.ImagePublicUrl,
  132. "width": 1193,
  133. "height": 564,
  134. }
  135. }
  136. body := map[string]interface{}{
  137. "message": message,
  138. "notify": "true",
  139. "message_format": "html",
  140. "color": color,
  141. "card": card,
  142. }
  143. hipUrl := fmt.Sprintf("%s/v2/room/%s/notification?auth_token=%s", this.Url, this.RoomId, this.ApiKey)
  144. data, _ := json.Marshal(&body)
  145. this.log.Info("Request payload", "json", string(data))
  146. cmd := &models.SendWebhookSync{Url: hipUrl, Body: string(data)}
  147. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  148. this.log.Error("Failed to send hipchat notification", "error", err, "webhook", this.Name)
  149. return err
  150. }
  151. return nil
  152. }