| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- package conditions
- import (
- "fmt"
- "strings"
- "time"
- "github.com/grafana/grafana/pkg/bus"
- "github.com/grafana/grafana/pkg/components/simplejson"
- m "github.com/grafana/grafana/pkg/models"
- "github.com/grafana/grafana/pkg/services/alerting"
- "github.com/grafana/grafana/pkg/tsdb"
- )
- func init() {
- alerting.RegisterCondition("query", func(model *simplejson.Json, index int) (alerting.Condition, error) {
- return NewQueryCondition(model, index)
- })
- }
- type QueryCondition struct {
- Index int
- Query AlertQuery
- Reducer QueryReducer
- Evaluator AlertEvaluator
- HandleRequest tsdb.HandleRequestFunc
- }
- type AlertQuery struct {
- Model *simplejson.Json
- DatasourceId int64
- From string
- To string
- }
- func (c *QueryCondition) Eval(context *alerting.EvalContext) {
- timeRange := tsdb.NewTimeRange(c.Query.From, c.Query.To)
- seriesList, err := c.executeQuery(context, timeRange)
- if err != nil {
- context.Error = err
- return
- }
- emptySerieCount := 0
- for _, series := range seriesList {
- reducedValue := c.Reducer.Reduce(series)
- evalMatch := c.Evaluator.Eval(reducedValue)
- if reducedValue.Valid == false {
- emptySerieCount++
- continue
- }
- if context.IsTestRun {
- context.Logs = append(context.Logs, &alerting.ResultLogEntry{
- Message: fmt.Sprintf("Condition[%d]: Eval: %v, Metric: %s, Value: %1.3f", c.Index, evalMatch, series.Name, reducedValue.Float64),
- })
- }
- if evalMatch {
- context.EvalMatches = append(context.EvalMatches, &alerting.EvalMatch{
- Metric: series.Name,
- Value: reducedValue.Float64,
- })
- }
- }
- context.NoDataFound = emptySerieCount == len(seriesList)
- context.Firing = len(context.EvalMatches) > 0
- }
- func (c *QueryCondition) executeQuery(context *alerting.EvalContext, timeRange *tsdb.TimeRange) (tsdb.TimeSeriesSlice, error) {
- getDsInfo := &m.GetDataSourceByIdQuery{
- Id: c.Query.DatasourceId,
- OrgId: context.Rule.OrgId,
- }
- if err := bus.Dispatch(getDsInfo); err != nil {
- return nil, fmt.Errorf("Could not find datasource")
- }
- req := c.getRequestForAlertRule(getDsInfo.Result, timeRange)
- result := make(tsdb.TimeSeriesSlice, 0)
- resp, err := c.HandleRequest(context.Context, req)
- if err != nil {
- return nil, fmt.Errorf("tsdb.HandleRequest() error %v", err)
- }
- for _, v := range resp.Results {
- if v.Error != nil {
- return nil, fmt.Errorf("tsdb.HandleRequest() response error %v", v)
- }
- result = append(result, v.Series...)
- if context.IsTestRun {
- context.Logs = append(context.Logs, &alerting.ResultLogEntry{
- Message: fmt.Sprintf("Condition[%d]: Query Result", c.Index),
- Data: v.Series,
- })
- }
- }
- return result, nil
- }
- func (c *QueryCondition) getRequestForAlertRule(datasource *m.DataSource, timeRange *tsdb.TimeRange) *tsdb.Request {
- req := &tsdb.Request{
- TimeRange: timeRange,
- Queries: []*tsdb.Query{
- {
- RefId: "A",
- Model: c.Query.Model,
- DataSource: &tsdb.DataSourceInfo{
- Id: datasource.Id,
- Name: datasource.Name,
- PluginId: datasource.Type,
- Url: datasource.Url,
- User: datasource.User,
- Password: datasource.Password,
- Database: datasource.Database,
- BasicAuth: datasource.BasicAuth,
- BasicAuthUser: datasource.BasicAuthUser,
- BasicAuthPassword: datasource.BasicAuthPassword,
- },
- },
- },
- }
- return req
- }
- func NewQueryCondition(model *simplejson.Json, index int) (*QueryCondition, error) {
- condition := QueryCondition{}
- condition.Index = index
- condition.HandleRequest = tsdb.HandleRequest
- queryJson := model.Get("query")
- condition.Query.Model = queryJson.Get("model")
- condition.Query.From = queryJson.Get("params").MustArray()[1].(string)
- condition.Query.To = queryJson.Get("params").MustArray()[2].(string)
- if err := validateFromValue(condition.Query.From); err != nil {
- return nil, err
- }
- if err := validateToValue(condition.Query.To); err != nil {
- return nil, err
- }
- condition.Query.DatasourceId = queryJson.Get("datasourceId").MustInt64()
- reducerJson := model.Get("reducer")
- condition.Reducer = NewSimpleReducer(reducerJson.Get("type").MustString())
- evaluatorJson := model.Get("evaluator")
- evaluator, err := NewAlertEvaluator(evaluatorJson)
- if err != nil {
- return nil, err
- }
- condition.Evaluator = evaluator
- return &condition, nil
- }
- func validateFromValue(from string) error {
- fromRaw := strings.Replace(from, "now-", "", 1)
- _, err := time.ParseDuration("-" + fromRaw)
- return err
- }
- func validateToValue(to string) error {
- if to == "now" {
- return nil
- } else if strings.HasPrefix(to, "now-") {
- withoutNow := strings.Replace(to, "now-", "", 1)
- _, err := time.ParseDuration("-" + withoutNow)
- if err == nil {
- return nil
- }
- }
- _, err := time.ParseDuration(to)
- return err
- }
|