nodealert.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. "yunion.io/x/onecloud/pkg/mcclient"
  18. modules "yunion.io/x/onecloud/pkg/mcclient/modules/monitor"
  19. "yunion.io/x/onecloud/pkg/mcclient/options"
  20. )
  21. func init() {
  22. /**
  23. * 创建一条报警规则
  24. */
  25. type NodealertCreateOptions struct {
  26. Type string `help:"Alert rule type" required:"true" choices:"guest|host"`
  27. Metric string `help:"Metric name, include measurement and field, e.g. vm_cpu.usage_active" required:"true"`
  28. NodeName string `help:"Name of the guest or host" required:"true"`
  29. NodeID string `help:"ID of the guest or host" required:"true"`
  30. Period string `help:"Specify the query time period for the data" required:"true"`
  31. Window string `help:"Specify the query interval for the data" required:"true"`
  32. Threshold float64 `help:"Threshold value of the metric" required:"true"`
  33. Comparator string `help:"Comparison operator for join expressions" choices:">|<|>=|<=|=|!=" required:"true"`
  34. Recipients string `help:"Comma separated recipient ID" required:"true"`
  35. Level string `help:"Alert level" required:"true" choices:"normal|important|fatal"`
  36. Channel string `help:"Ways to send an alarm" required:"true" choices:"email|mobile|dingtalk"`
  37. }
  38. R(&NodealertCreateOptions{}, "nodealert-create", "Create a node alert rule", func(s *mcclient.ClientSession, args *NodealertCreateOptions) error {
  39. params, err := options.StructToParams(args)
  40. if err != nil {
  41. return err
  42. }
  43. rst, err := modules.NodeAlert.Create(s, params)
  44. if err != nil {
  45. return err
  46. }
  47. printObject(rst)
  48. return nil
  49. })
  50. /**
  51. * 修改指定的报警规则
  52. */
  53. type NodealertUpdateOptions struct {
  54. ID string `help:"ID of the alert rule" required:"true" positional:"true"`
  55. Type string `help:"Alert rule type" choices:"guest|host"`
  56. Metric string `help:"Metric name, include measurement and field, such as vm_cpu.usage_active"`
  57. NodeName string `help:"Name of the guest or host"`
  58. NodeID string `help:"ID of the guest or host"`
  59. Period string `help:"Specify the query time period for the data"`
  60. Window string `help:"Specify the query interval for the data"`
  61. Threshold *float64 `help:"Threshold value of the metric"`
  62. Comparator string `help:"Comparison operator for join expressions" choices:">|<|>=|<=|=|!="`
  63. Recipients string `help:"Comma separated recipient ID"`
  64. Level string `help:"Alert level" choices:"normal|important|fatal"`
  65. Channel string `help:"Ways to send an alarm" choices:"email|mobile"`
  66. }
  67. R(&NodealertUpdateOptions{}, "nodealert-update", "Update the node alert rule", func(s *mcclient.ClientSession, args *NodealertUpdateOptions) error {
  68. params, err := options.StructToParams(args)
  69. if err != nil {
  70. return err
  71. }
  72. rst, err := modules.NodeAlert.Patch(s, args.ID, params)
  73. if err != nil {
  74. return err
  75. }
  76. printObject(rst)
  77. return nil
  78. })
  79. /**
  80. * 删除指定ID的报警规则
  81. */
  82. type NodealertDeleteOptions struct {
  83. ID []string `help:"ID of node alert" required:"true" positional:"true"`
  84. }
  85. R(&NodealertDeleteOptions{}, "nodealert-delete", "Delete a node alert", func(s *mcclient.ClientSession, args *NodealertDeleteOptions) error {
  86. ret := modules.NodeAlert.BatchDelete(s, args.ID, nil)
  87. printBatchResults(ret, modules.NodeAlert.GetColumns(s))
  88. return nil
  89. })
  90. /**
  91. * 启用指定ID的报警规则状态
  92. */
  93. type NodealertUpdateStatusOptions struct {
  94. ID string `help:"ID of the node alert" required:"true" positional:"true"`
  95. }
  96. R(&NodealertUpdateStatusOptions{}, "nodealert-enable", "Enable alert rule for the specified ID", func(s *mcclient.ClientSession, args *NodealertUpdateStatusOptions) error {
  97. params := jsonutils.NewDict()
  98. params.Add(jsonutils.NewString("Enabled"), "status")
  99. alarm, err := modules.NodeAlert.Patch(s, args.ID, params)
  100. if err != nil {
  101. return err
  102. }
  103. printObject(alarm)
  104. return nil
  105. })
  106. /**
  107. * 禁用指定ID的报警规则状态
  108. */
  109. R(&NodealertUpdateStatusOptions{}, "nodealert-disable", "Disaable alert rule for the specified ID", func(s *mcclient.ClientSession, args *NodealertUpdateStatusOptions) error {
  110. params := jsonutils.NewDict()
  111. params.Add(jsonutils.NewString("Disabled"), "status")
  112. alarm, err := modules.NodeAlert.Patch(s, args.ID, params)
  113. if err != nil {
  114. return err
  115. }
  116. printObject(alarm)
  117. return nil
  118. })
  119. /**
  120. * 列出报警规则
  121. */
  122. type NodealertListOptions struct {
  123. Type string `help:"Alarm rule type" choices:"guest|host"`
  124. Metric string `help:"Metric name, include measurement and field, e.g. vm_cpu.usage_active"`
  125. NodeName string `help:"Name of the guest or host"`
  126. NodeID string `help:"ID of the guest or host"`
  127. options.BaseListOptions
  128. }
  129. R(&NodealertListOptions{}, "nodealert-list", "List node alert", func(s *mcclient.ClientSession, args *NodealertListOptions) error {
  130. params, err := options.ListStructToParams(args)
  131. if err != nil {
  132. return err
  133. }
  134. result, err := modules.NodeAlert.List(s, params)
  135. if err != nil {
  136. return err
  137. }
  138. printList(result, modules.NodeAlert.GetColumns(s))
  139. return nil
  140. })
  141. }