grafana/pkg/services/alerting/notifiers/line.go

111 lines
3.1 KiB
Go
Raw Normal View History

2017-01-19 01:25:21 -06:00
package notifiers
import (
2017-01-27 08:21:02 -06:00
"fmt"
2017-09-06 04:23:52 -05:00
"net/url"
2017-01-27 08:21:02 -06:00
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
2017-01-27 08:21:02 -06:00
"github.com/grafana/grafana/pkg/services/alerting"
2017-01-19 01:25:21 -06:00
)
func init() {
alerting.RegisterNotifier(&alerting.NotifierPlugin{
2017-01-27 08:21:02 -06:00
Type: "LINE",
Name: "LINE",
2017-01-19 01:25:21 -06:00
Description: "Send notifications to LINE notify",
Heading: "LINE notify settings",
2017-01-27 08:21:02 -06:00
Factory: NewLINENotifier,
2017-01-19 01:25:21 -06:00
OptionsTemplate: `
<div class="gf-form-group">
<h3 class="page-heading">LINE notify settings</h3>
<div class="gf-form">
<span class="gf-form-label width-14">Token</span>
<input type="text" required class="gf-form-input max-width-22" ng-model="ctrl.model.settings.token" placeholder="LINE notify token key"></input>
</div>
</div>
`,
Options: []alerting.NotifierOption{
{
Label: "Token",
Element: alerting.ElementTypeInput,
InputType: alerting.InputTypeText,
Placeholder: "LINE notify token key",
PropertyName: "token",
Required: true,
}},
2017-01-19 01:25:21 -06:00
})
}
const (
lineNotifyURL string = "https://notify-api.line.me/api/notify"
2017-01-19 01:25:21 -06:00
)
// NewLINENotifier is the constructor for the LINE notifier
func NewLINENotifier(model *models.AlertNotification) (alerting.Notifier, error) {
2017-01-19 01:25:21 -06:00
token := model.Settings.Get("token").MustString()
if token == "" {
return nil, alerting.ValidationError{Reason: "Could not find token in settings"}
}
return &LineNotifier{
NotifierBase: NewNotifierBase(model),
2017-01-19 01:25:21 -06:00
Token: token,
log: log.New("alerting.notifier.line"),
}, nil
}
// LineNotifier is responsible for sending
// alert notifications to LINE.
2017-01-19 01:25:21 -06:00
type LineNotifier struct {
NotifierBase
Token string
log log.Logger
}
// Notify send an alert notification to LINE
func (ln *LineNotifier) Notify(evalContext *alerting.EvalContext) error {
ln.log.Info("Executing line notification", "ruleId", evalContext.Rule.ID, "notification", ln.Name)
if evalContext.Rule.State == models.AlertStateAlerting {
return ln.createAlert(evalContext)
2017-01-19 01:25:21 -06:00
}
return nil
2017-01-19 01:25:21 -06:00
}
func (ln *LineNotifier) createAlert(evalContext *alerting.EvalContext) error {
ln.log.Info("Creating Line notify", "ruleId", evalContext.Rule.ID, "notification", ln.Name)
ruleURL, err := evalContext.GetRuleURL()
2017-01-19 01:25:21 -06:00
if err != nil {
ln.log.Error("Failed get rule link", "error", err)
2017-01-19 01:25:21 -06:00
return err
}
form := url.Values{}
body := fmt.Sprintf("%s - %s\n%s", evalContext.Rule.Name, ruleURL, evalContext.Rule.Message)
2017-01-19 01:25:21 -06:00
form.Add("message", body)
if ln.NeedsImage() && evalContext.ImagePublicURL != "" {
form.Add("imageThumbnail", evalContext.ImagePublicURL)
form.Add("imageFullsize", evalContext.ImagePublicURL)
}
cmd := &models.SendWebhookSync{
Url: lineNotifyURL,
2017-01-19 01:25:21 -06:00
HttpMethod: "POST",
HttpHeader: map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", ln.Token),
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
2017-01-19 01:25:21 -06:00
},
Body: form.Encode(),
}
if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil {
ln.log.Error("Failed to send notification to LINE", "error", err, "body", body)
2017-01-19 01:25:21 -06:00
return err
}
return nil
}