migration_alert.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/onecloud/pkg/apis/monitor"
  20. "yunion.io/x/onecloud/pkg/mcclient/options"
  21. )
  22. type MigrationAlertListOptions struct {
  23. options.BaseListOptions
  24. MetricType string `help:"Migration alert metric type" choices:"cpu.usage_active|mem.available"`
  25. }
  26. func (o *MigrationAlertListOptions) Params() (jsonutils.JSONObject, error) {
  27. return options.ListStructToParams(o)
  28. }
  29. type MigrationAlertShowOptions struct {
  30. ID string `help:"ID of alart " json:"-"`
  31. }
  32. func (o *MigrationAlertShowOptions) Params() (jsonutils.JSONObject, error) {
  33. return options.StructToParams(o)
  34. }
  35. func (o *MigrationAlertShowOptions) GetId() string {
  36. return o.ID
  37. }
  38. type MigrationAlertCreateOptions struct {
  39. NAME string `help:"Name of the migration alert"`
  40. METRIC string `help:"Metric type" choices:"cpu.usage_active.gt|mem.available.lt"`
  41. THRESHOLD float64 `help:"Metric threshold"`
  42. Period string `help:"Period of execution, e.g. '5m', '1h'" default:"5m"`
  43. SourceHost []string `help:"Source hosts' id or name"`
  44. SourceGuest []string `help:"Source guests's id or name"`
  45. TargetHost []string `help:"Target hosts' id or name"`
  46. }
  47. func (o *MigrationAlertCreateOptions) parseMetric(m string) (monitor.MigrationAlertMetricType, error) {
  48. parts := strings.Split(m, ".")
  49. if len(parts) != 3 {
  50. return "", errors.Errorf("Invalid metric %q", m)
  51. }
  52. return monitor.MigrationAlertMetricType(strings.Join([]string{parts[0], parts[1]}, ".")), nil
  53. }
  54. func (o *MigrationAlertCreateOptions) Params() (jsonutils.JSONObject, error) {
  55. input := new(monitor.MigrationAlertCreateInput)
  56. input.Name = o.NAME
  57. input.Threshold = o.THRESHOLD
  58. input.Period = o.Period
  59. mt, err := o.parseMetric(o.METRIC)
  60. if err != nil {
  61. return nil, errors.Wrap(err, "metric_type")
  62. }
  63. input.MetricType = mt
  64. input.MigrationSettings = &monitor.MigrationAlertSettings{
  65. Source: &monitor.MigrationAlertSettingsSource{},
  66. Target: &monitor.MigrationAlertSettingsTarget{},
  67. }
  68. if len(o.SourceHost) != 0 {
  69. input.MigrationSettings.Source.HostIds = o.SourceHost
  70. }
  71. if len(o.SourceGuest) != 0 {
  72. input.MigrationSettings.Source.GuestIds = o.SourceGuest
  73. }
  74. if len(o.TargetHost) != 0 {
  75. input.MigrationSettings.Target.HostIds = o.TargetHost
  76. }
  77. return input.JSON(input), nil
  78. }