commonalert.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "time"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/onecloud/pkg/apis"
  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 CommonAlertListOptions struct {
  25. options.BaseListOptions
  26. // 报警类型
  27. AlertType string `help:"common alert type" choices:"normal|system"`
  28. Level string `help:"common alert notify level" choices:"normal|important|fatal"`
  29. MonitorResourceId []string `help:"monitor resource id"`
  30. StartTime *time.Time `help:"start time, format: 2025-01-01 00:00:00" json:"start_time"`
  31. EndTime *time.Time `help:"end time, format: 2025-01-01 00:00:00" json:"end_time"`
  32. Top *int `help:"top" json:"top"`
  33. }
  34. func (o *CommonAlertListOptions) Params() (jsonutils.JSONObject, error) {
  35. return options.ListStructToParams(o)
  36. }
  37. type CommonAlertShowOptions struct {
  38. ID string `help:"ID of alart " json:"-"`
  39. }
  40. func (o *CommonAlertShowOptions) Params() (jsonutils.JSONObject, error) {
  41. return options.StructToParams(o)
  42. }
  43. func (o *CommonAlertShowOptions) GetId() string {
  44. return o.ID
  45. }
  46. type CommonAlertUpdateOptions struct {
  47. ID string `help:"ID of alart " json:"-"`
  48. Period string `help:"exec period of alert" json:"period"`
  49. Comparator string `help:"Alarm policy threshold comparison method" json:"comparator" `
  50. Threshold string `help:"Alarm policy threshold" json:"threshold"`
  51. Reason string `help:"Alarm policy reason" json:"reason"`
  52. // 为 true 时不发送恢复通知
  53. DisableNotifyRecovery *bool `help:"when true, do not send recovery notifications" json:"disable_notify_recovery"`
  54. }
  55. func (o *CommonAlertUpdateOptions) Params() (jsonutils.JSONObject, error) {
  56. return options.StructToParams(o)
  57. }
  58. func (o *CommonAlertUpdateOptions) GetId() string {
  59. return o.ID
  60. }
  61. type CommonAlertDeleteOptions struct {
  62. ID []string `help:"ID of alart"`
  63. Force bool `help:"force to delete alert"`
  64. }
  65. func (o *CommonAlertDeleteOptions) GetIds() []string {
  66. return o.ID
  67. }
  68. func (o *CommonAlertDeleteOptions) Params() (jsonutils.JSONObject, error) {
  69. return options.StructToParams(o)
  70. }
  71. type CommonAlertConditionOptions struct {
  72. AlertConditionOptions
  73. }
  74. func (o CommonAlertConditionOptions) Params(conf *monitor2.AlertConfig) (*monitor2.AlertCondition, error) {
  75. cond, err := o.AlertConditionOptions.Params(conf)
  76. if err != nil {
  77. return nil, errors.Wrap(err, "AlertConditionOptions.Params")
  78. }
  79. return cond, nil
  80. }
  81. type CommonAlertCreateOptions struct {
  82. apis.Meta
  83. monitor.CommonAlertCreateBaseInput
  84. CommonAlertConditionOptions
  85. AlertStatesOptions
  86. NAME string `help:"Name of the alert"`
  87. // 报警级别
  88. Level string `json:"level"`
  89. }
  90. func (o *CommonAlertCreateOptions) Params() (jsonutils.JSONObject, error) {
  91. input, err := monitor2.NewAlertConfig(o.NAME, o.Period, true)
  92. if err != nil {
  93. return nil, err
  94. }
  95. if _, err := o.CommonAlertConditionOptions.Params(input); err != nil {
  96. return nil, err
  97. }
  98. input.NoDataState(o.NoDataState)
  99. input.ExecutionErrorState(o.ExecutionErrorState)
  100. ret := input.ToCommonAlertCreateInput(&o.CommonAlertCreateBaseInput)
  101. return ret.JSON(ret), nil
  102. }