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-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-07-27 09:29:28 -05:00
|
|
|
func (n *RootNotifier) Notify(context *EvalContext) {
|
2016-07-26 05:29:52 -05:00
|
|
|
n.log.Info("Sending notifications for", "ruleId", context.Rule.Id)
|
|
|
|
|
2016-07-25 06:52:17 -05:00
|
|
|
notifiers, err := n.getNotifiers(context.Rule.OrgId, context.Rule.Notifications)
|
|
|
|
if err != nil {
|
|
|
|
n.log.Error("Failed to read notifications", "error", err)
|
|
|
|
return
|
|
|
|
}
|
2016-07-23 04:50:48 -05:00
|
|
|
|
2016-07-31 02:31:32 -05:00
|
|
|
if len(notifiers) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-30 06:36:21 -05:00
|
|
|
err = n.uploadImage(context)
|
|
|
|
if err != nil {
|
|
|
|
n.log.Error("Failed to upload alert panel image", "error", err)
|
|
|
|
}
|
|
|
|
|
2016-09-05 07:43:53 -05:00
|
|
|
n.sendNotifications(notifiers, context)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *RootNotifier) sendNotifications(notifiers []Notifier, context *EvalContext) {
|
2016-07-23 04:50:48 -05:00
|
|
|
for _, notifier := range notifiers {
|
2016-07-25 06:52:17 -05:00
|
|
|
n.log.Info("Sending notification", "firing", context.Firing, "type", notifier.GetType())
|
|
|
|
go notifier.Notify(context)
|
2016-07-23 04:50:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-30 06:36:21 -05:00
|
|
|
func (n *RootNotifier) uploadImage(context *EvalContext) error {
|
2016-08-10 10:27:39 -05:00
|
|
|
uploader, _ := imguploader.NewImageUploader()
|
2016-07-30 06:36:21 -05:00
|
|
|
|
|
|
|
imageUrl, err := context.GetImageUrl()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
renderOpts := &renderer.RenderOpts{
|
|
|
|
Url: imageUrl,
|
|
|
|
Width: "800",
|
|
|
|
Height: "400",
|
|
|
|
SessionId: "123",
|
|
|
|
Timeout: "10",
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-25 06:52:17 -05:00
|
|
|
func (n *RootNotifier) getNotifiers(orgId int64, notificationIds []int64) ([]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-07-27 05:09:55 -05:00
|
|
|
if not, err := n.getNotifierFor(notification); err != nil {
|
2016-07-25 06:52:17 -05:00
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
result = append(result, not)
|
|
|
|
}
|
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-07-27 05:09:55 -05:00
|
|
|
func (n *RootNotifier) getNotifierFor(model *m.AlertNotification) (Notifier, error) {
|
|
|
|
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-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
|
|
|
}
|