executor.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package alerting
  2. import (
  3. m "github.com/grafana/grafana/pkg/models"
  4. "github.com/grafana/grafana/pkg/services/alerting/graphite"
  5. )
  6. type Executor interface {
  7. Execute(rule m.AlertRule, responseQueue chan *AlertResult)
  8. }
  9. type ExecutorImpl struct{}
  10. func (this *ExecutorImpl) Execute(rule m.AlertRule, responseQueue chan *AlertResult) {
  11. response, err := graphite.GraphiteClient{}.GetSeries(rule)
  12. if err != nil {
  13. responseQueue <- &AlertResult{State: "CRITICAL", Id: rule.Id}
  14. }
  15. responseQueue <- this.executeRules(response, rule)
  16. }
  17. func (this *ExecutorImpl) executeRules(series m.TimeSeriesSlice, rule m.AlertRule) *AlertResult {
  18. for _, v := range series {
  19. var avg float64
  20. var sum float64
  21. for _, dp := range v.Points {
  22. sum += dp[0]
  23. }
  24. avg = sum / float64(len(v.Points))
  25. if float64(rule.CritLevel) < avg {
  26. return &AlertResult{State: m.AlertStateCritical, Id: rule.Id, ActualValue: avg}
  27. }
  28. if float64(rule.WarnLevel) < avg {
  29. return &AlertResult{State: m.AlertStateWarn, Id: rule.Id, ActualValue: avg}
  30. }
  31. if float64(rule.CritLevel) < sum {
  32. return &AlertResult{State: m.AlertStateCritical, Id: rule.Id, ActualValue: sum}
  33. }
  34. if float64(rule.WarnLevel) < sum {
  35. return &AlertResult{State: m.AlertStateWarn, Id: rule.Id, ActualValue: sum}
  36. }
  37. }
  38. return &AlertResult{State: m.AlertStateOk, Id: rule.Id}
  39. }