hipchat.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package notifiers
  2. import (
  3. "encoding/json"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/log"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/services/alerting"
  8. "strconv"
  9. "strings"
  10. "time"
  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-6">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"></input>
  23. </div>
  24. <div class="gf-form max-width-30">
  25. <span class="gf-form-label width-6">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-6">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. func NewHipChatNotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  40. url := model.Settings.Get("url").MustString()
  41. if strings.HasSuffix(url, "/") {
  42. url = url[:len(url)-1]
  43. }
  44. if url == "" {
  45. return nil, alerting.ValidationError{Reason: "Could not find url property in settings"}
  46. }
  47. apikey := model.Settings.Get("apikey").MustString()
  48. roomid := model.Settings.Get("roomid").MustString()
  49. return &HipChatNotifier{
  50. NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  51. Url: url,
  52. ApiKey: apikey,
  53. RoomId: roomid,
  54. log: log.New("alerting.notifier.hipchat"),
  55. }, nil
  56. }
  57. type HipChatNotifier struct {
  58. NotifierBase
  59. Url string
  60. ApiKey string
  61. RoomId string
  62. log log.Logger
  63. }
  64. func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error {
  65. var message string
  66. var color string
  67. this.log.Info("Executing hipchat notification", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  68. ruleUrl, err := evalContext.GetRuleUrl()
  69. if err != nil {
  70. this.log.Error("Failed get rule link", "error", err)
  71. return err
  72. }
  73. message = evalContext.GetNotificationTitle() + " in state " + evalContext.GetStateModel().Text + "<br><a href=" + ruleUrl + ">Check Dasboard</a>"
  74. fields := make([]map[string]interface{}, 0)
  75. fieldLimitCount := 4
  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 > fieldLimitCount {
  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 != m.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. switch evalContext.Rule.State {
  100. case m.AlertStateOK:
  101. color = "green"
  102. case m.AlertStateNoData:
  103. color = "grey"
  104. case m.AlertStateAlerting:
  105. color = "red"
  106. }
  107. // Add a card with link to the dashboard
  108. card := map[string]interface{}{
  109. "style": "link",
  110. "url": ruleUrl,
  111. "id": "1",
  112. "title": evalContext.GetNotificationTitle(),
  113. "description": evalContext.GetNotificationTitle() + " in state " + evalContext.GetStateModel().Text,
  114. "icon": map[string]interface{}{
  115. "url": "http://grafana.org/assets/img/fav32.png",
  116. },
  117. "date": time.Now().Unix(),
  118. }
  119. body := map[string]interface{}{
  120. "message": message,
  121. "notify": "true",
  122. "message_format": "html",
  123. "color": color,
  124. "card": card,
  125. }
  126. hipUrl := this.Url + "/v2/room/" + this.RoomId + "/notification?auth_token=" + this.ApiKey
  127. data, _ := json.Marshal(&body)
  128. cmd := &m.SendWebhookSync{Url: hipUrl, Body: string(data)}
  129. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  130. this.log.Error("Failed to send hipchat notification", "error", err, "webhook", this.Name)
  131. return err
  132. }
  133. return nil
  134. }