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
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-07-27 05:09:55 -05:00
|
|
|
factory, found := notifierFactories[model.Type]
|
|
|
|
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
|
|
|
|
2016-07-27 05:09:55 -05:00
|
|
|
return 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
|
|
|
|
2016-07-27 05:09:55 -05:00
|
|
|
var notifierFactories map[string]NotifierFactory = make(map[string]NotifierFactory)
|
2016-07-23 04:50:48 -05:00
|
|
|
|
2016-07-27 05:09:55 -05:00
|
|
|
func RegisterNotifier(typeName string, factory NotifierFactory) {
|
|
|
|
notifierFactories[typeName] = factory
|
2016-07-23 04:50:48 -05:00
|
|
|
}
|