vm.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 alertresourcedrivers
  15. import (
  16. "yunion.io/x/onecloud/pkg/apis/monitor"
  17. "yunion.io/x/onecloud/pkg/monitor/models"
  18. )
  19. func init() {
  20. models.RegisterAlertResourceDriverFactory(new(vmDriverF))
  21. }
  22. type vmDriverF struct{}
  23. func (v vmDriverF) GetType() monitor.AlertResourceType {
  24. return monitor.AlertResourceTypeVM
  25. }
  26. const (
  27. VM_TAG_VM_ID = "vm_id"
  28. VM_TAG_VM_NAME = "vm_name"
  29. )
  30. func (v vmDriverF) IsEvalMatched(input monitor.EvalMatch) bool {
  31. tags := input.Tags
  32. /*resType, hasResType := tags[hostconsts.TELEGRAF_TAG_KEY_RES_TYPE]
  33. if !hasResType {
  34. return false
  35. }
  36. if !sets.NewString("guest", "agent").Has(resType) {
  37. return false
  38. }*/
  39. _, hasVmTag := tags[VM_TAG_VM_ID]
  40. _, hasVmName := tags[VM_TAG_VM_NAME]
  41. return hasVmTag && hasVmName
  42. }
  43. func (v vmDriverF) GetDriver(input monitor.EvalMatch) models.IAlertResourceDriver {
  44. return &guestDriver{
  45. vmDriverF: &v,
  46. match: input,
  47. }
  48. }
  49. type guestDriver struct {
  50. *vmDriverF
  51. match monitor.EvalMatch
  52. }
  53. func (g guestDriver) GetUniqCond() *models.AlertResourceUniqCond {
  54. tags := g.match.Tags
  55. name := tags[VM_TAG_VM_NAME]
  56. return &models.AlertResourceUniqCond{
  57. Type: g.GetType(),
  58. Name: name,
  59. }
  60. }