alerting.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package alerting
  2. import (
  3. "time"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/log"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/setting"
  8. )
  9. func Init() {
  10. if !setting.AlertingEnabled {
  11. return
  12. }
  13. log.Info("Alerting: Initializing scheduler...")
  14. scheduler := NewScheduler()
  15. reader := NewRuleReader()
  16. go scheduler.Dispatch(reader)
  17. go scheduler.Executor(&ExecutorImpl{})
  18. go scheduler.HandleResponses()
  19. }
  20. type Scheduler struct {
  21. jobs map[int64]*m.AlertJob
  22. runQueue chan *m.AlertJob
  23. responseQueue chan *m.AlertResult
  24. alertRuleFetcher RuleReader
  25. }
  26. func NewScheduler() *Scheduler {
  27. return &Scheduler{
  28. jobs: make(map[int64]*m.AlertJob, 0),
  29. runQueue: make(chan *m.AlertJob, 1000),
  30. responseQueue: make(chan *m.AlertResult, 1000),
  31. }
  32. }
  33. func (this *Scheduler) Dispatch(reader RuleReader) {
  34. reschedule := time.NewTicker(time.Second * 10)
  35. secondTicker := time.NewTicker(time.Second)
  36. this.updateJobs(reader.Fetch)
  37. for {
  38. select {
  39. case <-secondTicker.C:
  40. this.queueJobs()
  41. case <-reschedule.C:
  42. this.updateJobs(reader.Fetch)
  43. }
  44. }
  45. }
  46. func (this *Scheduler) updateJobs(f func() []m.AlertJob) {
  47. log.Debug("Scheduler: UpdateJobs()")
  48. jobs := make(map[int64]*m.AlertJob, 0)
  49. rules := f()
  50. for i := 0; i < len(rules); i++ {
  51. rule := rules[i]
  52. rule.Offset = int64(i)
  53. jobs[rule.Rule.Id] = &rule
  54. }
  55. log.Debug("Scheduler: Selected %d jobs", len(jobs))
  56. this.jobs = jobs
  57. }
  58. func (this *Scheduler) queueJobs() {
  59. now := time.Now().Unix()
  60. for _, job := range this.jobs {
  61. if now%job.Rule.Frequency == 0 && job.Running == false {
  62. log.Info("Scheduler: Putting job on to run queue: %s", job.Rule.Title)
  63. this.runQueue <- job
  64. }
  65. }
  66. }
  67. func (this *Scheduler) Executor(executor Executor) {
  68. for job := range this.runQueue {
  69. //log.Info("Executor: queue length %d", len(this.runQueue))
  70. log.Info("Executor: executing %s", job.Rule.Title)
  71. this.jobs[job.Rule.Id].Running = true
  72. this.MeasureAndExecute(executor, job)
  73. }
  74. }
  75. func (this *Scheduler) HandleResponses() {
  76. for response := range this.responseQueue {
  77. log.Info("Response: alert(%d) status(%s) actual(%v)", response.Id, response.State, response.ActualValue)
  78. if this.jobs[response.Id] != nil {
  79. this.jobs[response.Id].Running = false
  80. }
  81. cmd := m.UpdateAlertStateCommand{
  82. AlertId: response.Id,
  83. NewState: response.State,
  84. Info: response.Description,
  85. }
  86. if err := bus.Dispatch(&cmd); err != nil {
  87. log.Error(1, "failed to save state", err)
  88. }
  89. }
  90. }
  91. func (this *Scheduler) MeasureAndExecute(exec Executor, job *m.AlertJob) {
  92. now := time.Now()
  93. responseChan := make(chan *m.AlertResult, 1)
  94. go exec.Execute(job, responseChan)
  95. select {
  96. case <-time.After(time.Second * 5):
  97. this.responseQueue <- &m.AlertResult{
  98. Id: job.Rule.Id,
  99. State: "timed out",
  100. Duration: float64(time.Since(now).Nanoseconds()) / float64(1000000),
  101. Rule: job.Rule,
  102. }
  103. case result := <-responseChan:
  104. result.Duration = float64(time.Since(now).Nanoseconds()) / float64(1000000)
  105. log.Info("Schedular: exeuction took %vms", result.Duration)
  106. this.responseQueue <- result
  107. }
  108. }