hipchat.go 4.9 KB

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