rule.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package alerting
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strconv"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. m "github.com/grafana/grafana/pkg/models"
  8. )
  9. type Rule struct {
  10. Id int64
  11. OrgId int64
  12. DashboardId int64
  13. PanelId int64
  14. Frequency int64
  15. Name string
  16. Message string
  17. NoDataState m.NoDataOption
  18. ExecutionErrorState m.ExecutionErrorOption
  19. State m.AlertStateType
  20. Conditions []Condition
  21. Notifications []int64
  22. }
  23. type ValidationError struct {
  24. Reason string
  25. Err error
  26. Alertid int64
  27. DashboardId int64
  28. PanelId int64
  29. }
  30. func (e ValidationError) Error() string {
  31. extraInfo := ""
  32. if e.Alertid != 0 {
  33. extraInfo = fmt.Sprintf("%s AlertId: %v", extraInfo, e.Alertid)
  34. }
  35. if e.PanelId != 0 {
  36. extraInfo = fmt.Sprintf("%s PanelId: %v ", extraInfo, e.PanelId)
  37. }
  38. if e.DashboardId != 0 {
  39. extraInfo = fmt.Sprintf("%s DashboardId: %v", extraInfo, e.DashboardId)
  40. }
  41. if e.Err != nil {
  42. return fmt.Sprintf("%s %s%s", e.Err.Error(), e.Reason, extraInfo)
  43. }
  44. return fmt.Sprintf("Failed to extract alert.Reason: %s %s", e.Reason, extraInfo)
  45. }
  46. var (
  47. ValueFormatRegex = regexp.MustCompile(`^\d+`)
  48. UnitFormatRegex = regexp.MustCompile(`\w{1}$`)
  49. )
  50. var unitMultiplier = map[string]int{
  51. "s": 1,
  52. "m": 60,
  53. "h": 3600,
  54. }
  55. func getTimeDurationStringToSeconds(str string) (int64, error) {
  56. multiplier := 1
  57. matches := ValueFormatRegex.FindAllString(str, 1)
  58. if len(matches) <= 0 {
  59. return 0, fmt.Errorf("Frequency could not be parsed")
  60. }
  61. value, err := strconv.Atoi(matches[0])
  62. if err != nil {
  63. return 0, err
  64. }
  65. unit := UnitFormatRegex.FindAllString(str, 1)[0]
  66. if val, ok := unitMultiplier[unit]; ok {
  67. multiplier = val
  68. }
  69. return int64(value * multiplier), nil
  70. }
  71. func NewRuleFromDBAlert(ruleDef *m.Alert) (*Rule, error) {
  72. model := &Rule{}
  73. model.Id = ruleDef.Id
  74. model.OrgId = ruleDef.OrgId
  75. model.DashboardId = ruleDef.DashboardId
  76. model.PanelId = ruleDef.PanelId
  77. model.Name = ruleDef.Name
  78. model.Message = ruleDef.Message
  79. model.Frequency = ruleDef.Frequency
  80. model.State = ruleDef.State
  81. model.NoDataState = m.NoDataOption(ruleDef.Settings.Get("noDataState").MustString("no_data"))
  82. model.ExecutionErrorState = m.ExecutionErrorOption(ruleDef.Settings.Get("executionErrorState").MustString("alerting"))
  83. for _, v := range ruleDef.Settings.Get("notifications").MustArray() {
  84. jsonModel := simplejson.NewFromAny(v)
  85. id, err := jsonModel.Get("id").Int64()
  86. if err != nil {
  87. return nil, ValidationError{Reason: "Invalid notification schema", DashboardId: model.DashboardId, Alertid: model.Id, PanelId: model.PanelId}
  88. }
  89. model.Notifications = append(model.Notifications, id)
  90. }
  91. for index, condition := range ruleDef.Settings.Get("conditions").MustArray() {
  92. conditionModel := simplejson.NewFromAny(condition)
  93. conditionType := conditionModel.Get("type").MustString()
  94. factory, exist := conditionFactories[conditionType]
  95. if !exist {
  96. return nil, ValidationError{Reason: "Unknown alert condition: " + conditionType, DashboardId: model.DashboardId, Alertid: model.Id, PanelId: model.PanelId}
  97. }
  98. queryCondition, err := factory(conditionModel, index)
  99. if err != nil {
  100. return nil, ValidationError{Err: err, DashboardId: model.DashboardId, Alertid: model.Id, PanelId: model.PanelId}
  101. }
  102. model.Conditions = append(model.Conditions, queryCondition)
  103. }
  104. if len(model.Conditions) == 0 {
  105. return nil, fmt.Errorf("Alert is missing conditions")
  106. }
  107. return model, nil
  108. }
  109. type ConditionFactory func(model *simplejson.Json, index int) (Condition, error)
  110. var conditionFactories = make(map[string]ConditionFactory)
  111. func RegisterCondition(typeName string, factory ConditionFactory) {
  112. conditionFactories[typeName] = factory
  113. }