commonalert.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. "yunion.io/x/jsonutils"
  17. monitorapi "yunion.io/x/onecloud/pkg/apis/monitor"
  18. "yunion.io/x/onecloud/pkg/mcclient"
  19. modules "yunion.io/x/onecloud/pkg/mcclient/modules/monitor"
  20. options "yunion.io/x/onecloud/pkg/mcclient/options/monitor"
  21. )
  22. type CommonAlertTerm struct {
  23. Database string
  24. Measurement string
  25. Operator string // and / or
  26. Field []string
  27. FieldFunc string
  28. Reduce string
  29. Comparator string
  30. Threshold float64
  31. Filters []monitorapi.MetricQueryTag
  32. FieldOpt monitorapi.CommonAlertFieldOpt
  33. Name string
  34. ConditionType string
  35. From string
  36. Interval string
  37. GroupBy string
  38. Level string
  39. }
  40. func newCommonAlertQuery(tem *CommonAlertTerm) *monitorapi.CommonAlertQuery {
  41. mq := monitorapi.MetricQuery{
  42. Alias: "",
  43. Tz: "",
  44. Database: tem.Database,
  45. Measurement: tem.Measurement,
  46. Tags: make([]monitorapi.MetricQueryTag, 0),
  47. GroupBy: make([]monitorapi.MetricQueryPart, 0),
  48. Selects: nil,
  49. Interval: "",
  50. Policy: "",
  51. ResultFormat: "",
  52. }
  53. for _, field := range tem.Field {
  54. sel := monitorapi.MetricQueryPart{
  55. Type: "field",
  56. Params: []string{field},
  57. }
  58. selectPart := []monitorapi.MetricQueryPart{sel}
  59. if len(tem.FieldFunc) != 0 {
  60. selectPart = append(selectPart, monitorapi.MetricQueryPart{
  61. Type: tem.FieldFunc,
  62. Params: []string{},
  63. })
  64. } else {
  65. selectPart = append(selectPart, monitorapi.MetricQueryPart{
  66. Type: "mean",
  67. Params: []string{},
  68. })
  69. }
  70. mq.Selects = append(mq.Selects, selectPart)
  71. }
  72. if len(tem.Filters) != 0 {
  73. mq.Tags = append(mq.Tags, tem.Filters...)
  74. }
  75. alertQ := new(monitorapi.AlertQuery)
  76. alertQ.Model = mq
  77. alertQ.From = "60m"
  78. commonAlert := monitorapi.CommonAlertQuery{
  79. AlertQuery: alertQ,
  80. Reduce: tem.Reduce,
  81. Comparator: tem.Comparator,
  82. Threshold: tem.Threshold,
  83. Operator: tem.Operator,
  84. }
  85. if tem.FieldOpt != "" {
  86. commonAlert.FieldOpt = monitorapi.CommonAlertFieldOptDivision
  87. }
  88. if len(tem.ConditionType) != 0 {
  89. commonAlert.ConditionType = tem.ConditionType
  90. }
  91. if len(tem.GroupBy) != 0 {
  92. commonAlert.Model.GroupBy = append(commonAlert.Model.GroupBy, monitorapi.MetricQueryPart{
  93. Type: "field",
  94. Params: []string{tem.GroupBy},
  95. })
  96. }
  97. return &commonAlert
  98. }
  99. var (
  100. cpuTem = &CommonAlertTerm{
  101. Operator: "or",
  102. Database: "telegraf",
  103. Measurement: "vm_cpu",
  104. Field: []string{"usage_active"},
  105. Comparator: ">=",
  106. Reduce: "avg",
  107. Threshold: 50,
  108. Name: "lzx-test.cpu.usage_active",
  109. Filters: []monitorapi.MetricQueryTag{
  110. {
  111. Key: "id",
  112. Operator: "=",
  113. Value: "a0eee5dd-3cfe-4ab1-8c79-aee1a8cf4dab",
  114. },
  115. },
  116. }
  117. memTem = &CommonAlertTerm{
  118. Operator: "or",
  119. Database: "telegraf",
  120. Measurement: "vm_mem",
  121. Field: []string{"used_percent"},
  122. Comparator: ">=",
  123. Reduce: "avg",
  124. Threshold: 3,
  125. Name: "lzx-test.mem.avaiable",
  126. Filters: []monitorapi.MetricQueryTag{
  127. {
  128. Key: "id",
  129. Operator: "=",
  130. Value: "a0eee5dd-3cfe-4ab1-8c79-aee1a8cf4dab",
  131. },
  132. },
  133. }
  134. )
  135. func init() {
  136. cmd := NewResourceCmd(modules.CommonAlerts)
  137. cmd.Create(new(options.CommonAlertCreateOptions))
  138. cmd.List(new(options.CommonAlertListOptions))
  139. cmd.Show(new(options.CommonAlertShowOptions))
  140. cmd.Perform("enable", &options.CommonAlertShowOptions{})
  141. cmd.Perform("disable", &options.CommonAlertShowOptions{})
  142. cmd.BatchDelete(new(options.CommonAlertDeleteOptions))
  143. cmd.Perform("config", &options.CommonAlertUpdateOptions{})
  144. type TestOpt struct {
  145. NAME string
  146. RobotIds []string
  147. Users []string
  148. }
  149. R(&TestOpt{}, "monitor-commonalert-create-mul-test", "create test monitor common alert", func(s *mcclient.ClientSession, opt *TestOpt) error {
  150. cpuQ := newCommonAlertQuery(cpuTem)
  151. memQ := newCommonAlertQuery(memTem)
  152. input := monitorapi.CommonAlertCreateInput{
  153. CommonMetricInputQuery: monitorapi.CommonMetricInputQuery{
  154. MetricQuery: []*monitorapi.CommonAlertQuery{
  155. cpuQ,
  156. memQ,
  157. },
  158. },
  159. AlertCreateInput: monitorapi.AlertCreateInput{
  160. Name: opt.NAME,
  161. },
  162. CommonAlertCreateBaseInput: monitorapi.CommonAlertCreateBaseInput{
  163. Recipients: opt.Users,
  164. RobotIds: opt.RobotIds,
  165. Channel: []string{"webconsole"},
  166. AlertType: monitorapi.CommonAlertNomalAlertType,
  167. Scope: "system",
  168. },
  169. }
  170. _, err := modules.CommonAlerts.Create(s, jsonutils.Marshal(input))
  171. return err
  172. })
  173. }