2016-09-05 07:43:53 -05:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
import (
|
2016-10-03 02:38:03 -05:00
|
|
|
"context"
|
2017-05-17 02:51:51 -05:00
|
|
|
"fmt"
|
2016-10-03 02:38:03 -05:00
|
|
|
|
2016-09-05 07:43:53 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2017-01-13 05:32:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/null"
|
2016-09-05 07:43:53 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2019-05-14 01:15:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-09-05 07:43:53 -05:00
|
|
|
)
|
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
// NotificationTestCommand initiates an test
|
|
|
|
// execution of an alert notification.
|
2016-09-05 07:43:53 -05:00
|
|
|
type NotificationTestCommand struct {
|
2019-05-14 01:15:05 -05:00
|
|
|
State models.AlertStateType
|
2016-09-05 07:43:53 -05:00
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
Settings *simplejson.Json
|
|
|
|
}
|
|
|
|
|
2019-01-15 04:49:18 -06:00
|
|
|
var (
|
|
|
|
logger = log.New("alerting.testnotification")
|
|
|
|
)
|
|
|
|
|
2016-09-05 07:43:53 -05:00
|
|
|
func init() {
|
|
|
|
bus.AddHandler("alerting", handleNotificationTestCommand)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleNotificationTestCommand(cmd *NotificationTestCommand) error {
|
2019-05-20 05:13:32 -05:00
|
|
|
notifier := newNotificationService(nil)
|
2016-09-05 07:43:53 -05:00
|
|
|
|
2019-05-14 01:15:05 -05:00
|
|
|
model := &models.AlertNotification{
|
2016-09-05 07:43:53 -05:00
|
|
|
Name: cmd.Name,
|
|
|
|
Type: cmd.Type,
|
|
|
|
Settings: cmd.Settings,
|
|
|
|
}
|
|
|
|
|
2018-12-19 06:38:49 -06:00
|
|
|
notifiers, err := InitNotifier(model)
|
2016-09-05 07:43:53 -05:00
|
|
|
|
|
|
|
if err != nil {
|
2019-01-15 04:49:18 -06:00
|
|
|
logger.Error("Failed to create notifier", "error", err.Error())
|
2016-09-05 07:43:53 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-02 07:03:30 -05:00
|
|
|
return notifier.sendNotifications(createTestEvalContext(cmd), notifierStateSlice{{notifier: notifiers}})
|
2016-09-05 07:43:53 -05:00
|
|
|
}
|
|
|
|
|
2017-02-13 05:43:12 -06:00
|
|
|
func createTestEvalContext(cmd *NotificationTestCommand) *EvalContext {
|
2016-09-05 07:43:53 -05:00
|
|
|
testRule := &Rule{
|
|
|
|
DashboardId: 1,
|
|
|
|
PanelId: 1,
|
|
|
|
Name: "Test notification",
|
|
|
|
Message: "Someone is testing the alert notification within grafana.",
|
2019-05-14 01:15:05 -05:00
|
|
|
State: models.AlertStateAlerting,
|
2016-09-05 07:43:53 -05:00
|
|
|
}
|
|
|
|
|
2017-02-24 02:20:21 -06:00
|
|
|
ctx := NewEvalContext(context.Background(), testRule)
|
2017-02-13 05:43:12 -06:00
|
|
|
if cmd.Settings.Get("uploadImage").MustBool(true) {
|
2019-03-10 18:23:07 -05:00
|
|
|
ctx.ImagePublicUrl = "https://grafana.com/assets/img/blog/mixed_styles.png"
|
2017-02-13 05:43:12 -06:00
|
|
|
}
|
2016-09-05 07:43:53 -05:00
|
|
|
ctx.IsTestRun = true
|
2016-09-13 13:14:18 -05:00
|
|
|
ctx.Firing = true
|
2017-05-17 02:51:51 -05:00
|
|
|
ctx.Error = fmt.Errorf("This is only a test")
|
2016-09-13 13:14:18 -05:00
|
|
|
ctx.EvalMatches = evalMatchesBasedOnState()
|
2016-09-05 07:43:53 -05:00
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2016-09-13 13:14:18 -05:00
|
|
|
func evalMatchesBasedOnState() []*EvalMatch {
|
2016-09-05 07:43:53 -05:00
|
|
|
matches := make([]*EvalMatch, 0)
|
|
|
|
matches = append(matches, &EvalMatch{
|
|
|
|
Metric: "High value",
|
2017-01-13 05:32:30 -06:00
|
|
|
Value: null.FloatFrom(100),
|
2016-09-05 07:43:53 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
matches = append(matches, &EvalMatch{
|
|
|
|
Metric: "Higher Value",
|
2017-01-13 05:32:30 -06:00
|
|
|
Value: null.FloatFrom(200),
|
2016-09-05 07:43:53 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return matches
|
|
|
|
}
|