2016-05-23 07:14:02 -05:00
|
|
|
package alerting
|
2016-06-14 09:56:14 -05:00
|
|
|
|
2016-07-23 04:50:48 -05:00
|
|
|
import (
|
2016-07-25 06:52:17 -05:00
|
|
|
"errors"
|
2016-09-26 03:51:45 -05:00
|
|
|
"fmt"
|
2016-07-23 04:50:48 -05:00
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2016-07-23 04:50:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2016-07-30 06:36:21 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/imguploader"
|
|
|
|
"github.com/grafana/grafana/pkg/components/renderer"
|
2016-07-25 06:52:17 -05:00
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2016-07-23 04:50:48 -05:00
|
|
|
)
|
|
|
|
|
2017-01-06 05:04:25 -06:00
|
|
|
type NotifierPlugin struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
OptionsTemplate string `json:"optionsTemplate"`
|
|
|
|
Factory NotifierFactory `json:"-"`
|
|
|
|
}
|
|
|
|
|
2016-07-25 06:52:17 -05:00
|
|
|
type RootNotifier struct {
|
|
|
|
log log.Logger
|
2016-07-23 04:50:48 -05:00
|
|
|
}
|
|
|
|
|
2016-07-25 06:52:17 -05:00
|
|
|
func NewRootNotifier() *RootNotifier {
|
|
|
|
return &RootNotifier{
|
|
|
|
log: log.New("alerting.notifier"),
|
2016-07-23 04:50:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 05:09:55 -05:00
|
|
|
func (n *RootNotifier) GetType() string {
|
|
|
|
return "root"
|
|
|
|
}
|
|
|
|
|
2016-07-29 07:55:02 -05:00
|
|
|
func (n *RootNotifier) NeedsImage() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-01-02 06:22:37 -06:00
|
|
|
func (n *RootNotifier) PassesFilter(rule *Rule) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-10-11 03:13:19 -05:00
|
|
|
func (n *RootNotifier) GetNotifierId() int64 {
|
|
|
|
return 0
|
|
|
|
}
|
2016-07-26 05:29:52 -05:00
|
|
|
|
2016-10-11 03:13:19 -05:00
|
|
|
func (n *RootNotifier) GetIsDefault() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *RootNotifier) Notify(context *EvalContext) error {
|
2016-09-06 06:19:05 -05:00
|
|
|
notifiers, err := n.getNotifiers(context.Rule.OrgId, context.Rule.Notifications, context)
|
2016-07-25 06:52:17 -05:00
|
|
|
if err != nil {
|
2016-10-03 02:38:03 -05:00
|
|
|
return err
|
2016-07-25 06:52:17 -05:00
|
|
|
}
|
2016-07-23 04:50:48 -05:00
|
|
|
|
2016-11-03 12:04:50 -05:00
|
|
|
n.log.Info("Sending notifications for", "ruleId", context.Rule.Id, "sent count", len(notifiers))
|
2016-10-11 03:13:19 -05:00
|
|
|
|
2016-07-31 02:31:32 -05:00
|
|
|
if len(notifiers) == 0 {
|
2016-10-03 02:38:03 -05:00
|
|
|
return nil
|
2016-07-31 02:31:32 -05:00
|
|
|
}
|
|
|
|
|
2016-11-21 02:45:13 -06:00
|
|
|
if err = n.uploadImage(context); err != nil {
|
|
|
|
n.log.Error("Failed to upload alert panel image.", "error", err)
|
2016-07-30 06:36:21 -05:00
|
|
|
}
|
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
return n.sendNotifications(context, notifiers)
|
2016-09-05 07:43:53 -05:00
|
|
|
}
|
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
func (n *RootNotifier) sendNotifications(context *EvalContext, notifiers []Notifier) error {
|
2016-10-03 10:03:21 -05:00
|
|
|
g, _ := errgroup.WithContext(context.Ctx)
|
2016-10-03 02:38:03 -05:00
|
|
|
|
2016-07-23 04:50:48 -05:00
|
|
|
for _, notifier := range notifiers {
|
2016-10-12 02:45:22 -05:00
|
|
|
not := notifier //avoid updating scope variable in go routine
|
|
|
|
n.log.Info("Sending notification", "type", not.GetType(), "id", not.GetNotifierId(), "isDefault", not.GetIsDefault())
|
|
|
|
g.Go(func() error { return not.Notify(context) })
|
2016-07-23 04:50:48 -05:00
|
|
|
}
|
2016-10-03 02:38:03 -05:00
|
|
|
|
|
|
|
return g.Wait()
|
2016-07-23 04:50:48 -05:00
|
|
|
}
|
|
|
|
|
2016-09-26 03:51:45 -05:00
|
|
|
func (n *RootNotifier) uploadImage(context *EvalContext) (err error) {
|
2016-09-29 07:31:19 -05:00
|
|
|
uploader, err := imguploader.NewImageUploader()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-30 06:36:21 -05:00
|
|
|
|
|
|
|
renderOpts := &renderer.RenderOpts{
|
2016-09-23 05:29:53 -05:00
|
|
|
Width: "800",
|
|
|
|
Height: "400",
|
|
|
|
Timeout: "30",
|
|
|
|
OrgId: context.Rule.OrgId,
|
2016-07-30 06:36:21 -05:00
|
|
|
}
|
|
|
|
|
2016-09-26 03:51:45 -05:00
|
|
|
if slug, err := context.GetDashboardSlug(); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
2016-09-26 04:07:36 -05:00
|
|
|
renderOpts.Path = fmt.Sprintf("dashboard-solo/db/%s?&panelId=%d", slug, context.Rule.PanelId)
|
2016-09-26 03:51:45 -05:00
|
|
|
}
|
|
|
|
|
2016-07-30 06:36:21 -05:00
|
|
|
if imagePath, err := renderer.RenderToPng(renderOpts); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
context.ImageOnDiskPath = imagePath
|
|
|
|
}
|
|
|
|
|
|
|
|
context.ImagePublicUrl, err = uploader.Upload(context.ImageOnDiskPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
n.log.Info("uploaded", "url", context.ImagePublicUrl)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-06 06:19:05 -05:00
|
|
|
func (n *RootNotifier) getNotifiers(orgId int64, notificationIds []int64, context *EvalContext) ([]Notifier, error) {
|
2016-09-06 01:42:35 -05:00
|
|
|
query := &m.GetAlertNotificationsToSendQuery{OrgId: orgId, Ids: notificationIds}
|
2016-07-23 04:50:48 -05:00
|
|
|
|
|
|
|
if err := bus.Dispatch(query); err != nil {
|
2016-07-25 06:52:17 -05:00
|
|
|
return nil, err
|
2016-07-23 04:50:48 -05:00
|
|
|
}
|
|
|
|
|
2016-07-25 06:52:17 -05:00
|
|
|
var result []Notifier
|
|
|
|
for _, notification := range query.Result {
|
2016-09-06 06:19:05 -05:00
|
|
|
if not, err := n.createNotifierFor(notification); err != nil {
|
2016-07-25 06:52:17 -05:00
|
|
|
return nil, err
|
|
|
|
} else {
|
2016-09-06 06:19:05 -05:00
|
|
|
if shouldUseNotification(not, context) {
|
|
|
|
result = append(result, not)
|
|
|
|
}
|
2016-07-25 06:52:17 -05:00
|
|
|
}
|
2016-07-23 04:50:48 -05:00
|
|
|
}
|
|
|
|
|
2016-07-25 06:52:17 -05:00
|
|
|
return result, nil
|
2016-07-23 04:50:48 -05:00
|
|
|
}
|
|
|
|
|
2016-09-06 06:19:05 -05:00
|
|
|
func (n *RootNotifier) createNotifierFor(model *m.AlertNotification) (Notifier, error) {
|
2017-01-06 05:04:25 -06:00
|
|
|
notifierPlugin, found := notifierFactories[model.Type]
|
2016-07-27 05:09:55 -05:00
|
|
|
if !found {
|
|
|
|
return nil, errors.New("Unsupported notification type")
|
2016-07-23 04:50:48 -05:00
|
|
|
}
|
2016-07-26 05:29:52 -05:00
|
|
|
|
2017-01-06 05:04:25 -06:00
|
|
|
return notifierPlugin.Factory(model)
|
2016-07-23 04:50:48 -05:00
|
|
|
}
|
|
|
|
|
2016-09-06 06:19:05 -05:00
|
|
|
func shouldUseNotification(notifier Notifier, context *EvalContext) bool {
|
|
|
|
if !context.Firing {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if context.Error != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-09-13 08:09:55 -05:00
|
|
|
return notifier.PassesFilter(context.Rule)
|
2016-09-06 06:19:05 -05:00
|
|
|
}
|
|
|
|
|
2016-07-27 05:09:55 -05:00
|
|
|
type NotifierFactory func(notification *m.AlertNotification) (Notifier, error)
|
2016-07-25 06:52:17 -05:00
|
|
|
|
2017-01-06 05:04:25 -06:00
|
|
|
var notifierFactories map[string]*NotifierPlugin = make(map[string]*NotifierPlugin)
|
|
|
|
|
|
|
|
func RegisterNotifier(plugin *NotifierPlugin) {
|
|
|
|
notifierFactories[plugin.Type] = plugin
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetNotifiers() []*NotifierPlugin {
|
|
|
|
list := make([]*NotifierPlugin, 0)
|
|
|
|
|
|
|
|
for _, value := range notifierFactories {
|
|
|
|
list = append(list, value)
|
|
|
|
}
|
2016-07-23 04:50:48 -05:00
|
|
|
|
2017-01-06 05:04:25 -06:00
|
|
|
return list
|
2016-07-23 04:50:48 -05:00
|
|
|
}
|