alert.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package monitor
  15. import (
  16. "reflect"
  17. "time"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/gotypes"
  21. "yunion.io/x/onecloud/pkg/apis"
  22. )
  23. type AlertStateType string
  24. type AlertSeverityType string
  25. type NoDataOption string
  26. type ExecutionErrorOption string
  27. const (
  28. AlertStateNoData AlertStateType = "no_data"
  29. AlertStatePaused AlertStateType = "paused"
  30. AlertStateAlerting AlertStateType = "alerting"
  31. AlertStateOK AlertStateType = "ok"
  32. AlertStatePending AlertStateType = "pending"
  33. AlertStateUnknown AlertStateType = "unknown"
  34. )
  35. const (
  36. NoDataSetOK NoDataOption = "ok"
  37. NoDataSetNoData NoDataOption = "no_data"
  38. NoDataKeepState NoDataOption = "keep_state"
  39. NoDataSetAlerting NoDataOption = "alerting"
  40. )
  41. const (
  42. ExecutionErrorSetAlerting ExecutionErrorOption = "alerting"
  43. ExecutionErrorKeepState ExecutionErrorOption = "keep_state"
  44. )
  45. var (
  46. ErrCannotChangeStateOnPausedAlert = errors.Error("Cannot change state on pause alert")
  47. ErrRequiresNewState = errors.Error("update alert state requires a new state")
  48. )
  49. func (s AlertStateType) IsValid() bool {
  50. return s == AlertStateOK ||
  51. s == AlertStateNoData ||
  52. s == AlertStatePaused ||
  53. s == AlertStatePending ||
  54. s == AlertStateAlerting ||
  55. s == AlertStateUnknown
  56. }
  57. func (s NoDataOption) IsValid() bool {
  58. return s == NoDataSetNoData || s == NoDataSetAlerting || s == NoDataKeepState || s == NoDataSetOK
  59. }
  60. func (s NoDataOption) ToAlertState() AlertStateType {
  61. return AlertStateType(s)
  62. }
  63. func (s ExecutionErrorOption) IsValid() bool {
  64. return s == ExecutionErrorSetAlerting || s == ExecutionErrorKeepState
  65. }
  66. func (s ExecutionErrorOption) ToAlertState() AlertStateType {
  67. return AlertStateType(s)
  68. }
  69. // AlertSettings contains alert conditions
  70. type AlertSetting struct {
  71. Conditions []AlertCondition `json:"conditions"`
  72. }
  73. func (s AlertSetting) String() string {
  74. return jsonutils.Marshal(s).String()
  75. }
  76. func (s AlertSetting) IsZero() bool {
  77. return len(s.Conditions) == 0
  78. }
  79. type AlertCondition struct {
  80. Type string `json:"type"`
  81. Query AlertQuery `json:"query"`
  82. Reducer Condition `json:"reducer"`
  83. ReducerOrder ResultReducerOrder `json:"reducer_order"`
  84. Evaluator Condition `json:"evaluator"`
  85. Operator string `json:"operator"`
  86. }
  87. type ResultReducerOrder string
  88. const (
  89. RESULT_REDUCER_ORDER_ASC ResultReducerOrder = "asc"
  90. RESULT_REDUCER_ORDER_DESC ResultReducerOrder = "desc"
  91. )
  92. type AlertQuery struct {
  93. Model MetricQuery `json:"model"`
  94. From string `json:"from"`
  95. To string `json:"to"`
  96. // 查询结果 reducer,执行 p95 这些操作
  97. ResultReducer *Condition `json:"result_reducer"`
  98. ResultReducerOrder ResultReducerOrder `json:"result_reducer_order"`
  99. }
  100. type AlertCreateInput struct {
  101. apis.Meta
  102. // 报警名称
  103. Name string `json:"name"`
  104. // 报警执行频率
  105. Frequency int64 `json:"frequency"`
  106. // 报警持续时间
  107. For int64 `json:"for"`
  108. // 报警设置
  109. Settings AlertSetting `json:"settings"`
  110. // 启用报警
  111. Enabled *bool `json:"enabled"`
  112. // 报警级别
  113. Level string `json:"level"`
  114. // 没有收到监控指标时将当前报警状态设置为对应的状态
  115. NoDataState string `json:"no_data_state"`
  116. // 报警执行错误将当前报警状态设置为对应的状态
  117. ExecutionErrorState string `json:"execution_error_state"`
  118. UsedBy string `json:"used_by"`
  119. // customize info
  120. CustomizeConfig jsonutils.JSONObject `json:"customize_config"`
  121. Reason string `json:"reason"`
  122. }
  123. type MeterCustomizeConfig struct {
  124. UnitDesc string `json:"unit_desc"`
  125. Name string `json:"name"`
  126. Currency string `json:"currency"`
  127. }
  128. type AlertUpdateInput struct {
  129. apis.StandaloneResourceBaseUpdateInput
  130. Message *string `json:"message"`
  131. // 报警执行频率
  132. Frequency *int64 `json:"frequency"`
  133. // 报警持续时间
  134. For int64 `json:"for"`
  135. // 报警设置
  136. Settings *AlertSetting `json:"settings"`
  137. // 启用报警
  138. Enabled *bool `json:"enabled"`
  139. // 报警级别
  140. Level *string `json:"level"`
  141. // 没有收到监控指标时将当前报警状态设置为对应的状态
  142. NoDataState string `json:"no_data_state"`
  143. // 报警执行错误将当前报警状态设置为对应的状态
  144. ExecutionErrorState string `json:"execution_error_state"`
  145. // 报警原因
  146. Reason string `json:"reason"`
  147. }
  148. type AlertListInput struct {
  149. apis.ScopedResourceBaseListInput
  150. apis.EnabledResourceBaseListInput
  151. apis.StatusStandaloneResourceListInput
  152. // 以报警是否启用/禁用过滤列表
  153. // Enabled *bool `json:"enabled"`
  154. MonitorResourceId []string `json:"monitor_resource_id"`
  155. }
  156. type AlertDetails struct {
  157. SAlert
  158. apis.StatusStandaloneResourceDetails
  159. apis.ScopedResourceBaseInfo
  160. }
  161. type AlertTestRunInput struct {
  162. apis.Meta
  163. IsDebug bool `json:"is_debug"`
  164. }
  165. // ResultLogEntry represents log data for the alert evaluation.
  166. type ResultLogEntry struct {
  167. Message string `json:"message"`
  168. Data interface{} `json:"data"`
  169. }
  170. // EvalMatch represents the series violating the threshold.
  171. type EvalMatch struct {
  172. Condition string `json:"condition"`
  173. Value *float64 `json:"value"`
  174. ValueStr string `json:"value_str"`
  175. Metric string `json:"metric"`
  176. Tags map[string]string `json:"tags"`
  177. Unit string `json:"unit"`
  178. AlertDetails jsonutils.JSONObject `json:"alert_details"`
  179. IsRecovery bool `json:"is_recovery"`
  180. }
  181. type AlertTestRunOutput struct {
  182. apis.Meta
  183. Firing bool `json:"firing"`
  184. IsTestRun bool `json:"is_test_run"`
  185. IsDebug bool `json:"is_debug"`
  186. EvalMatches []*EvalMatch `json:"eval_matches"`
  187. AlertOKEvalMatches []*EvalMatch `json:"alert_ok_eval_matches"`
  188. Logs []*ResultLogEntry `json:"logs"`
  189. Error error `json:"error"`
  190. ConditionEvals string `json:"condition_evals"`
  191. StartTime time.Time `json:"start_time"`
  192. EndTime time.Time `json:"end_time"`
  193. NoDataFound bool `json:"no_data_found"`
  194. PrevAlertState string `json:"prev_alert_state"`
  195. }
  196. type AlertPauseInput struct {
  197. apis.Meta
  198. Paused bool `json:"paused"`
  199. }
  200. func init() {
  201. gotypes.RegisterSerializable(reflect.TypeOf(&AlertSetting{}), func() gotypes.ISerializable {
  202. return &AlertSetting{}
  203. })
  204. }
  205. type EvaluatorType string
  206. const (
  207. EvaluatorTypeGT EvaluatorType = "gt"
  208. EvaluatorTypeLT EvaluatorType = "lt"
  209. EvaluatorTypeEQ EvaluatorType = "eq"
  210. EvaluatorTypeWithinRange EvaluatorType = "within_range"
  211. EvaluatorTypeOutsideRange EvaluatorType = "outside_range"
  212. )