line.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package notifiers
  2. import (
  3. "fmt"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/log"
  6. "github.com/grafana/grafana/pkg/metrics"
  7. m "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/services/alerting"
  9. "net/url"
  10. )
  11. func init() {
  12. alerting.RegisterNotifier(&alerting.NotifierPlugin{
  13. Type: "LINE",
  14. Name: "LINE",
  15. Description: "Send notifications to LINE notify",
  16. Factory: NewLINENotifier,
  17. OptionsTemplate: `
  18. <div class="gf-form-group">
  19. <h3 class="page-heading">LINE notify settings</h3>
  20. <div class="gf-form">
  21. <span class="gf-form-label width-14">Token</span>
  22. <input type="text" required class="gf-form-input max-width-22" ng-model="ctrl.model.settings.token" placeholder="LINE notify token key"></input>
  23. </div>
  24. </div>
  25. `,
  26. })
  27. }
  28. const (
  29. lineNotifyUrl string = "https://notify-api.line.me/api/notify"
  30. )
  31. func NewLINENotifier(model *m.AlertNotification) (alerting.Notifier, error) {
  32. token := model.Settings.Get("token").MustString()
  33. if token == "" {
  34. return nil, alerting.ValidationError{Reason: "Could not find token in settings"}
  35. }
  36. return &LineNotifier{
  37. NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings),
  38. Token: token,
  39. log: log.New("alerting.notifier.line"),
  40. }, nil
  41. }
  42. type LineNotifier struct {
  43. NotifierBase
  44. Token string
  45. log log.Logger
  46. }
  47. func (this *LineNotifier) Notify(evalContext *alerting.EvalContext) error {
  48. this.log.Info("Executing line notification", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  49. metrics.M_Alerting_Notification_Sent_LINE.Inc(1)
  50. var err error
  51. switch evalContext.Rule.State {
  52. case m.AlertStateAlerting:
  53. err = this.createAlert(evalContext)
  54. }
  55. return err
  56. }
  57. func (this *LineNotifier) createAlert(evalContext *alerting.EvalContext) error {
  58. this.log.Info("Creating Line notify", "ruleId", evalContext.Rule.Id, "notification", this.Name)
  59. ruleUrl, err := evalContext.GetRuleUrl()
  60. if err != nil {
  61. this.log.Error("Failed get rule link", "error", err)
  62. return err
  63. }
  64. form := url.Values{}
  65. body := fmt.Sprintf("%s - %s\n%s", evalContext.Rule.Name, ruleUrl, evalContext.Rule.Message)
  66. form.Add("message", body)
  67. cmd := &m.SendWebhookSync{
  68. Url: lineNotifyUrl,
  69. HttpMethod: "POST",
  70. HttpHeader: map[string]string{
  71. "Authorization": fmt.Sprintf("Bearer %s", this.Token),
  72. "Content-Type": "application/x-www-form-urlencoded",
  73. },
  74. Body: form.Encode(),
  75. }
  76. if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
  77. this.log.Error("Failed to send notification to LINE", "error", err, "body", string(body))
  78. return err
  79. }
  80. return nil
  81. }