2016-09-05 07:43:53 -05:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
import (
|
2016-10-03 02:38:03 -05:00
|
|
|
"context"
|
|
|
|
|
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"
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2016-09-13 08:09:55 -05:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2016-09-05 07:43:53 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type NotificationTestCommand struct {
|
2016-09-13 08:09:55 -05:00
|
|
|
State m.AlertStateType
|
2016-09-05 07:43:53 -05:00
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
Settings *simplejson.Json
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
bus.AddHandler("alerting", handleNotificationTestCommand)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleNotificationTestCommand(cmd *NotificationTestCommand) error {
|
|
|
|
notifier := NewRootNotifier()
|
|
|
|
|
2016-09-13 08:09:55 -05:00
|
|
|
model := &m.AlertNotification{
|
2016-09-05 07:43:53 -05:00
|
|
|
Name: cmd.Name,
|
|
|
|
Type: cmd.Type,
|
|
|
|
Settings: cmd.Settings,
|
|
|
|
}
|
|
|
|
|
2016-09-06 06:19:05 -05:00
|
|
|
notifiers, err := notifier.createNotifierFor(model)
|
2016-09-05 07:43:53 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error2("Failed to create notifier", "error", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-20 08:06:59 -05:00
|
|
|
return notifier.sendNotifications(createTestEvalContext(), []Notifier{notifiers})
|
2016-09-05 07:43:53 -05:00
|
|
|
}
|
|
|
|
|
2016-09-13 13:14:18 -05:00
|
|
|
func createTestEvalContext() *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.",
|
2016-09-13 13:14:18 -05:00
|
|
|
State: m.AlertStateAlerting,
|
2016-09-05 07:43:53 -05:00
|
|
|
}
|
|
|
|
|
2016-10-03 02:38:03 -05:00
|
|
|
ctx := NewEvalContext(context.TODO(), testRule)
|
2016-09-05 07:43:53 -05:00
|
|
|
ctx.ImagePublicUrl = "http://grafana.org/assets/img/blog/mixed_styles.png"
|
|
|
|
ctx.IsTestRun = true
|
2016-09-13 13:14:18 -05:00
|
|
|
ctx.Firing = true
|
2016-09-05 07:43:53 -05:00
|
|
|
ctx.Error = nil
|
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
|
|
|
|
}
|