alert.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. "fmt"
  17. "strings"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/onecloud/pkg/apis/monitor"
  21. monitor2 "yunion.io/x/onecloud/pkg/mcclient/modules/monitor"
  22. "yunion.io/x/onecloud/pkg/mcclient/options"
  23. )
  24. type AlertListOptions struct {
  25. options.BaseListOptions
  26. }
  27. func (o AlertListOptions) Params() (jsonutils.JSONObject, error) {
  28. return o.BaseListOptions.Params()
  29. }
  30. type AlertShowOptions struct {
  31. ID string `help:"ID or name of the alert" json:"-"`
  32. }
  33. func (o AlertShowOptions) GetId() string {
  34. return o.ID
  35. }
  36. func (o AlertShowOptions) Params() (jsonutils.JSONObject, error) {
  37. return nil, nil
  38. }
  39. type AlertDeleteOptions struct {
  40. ID []string `help:"ID of alert to delete"`
  41. }
  42. func (o AlertDeleteOptions) GetIds() []string {
  43. return o.ID
  44. }
  45. func (o AlertDeleteOptions) Params() (jsonutils.JSONObject, error) {
  46. return nil, nil
  47. }
  48. type AlertTestRunOptions struct {
  49. ID string `help:"ID of alert to delete"`
  50. Debug bool `help:"Show more debug info"`
  51. }
  52. type AlertPauseOptions struct {
  53. ID string `help:"ID of alert to delete"`
  54. UnPause bool `help:"Unpause alert"`
  55. }
  56. func (o AlertPauseOptions) GetId() string {
  57. return o.ID
  58. }
  59. func (o AlertPauseOptions) Params() (jsonutils.JSONObject, error) {
  60. data := new(monitor.AlertPauseInput)
  61. if o.UnPause {
  62. data.Paused = false
  63. } else {
  64. data.Paused = true
  65. }
  66. return data.JSON(data), nil
  67. }
  68. func (o AlertPauseOptions) Description() string {
  69. return "Pause or unpause alert"
  70. }
  71. type AlertConditionOptions struct {
  72. REDUCER string `help:"Metric query reducer, e.g. 'avg'" choices:"avg|sum|min|max|count|last|median"`
  73. DATABASE string `help:"Metric database, e.g. 'telegraf'"`
  74. METRIC string `help:"Query metric format <measurement>.<field>, e.g. 'cpu.cpu_usage'"`
  75. COMPARATOR string `help:"Evaluator compare" choices:"gt|lt"`
  76. THRESHOLD float64 `help:"Alert threshold"`
  77. Period string `help:"Query metric period e.g. '5m', '1h'" default:"5m"`
  78. Tag []string `help:"Query tag, e.g. 'zone=zon0,name=vmname'"`
  79. }
  80. func (opt AlertConditionOptions) Params(conf *monitor2.AlertConfig) (*monitor2.AlertCondition, error) {
  81. parts := strings.Split(opt.METRIC, ".")
  82. if len(parts) != 2 {
  83. return nil, fmt.Errorf("metric %s is invalid format", opt.METRIC)
  84. }
  85. cond := conf.Condition(opt.DATABASE, parts[0])
  86. if opt.COMPARATOR == "gt" {
  87. cond.GT(opt.THRESHOLD)
  88. }
  89. if opt.COMPARATOR == "lt" {
  90. cond.LT(opt.THRESHOLD)
  91. }
  92. switch opt.REDUCER {
  93. case "avg":
  94. cond.Avg()
  95. case "sum":
  96. cond.Sum()
  97. case "min":
  98. cond.Min()
  99. case "max":
  100. cond.Max()
  101. case "count":
  102. cond.Count()
  103. case "last":
  104. cond.Last()
  105. case "median":
  106. cond.Median()
  107. }
  108. q := cond.Query().From(opt.Period)
  109. q.Selects().Select(parts[1])
  110. for _, tag := range opt.Tag {
  111. parts := strings.Split(tag, "=")
  112. if len(parts) != 2 {
  113. return nil, fmt.Errorf("invalid tag format: %s", tag)
  114. }
  115. q.Where().Equal(parts[0], parts[1])
  116. }
  117. return cond, nil
  118. }
  119. type AlertStatesOptions struct {
  120. NoDataState string `help:"Set state when no data"`
  121. ExecutionErrorState string `help:"Set state when execution error"`
  122. }
  123. type AlertCreateOptions struct {
  124. AlertConditionOptions
  125. AlertStatesOptions
  126. NAME string `help:"Name of the alert"`
  127. Frequency string `help:"Alert execute frequency, e.g. '5m', '1h'"`
  128. Enabled bool `help:"Enable alert"`
  129. Level string `help:"Alert level"`
  130. For string `help:"For time duration"`
  131. }
  132. func (opt AlertCreateOptions) Params() (jsonutils.JSONObject, error) {
  133. input, err := monitor2.NewAlertConfig(opt.NAME, opt.Frequency, opt.Enabled)
  134. if err != nil {
  135. return nil, err
  136. }
  137. _, err = opt.AlertConditionOptions.Params(input)
  138. if err != nil {
  139. return nil, err
  140. }
  141. input.NoDataState(opt.NoDataState)
  142. input.ExecutionErrorState(opt.ExecutionErrorState)
  143. ret := input.ToAlertCreateInput()
  144. return ret.JSON(ret), nil
  145. }
  146. type AlertUpdateOptions struct {
  147. ID string `help:"ID or name of the alert"`
  148. Name string `help:"Update alert name"`
  149. Frequency string `help:"Alert execute frequency, e.g. '5m', '1h'"`
  150. AlertStatesOptions
  151. }
  152. func (opt AlertUpdateOptions) GetId() string {
  153. return opt.ID
  154. }
  155. func (opt AlertUpdateOptions) Params() (jsonutils.JSONObject, error) {
  156. input := new(monitor.AlertUpdateInput)
  157. if opt.Name != "" {
  158. input.Name = opt.Name
  159. }
  160. if opt.Frequency != "" {
  161. freq, err := time.ParseDuration(opt.Frequency)
  162. if err != nil {
  163. return nil, fmt.Errorf("Invalid frequency time format %s: %v", opt.Frequency, err)
  164. }
  165. f := int64(freq / time.Second)
  166. input.Frequency = &f
  167. }
  168. input.NoDataState = opt.NoDataState
  169. input.ExecutionErrorState = opt.ExecutionErrorState
  170. return input.JSON(input), nil
  171. }
  172. type AlertNotificationAttachOptions struct {
  173. ALERT string `help:"ID or name of alert"`
  174. NOTIFICATION string `help:"ID or name of alert notification"`
  175. UsedBy string `help:"UsedBy annotation"`
  176. }
  177. type AlertNotificationListOptions struct {
  178. options.BaseListOptions
  179. Notification string `help:"ID or name of notification" short-token:"n"`
  180. AlertId []string `help:"ID or name of alert" short-token:"a"`
  181. }
  182. func (o AlertNotificationListOptions) Params() (*jsonutils.JSONDict, error) {
  183. params, err := o.BaseListOptions.Params()
  184. if err != nil {
  185. return nil, err
  186. }
  187. if len(o.AlertId) > 0 {
  188. params.Add(jsonutils.NewStringArray(o.AlertId), "alert_ids")
  189. }
  190. return params, nil
  191. }