auto_migration.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 notifiers
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/log"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/onecloud/pkg/apis/monitor"
  20. "yunion.io/x/onecloud/pkg/mcclient"
  21. "yunion.io/x/onecloud/pkg/mcclient/auth"
  22. "yunion.io/x/onecloud/pkg/monitor/alerting"
  23. "yunion.io/x/onecloud/pkg/monitor/controller/balancer"
  24. "yunion.io/x/onecloud/pkg/monitor/models"
  25. "yunion.io/x/onecloud/pkg/monitor/options"
  26. )
  27. func init() {
  28. alerting.RegisterNotifier(&alerting.NotifierPlugin{
  29. Type: monitor.AlertNotificationTypeAutoMigration,
  30. Factory: newAutoMigratingNotifier,
  31. ValidateCreateData: func(cred mcclient.IIdentityProvider, input monitor.NotificationCreateInput) (monitor.NotificationCreateInput, error) {
  32. settings := new(monitor.NotificationSettingAutoMigration)
  33. if err := input.Settings.Unmarshal(settings); err != nil {
  34. return input, errors.Wrap(err, "Unmarshal setting")
  35. }
  36. input.Settings = jsonutils.Marshal(settings)
  37. return input, nil
  38. },
  39. })
  40. }
  41. type autoMigrationNotifier struct {
  42. NotifierBase
  43. Settings *monitor.NotificationSettingAutoMigration
  44. }
  45. func newAutoMigratingNotifier(conf alerting.NotificationConfig) (alerting.Notifier, error) {
  46. settings := new(monitor.NotificationSettingAutoMigration)
  47. if err := conf.Settings.Unmarshal(settings); err != nil {
  48. return nil, errors.Wrap(err, "unmarshal setting")
  49. }
  50. return &autoMigrationNotifier{
  51. NotifierBase: NewNotifierBase(conf),
  52. Settings: settings,
  53. }, nil
  54. }
  55. func (am *autoMigrationNotifier) getBalancerRules(ctx *alerting.EvalContext, alert *models.SMigrationAlert) (*balancer.Rules, error) {
  56. if len(ctx.EvalMatches) >= 1 {
  57. log.Warningf("EvalMatches great than 1, use first one")
  58. } else {
  59. return nil, errors.Errorf("Matches not >= 1 %d", len(ctx.EvalMatches))
  60. }
  61. match := ctx.EvalMatches[0]
  62. drv, err := balancer.GetMetricDrivers().Get(alert.GetMetricType())
  63. if err != nil {
  64. return nil, errors.Wrap(err, "Get metric driver")
  65. }
  66. log.Infof("autoMigrationNotifier for evalMatch: %s", jsonutils.Marshal(match))
  67. return balancer.NewRules(ctx, match, alert, drv, options.Options.AutoMigrationMustPair)
  68. }
  69. func (am *autoMigrationNotifier) Notify(ctx *alerting.EvalContext, data jsonutils.JSONObject) error {
  70. if !ctx.Firing {
  71. // do nothing
  72. return nil
  73. }
  74. alertId := ctx.Rule.Id
  75. man := models.GetMigrationAlertManager()
  76. obj, err := man.FetchById(alertId)
  77. if err != nil {
  78. return errors.Wrapf(err, "Fetch alert by Id: %q", alertId)
  79. }
  80. alert := obj.(*models.SMigrationAlert)
  81. rules, err := am.getBalancerRules(ctx, alert)
  82. if err != nil {
  83. return errors.Wrapf(err, "get balancer rules")
  84. }
  85. if err := balancer.DoBalance(ctx.Ctx, auth.GetAdminSession(ctx.Ctx, options.Options.Region), rules, balancer.NewRecorder()); err != nil {
  86. return errors.Wrap(err, "DoBalance")
  87. }
  88. return nil
  89. }