hipchat.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. // NewHipChatNotifier is the constructor functions
  43. // for the HipChatNotifier
  44. func NewHipChatNotifier(model *models.AlertNotification) (alerting.Notifier, error) {
  45. url := model.Settings.Get("url").MustString()
  46. if strings.HasSuffix(url, "/") {
  47. url = url[:len(url)-1]
  48. }
  49. if url == "" {
  50. return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
  51. }
  52. apikey := model.Settings.Get("apikey").MustString()
  53. roomID := model.Settings.Get("roomid").MustString()
  54. return &HipChatNotifier{
  55. NotifierBase: NewNotifierBase(model),
  56. URL: url,
  57. APIKey: apikey,
  58. RoomID: roomID,
  59. log: log.New("alerting.notifier.hipchat"),
  60. }, nil
  61. }
  62. // HipChatNotifier is responsible for sending
  63. // alert notifications to Hipchat.
  64. type HipChatNotifier struct {
  65. NotifierBase
  66. URL string
  67. APIKey string
  68. RoomID string
  69. log log.Logger
  70. }
  71. // Notify sends an alert notification to HipChat
  72. func (hc *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error {
  73. hc.log.Info("Executing hipchat notification", "ruleId", evalContext.Rule.ID, "notification", hc.Name)
  74. ruleURL, err := evalContext.GetRuleURL()
  75. if err != nil {
  76. hc.log.Error("Failed get rule link", "error", err)
  77. return err
  78. }
  79. attributes := make([]map[string]interface{}, 0)
  80. for index, evt := range evalContext.EvalMatches {
  81. metricName := evt.Metric
  82. if len(metricName) > 50 {
  83. metricName = metricName[:50]
  84. }
  85. attributes = append(attributes, map[string]interface{}{
  86. "label": metricName,
  87. "value": map[string]interface{}{
  88. "label": strconv.FormatFloat(evt.Value.Float64, 'f', -1, 64),
  89. },
  90. })
  91. if index > maxFieldCount {
  92. break
  93. }
  94. }
  95. if evalContext.Error != nil {
  96. attributes = append(attributes, map[string]interface{}{
  97. "label": "Error message",
  98. "value": map[string]interface{}{
  99. "label": evalContext.Error.Error(),
  100. },
  101. })
  102. }
  103. message := ""
  104. if evalContext.Rule.State != models.AlertStateOK { //don't add message when going back to alert state ok.
  105. message += " " + evalContext.Rule.Message
  106. }
  107. if message == "" {
  108. message = evalContext.GetNotificationTitle() + " in state " + evalContext.GetStateModel().Text
  109. }
  110. //HipChat has a set list of colors
  111. var color string
  112. switch evalContext.Rule.State {
  113. case models.AlertStateOK:
  114. color = "green"
  115. case models.AlertStateNoData:
  116. color = "gray"
  117. case models.AlertStateAlerting:
  118. color = "red"
  119. }
  120. // Add a card with link to the dashboard
  121. card := map[string]interface{}{
  122. "style": "application",
  123. "url": ruleURL,
  124. "id": "1",
  125. "title": evalContext.GetNotificationTitle(),
  126. "description": message,
  127. "icon": map[string]interface{}{
  128. "url": "https://grafana.com/assets/img/fav32.png",
  129. },
  130. "date": evalContext.EndTime.Unix(),
  131. "attributes": attributes,
  132. }
  133. if evalContext.ImagePublicURL != "" {
  134. card["thumbnail"] = map[string]interface{}{
  135. "url": evalContext.ImagePublicURL,
  136. "url@2x": evalContext.ImagePublicURL,
  137. "width": 1193,
  138. "height": 564,
  139. }
  140. }
  141. body := map[string]interface{}{
  142. "message": message,
  143. "notify": "true",
  144. "message_format": "html",
  145. "color": color,
  146. "card": card,
  147. }
  148. hipURL := fmt.Sprintf("%s/v2/room/%s/notification?auth_token=%s", hc.URL, hc.RoomID, hc.APIKey)
  149. data, _ := json.Marshal(&body)
  150. hc.log.Info("Request payload", "json", string(data))
  151. cmd := &models.SendWebhookSync{Url: hipURL, Body: string(data)}
  152. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  153. hc.log.Error("Failed to send hipchat notification", "error", err, "webhook", hc.Name)
  154. return err
  155. }
  156. return nil
  157. }