2016-06-23 05:57:10 -05:00
|
|
|
package alerting
|
|
|
|
|
2016-07-21 10:31:46 -05:00
|
|
|
import (
|
2016-08-01 03:07:00 -05:00
|
|
|
"time"
|
|
|
|
|
2016-07-21 10:31:46 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2016-08-30 06:22:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2016-07-21 10:31:46 -05:00
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2016-08-11 14:12:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
2016-07-21 10:31:46 -05:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2016-08-01 03:07:00 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/annotations"
|
2016-07-21 10:31:46 -05:00
|
|
|
)
|
2016-06-23 05:57:10 -05:00
|
|
|
|
|
|
|
type ResultHandler interface {
|
2016-08-01 03:07:00 -05:00
|
|
|
Handle(ctx *EvalContext)
|
2016-06-23 05:57:10 -05:00
|
|
|
}
|
|
|
|
|
2016-07-27 09:29:28 -05:00
|
|
|
type DefaultResultHandler struct {
|
2016-06-23 05:57:10 -05:00
|
|
|
notifier Notifier
|
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
2016-07-27 09:29:28 -05:00
|
|
|
func NewResultHandler() *DefaultResultHandler {
|
|
|
|
return &DefaultResultHandler{
|
2016-07-26 05:29:52 -05:00
|
|
|
log: log.New("alerting.resultHandler"),
|
|
|
|
notifier: NewRootNotifier(),
|
2016-06-23 05:57:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 03:07:00 -05:00
|
|
|
func (handler *DefaultResultHandler) Handle(ctx *EvalContext) {
|
|
|
|
oldState := ctx.Rule.State
|
2016-07-22 06:14:09 -05:00
|
|
|
|
2016-08-18 04:37:35 -05:00
|
|
|
exeuctionError := ""
|
2016-08-31 07:06:54 -05:00
|
|
|
annotationData := simplejson.New()
|
2016-08-01 03:07:00 -05:00
|
|
|
if ctx.Error != nil {
|
|
|
|
handler.log.Error("Alert Rule Result Error", "ruleId", ctx.Rule.Id, "error", ctx.Error)
|
2016-09-13 08:09:55 -05:00
|
|
|
ctx.Rule.State = m.AlertStateExecError
|
2016-08-17 02:27:29 -05:00
|
|
|
exeuctionError = ctx.Error.Error()
|
2016-08-31 07:06:54 -05:00
|
|
|
annotationData.Set("errorMessage", exeuctionError)
|
2016-08-01 03:07:00 -05:00
|
|
|
} else if ctx.Firing {
|
2016-09-13 08:09:55 -05:00
|
|
|
ctx.Rule.State = m.AlertStateAlerting
|
2016-08-31 07:06:54 -05:00
|
|
|
annotationData = simplejson.NewFromAny(ctx.EvalMatches)
|
2016-07-22 06:14:09 -05:00
|
|
|
} else {
|
2016-09-06 13:40:12 -05:00
|
|
|
// handle no data case
|
|
|
|
if ctx.NoDataFound {
|
|
|
|
ctx.Rule.State = ctx.Rule.NoDataState
|
|
|
|
} else {
|
|
|
|
ctx.Rule.State = m.AlertStateOK
|
|
|
|
}
|
2016-07-21 10:31:46 -05:00
|
|
|
}
|
|
|
|
|
2016-08-31 04:55:35 -05:00
|
|
|
countStateResult(ctx.Rule.State)
|
2016-08-01 03:07:00 -05:00
|
|
|
if ctx.Rule.State != oldState {
|
|
|
|
handler.log.Info("New state change", "alertId", ctx.Rule.Id, "newState", ctx.Rule.State, "oldState", oldState)
|
2016-07-21 10:31:46 -05:00
|
|
|
|
2016-07-22 06:14:09 -05:00
|
|
|
cmd := &m.SetAlertStateCommand{
|
2016-09-14 07:35:05 -05:00
|
|
|
AlertId: ctx.Rule.Id,
|
|
|
|
OrgId: ctx.Rule.OrgId,
|
|
|
|
State: ctx.Rule.State,
|
|
|
|
Error: exeuctionError,
|
|
|
|
EvalData: annotationData,
|
2016-07-21 10:31:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(cmd); err != nil {
|
|
|
|
handler.log.Error("Failed to save state", "error", err)
|
|
|
|
}
|
|
|
|
|
2016-08-01 03:07:00 -05:00
|
|
|
// save annotation
|
|
|
|
item := annotations.Item{
|
2016-09-09 04:30:55 -05:00
|
|
|
OrgId: ctx.Rule.OrgId,
|
|
|
|
DashboardId: ctx.Rule.DashboardId,
|
|
|
|
PanelId: ctx.Rule.PanelId,
|
|
|
|
Type: annotations.AlertType,
|
|
|
|
AlertId: ctx.Rule.Id,
|
|
|
|
Title: ctx.Rule.Name,
|
|
|
|
Text: ctx.GetStateModel().Text,
|
|
|
|
NewState: string(ctx.Rule.State),
|
|
|
|
PrevState: string(oldState),
|
|
|
|
Epoch: time.Now().Unix(),
|
|
|
|
Data: annotationData,
|
2016-08-01 03:07:00 -05:00
|
|
|
}
|
2016-08-16 14:26:54 -05:00
|
|
|
|
2016-08-01 03:07:00 -05:00
|
|
|
annotationRepo := annotations.GetRepository()
|
|
|
|
if err := annotationRepo.Save(&item); err != nil {
|
|
|
|
handler.log.Error("Failed to save annotation for new alert state", "error", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.notifier.Notify(ctx)
|
2016-07-21 10:31:46 -05:00
|
|
|
}
|
2016-06-23 05:57:10 -05:00
|
|
|
}
|
2016-08-11 14:12:39 -05:00
|
|
|
|
2016-08-31 04:55:35 -05:00
|
|
|
func countStateResult(state m.AlertStateType) {
|
2016-08-11 14:12:39 -05:00
|
|
|
switch state {
|
2016-09-13 08:09:55 -05:00
|
|
|
case m.AlertStateAlerting:
|
|
|
|
metrics.M_Alerting_Result_State_Alerting.Inc(1)
|
2016-08-31 04:55:35 -05:00
|
|
|
case m.AlertStateOK:
|
|
|
|
metrics.M_Alerting_Result_State_Ok.Inc(1)
|
|
|
|
case m.AlertStatePaused:
|
|
|
|
metrics.M_Alerting_Result_State_Paused.Inc(1)
|
2016-09-13 08:09:55 -05:00
|
|
|
case m.AlertStateNoData:
|
|
|
|
metrics.M_Alerting_Result_State_NoData.Inc(1)
|
|
|
|
case m.AlertStateExecError:
|
|
|
|
metrics.M_Alerting_Result_State_ExecError.Inc(1)
|
2016-08-11 14:12:39 -05:00
|
|
|
}
|
|
|
|
}
|