nodealert.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. "strings"
  17. )
  18. const (
  19. NodeAlertTypeGuest = "guest"
  20. NodeAlertTypeHost = "host"
  21. )
  22. type ResourceAlertV1CreateInput struct {
  23. AlertCreateInput
  24. // 查询指标周期
  25. Period string `json:"period"`
  26. // 每隔多久查询一次
  27. Window string `json:"window"`
  28. // 比较运算符, 比如: >, <, >=, <=
  29. Comparator string `json:"comparator"`
  30. // 报警阀值
  31. Threshold float64 `json:"threshold"`
  32. // 通知方式, 比如: email, mobile
  33. Channel string `json:"channel"`
  34. // 通知接受者
  35. Recipients string `json:"recipients"`
  36. }
  37. type NodeAlertCreateInput struct {
  38. ResourceAlertV1CreateInput
  39. // 监控指标名称
  40. Metric string `json:"metric"`
  41. // 监控资源类型, 比如: guest, host
  42. Type string `json:"type"`
  43. // 监控资源名称
  44. NodeName string `json:"node_name"`
  45. // 监控资源 Id
  46. NodeId string `json:"node_id"`
  47. // CommonAlertCreateInput injected by server and do not pass it through API
  48. CommonAlertCreateInput *CommonAlertCreateInput `json:"common_alert_create_input,omitempty"`
  49. }
  50. func (input NodeAlertCreateInput) ToCommonAlertCreateInput(
  51. name string,
  52. field string,
  53. measurement string,
  54. db string) CommonAlertCreateInput {
  55. ret := CommonAlertCreateInput{
  56. AlertCreateInput: AlertCreateInput{
  57. Name: name,
  58. Level: input.Level,
  59. },
  60. CommonAlertCreateBaseInput: CommonAlertCreateBaseInput{
  61. Channel: strings.Split(input.Channel, ","),
  62. Recipients: strings.Split(input.Recipients, ","),
  63. AlertType: CommonAlertNomalAlertType,
  64. },
  65. CommonMetricInputQuery: CommonMetricInputQuery{
  66. MetricQuery: []*CommonAlertQuery{
  67. {
  68. AlertQuery: &AlertQuery{
  69. Model: input.GetQuery(field, measurement, db),
  70. From: input.Period,
  71. To: "now",
  72. },
  73. Comparator: input.Comparator,
  74. Threshold: input.Threshold,
  75. ConditionType: "query",
  76. Reduce: "avg",
  77. },
  78. },
  79. },
  80. Period: input.Window,
  81. }
  82. ret.UsedBy = AlertNotificationUsedByNodeAlert
  83. return ret
  84. }
  85. // func (input NodeAlertCreateInput) ToAlertCreateInput(
  86. // name string,
  87. // field string,
  88. // measurement string,
  89. // db string) AlertCreateInput {
  90. // freq, _ := time.ParseDuration(input.Window)
  91. // ret := AlertCreateInput{
  92. // Name: name,
  93. // Frequency: int64(freq / time.Second),
  94. // Level: input.Level,
  95. // Settings: AlertSetting{
  96. // Conditions: []AlertCondition{
  97. // {
  98. // Type: "query",
  99. // Operator: "and",
  100. // Query: AlertQuery{
  101. // Model: input.GetQuery(field, measurement, db),
  102. // From: input.Period,
  103. // To: "now",
  104. // },
  105. // Evaluator: input.GetEvaluator(),
  106. // Reducer: Condition{
  107. // Type: "avg",
  108. // },
  109. // },
  110. // },
  111. // },
  112. // }
  113. // return ret
  114. // }
  115. func (input NodeAlertCreateInput) GetQuery(field, measurement, db string) MetricQuery {
  116. return GetNodeAlertQuery(input.Type, field, measurement, db, input.NodeId)
  117. }
  118. func GetNodeAlertQuery(typ, field, measurement, db, nodeId string) MetricQuery {
  119. var idField string
  120. switch typ {
  121. case NodeAlertTypeGuest:
  122. idField = "vm_id"
  123. case NodeAlertTypeHost:
  124. idField = "host_id"
  125. }
  126. sels := make([]MetricQuerySelect, 0)
  127. sels = append(sels, NewMetricQuerySelect(MetricQueryPart{Type: "field", Params: []string{field}}))
  128. return MetricQuery{
  129. Selects: sels,
  130. Tags: []MetricQueryTag{
  131. {
  132. Key: idField,
  133. Operator: "=",
  134. Value: nodeId,
  135. },
  136. },
  137. GroupBy: []MetricQueryPart{
  138. {
  139. Type: "field",
  140. Params: []string{"*"},
  141. },
  142. },
  143. Measurement: measurement,
  144. Database: db,
  145. }
  146. }
  147. func (input NodeAlertCreateInput) GetEvaluator() Condition {
  148. return GetNodeAlertEvaluator(input.Comparator, input.Threshold)
  149. }
  150. const (
  151. ConditionGreaterThan = "gt"
  152. ConditionLessThan = "lt"
  153. )
  154. func GetNodeAlertEvaluator(comparator string, threshold float64) Condition {
  155. typ := EvaluatorType(ConditionGreaterThan)
  156. switch comparator {
  157. case ">=", ">":
  158. typ = EvaluatorType(ConditionGreaterThan)
  159. case "<=", "<":
  160. typ = EvaluatorType(ConditionLessThan)
  161. }
  162. return Condition{
  163. Type: string(typ),
  164. Params: []float64{threshold},
  165. }
  166. }
  167. type V1AlertListInput struct {
  168. AlertListInput
  169. }
  170. type NodeAlertListInput struct {
  171. AlertListInput
  172. // 监控指标名称
  173. Metric string `json:"metric"`
  174. // 监控资源类型, 比如: guest, host
  175. Type string `json:"type"`
  176. // 监控资源名称
  177. NodeName string `json:"node_name"`
  178. // 监控资源 Id
  179. NodeId string `json:"node_id"`
  180. }
  181. func (o NodeAlertListInput) ToCommonAlertListInput() CommonAlertListInput {
  182. return CommonAlertListInput{
  183. AlertListInput: o.AlertListInput,
  184. Metric: o.Metric,
  185. }
  186. }
  187. type AlertV1Details struct {
  188. AlertDetails
  189. Name string `json:"name"`
  190. Period string `json:"period"`
  191. Window string `json:"window"`
  192. Comparator string `json:"comparator"`
  193. Threshold float64 `json:"threshold"`
  194. Recipients string `json:"recipients"`
  195. Level string `json:"level"`
  196. Channel string `json:"channel"`
  197. DB string `json:"db"`
  198. Measurement string `json:"measurement"`
  199. Field string `json:"field"`
  200. NotifierId string `json:"notifier_id"`
  201. Status string `json:"status"`
  202. }
  203. type NodeAlertDetails struct {
  204. AlertV1Details
  205. Type string `json:"type"`
  206. Metric string `json:"metric"`
  207. NodeId string `json:"node_id"`
  208. NodeName string `json:"node_name"`
  209. }
  210. type NodeAlertUpdateInput struct {
  211. AlertUpdateInput
  212. // 监控指标名称
  213. Metric *string `json:"metric"`
  214. // 监控资源类型, 比如: guest, host
  215. Type *string `json:"type"`
  216. // 监控资源名称
  217. NodeName *string `json:"node_name"`
  218. // 监控资源 Id
  219. NodeId *string `json:"node_id"`
  220. // 查询指标周期
  221. Period *string `json:"period"`
  222. // 每隔多久查询一次
  223. Window *string `json:"window"`
  224. // 比较运算符, 比如: >, <, >=, <=
  225. Comparator *string `json:"comparator"`
  226. // 报警阀值
  227. Threshold *float64 `json:"threshold"`
  228. // 报警级别
  229. Level *string `json:"level"`
  230. // 通知方式, 比如: email, mobile
  231. Channel *string `json:"channel"`
  232. // 通知接受者
  233. Recipients *string `json:"recipients"`
  234. }
  235. type V1AlertUpdateInput struct {
  236. AlertUpdateInput
  237. }