monitor_resource_sync.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 models
  15. import (
  16. "fmt"
  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/hostman/hostinfo/hostconsts"
  21. )
  22. var (
  23. resourceSyncMap map[string]IResourceSync
  24. guestResourceSync IResourceSync
  25. hostResourceSync IResourceSync
  26. redisResourceSync IResourceSync
  27. rdsResourceSync IResourceSync
  28. ossResourceSync IResourceSync
  29. accountResourceSync IResourceSync
  30. storageResourceSync IResourceSync
  31. )
  32. func RegistryResourceSync(sync IResourceSync) error {
  33. if resourceSyncMap == nil {
  34. resourceSyncMap = make(map[string]IResourceSync)
  35. }
  36. if _, ok := resourceSyncMap[sync.SyncType()]; ok {
  37. return errors.Errorf("syncType:%s has registered", sync.SyncType())
  38. }
  39. resourceSyncMap[sync.SyncType()] = sync
  40. return nil
  41. }
  42. func GetResourceSyncByType(syncType string) IResourceSync {
  43. if resourceSyncMap == nil {
  44. resourceSyncMap = make(map[string]IResourceSync)
  45. }
  46. return resourceSyncMap[syncType]
  47. }
  48. func GetResourceSyncMap() map[string]IResourceSync {
  49. if resourceSyncMap == nil {
  50. resourceSyncMap = make(map[string]IResourceSync)
  51. }
  52. return resourceSyncMap
  53. }
  54. type SyncObject struct {
  55. sync IResourceSync
  56. }
  57. type IResourceSync interface {
  58. SyncType() string
  59. }
  60. type GuestResourceSync struct {
  61. SyncObject
  62. }
  63. func NewGuestResourceSync() IResourceSync {
  64. if guestResourceSync == nil {
  65. sync := new(GuestResourceSync)
  66. obj := newSyncObj(sync)
  67. sync.SyncObject = obj
  68. guestResourceSync = sync
  69. }
  70. return guestResourceSync
  71. }
  72. func (g *GuestResourceSync) SyncType() string {
  73. return monitor.METRIC_RES_TYPE_GUEST
  74. }
  75. type HostResourceSync struct {
  76. SyncObject
  77. }
  78. func (self *HostResourceSync) SyncType() string {
  79. return monitor.METRIC_RES_TYPE_HOST
  80. }
  81. func NewHostResourceSync() IResourceSync {
  82. if hostResourceSync == nil {
  83. sync := new(HostResourceSync)
  84. obj := newSyncObj(sync)
  85. sync.SyncObject = obj
  86. hostResourceSync = sync
  87. }
  88. return hostResourceSync
  89. }
  90. type RedisResourceSync struct {
  91. SyncObject
  92. }
  93. func NewRedisResourceSync() IResourceSync {
  94. if redisResourceSync == nil {
  95. sync := new(RedisResourceSync)
  96. obj := newSyncObj(sync)
  97. sync.SyncObject = obj
  98. redisResourceSync = sync
  99. }
  100. return redisResourceSync
  101. }
  102. func (g *RedisResourceSync) SyncType() string {
  103. return monitor.METRIC_RES_TYPE_REDIS
  104. }
  105. type RdsResourceSync struct {
  106. SyncObject
  107. }
  108. func NewRdsResourceSync() IResourceSync {
  109. if rdsResourceSync == nil {
  110. sync := new(RdsResourceSync)
  111. obj := newSyncObj(sync)
  112. sync.SyncObject = obj
  113. rdsResourceSync = sync
  114. }
  115. return rdsResourceSync
  116. }
  117. func (g *RdsResourceSync) SyncType() string {
  118. return monitor.METRIC_RES_TYPE_RDS
  119. }
  120. type OssResourceSync struct {
  121. SyncObject
  122. }
  123. func NewOssResourceSync() IResourceSync {
  124. if ossResourceSync == nil {
  125. sync := new(OssResourceSync)
  126. obj := newSyncObj(sync)
  127. sync.SyncObject = obj
  128. ossResourceSync = sync
  129. }
  130. return ossResourceSync
  131. }
  132. func (g *OssResourceSync) SyncType() string {
  133. return monitor.METRIC_RES_TYPE_OSS
  134. }
  135. type AccountResourceSync struct {
  136. SyncObject
  137. }
  138. func NewAccountResourceSync() IResourceSync {
  139. if accountResourceSync == nil {
  140. sync := new(AccountResourceSync)
  141. obj := newSyncObj(sync)
  142. sync.SyncObject = obj
  143. accountResourceSync = sync
  144. }
  145. return accountResourceSync
  146. }
  147. func (g *AccountResourceSync) SyncType() string {
  148. return monitor.METRIC_RES_TYPE_CLOUDACCOUNT
  149. }
  150. type StorageResourceSync struct {
  151. SyncObject
  152. }
  153. func NewStorageResourceSync() IResourceSync {
  154. if storageResourceSync == nil {
  155. sync := new(StorageResourceSync)
  156. obj := newSyncObj(sync)
  157. sync.SyncObject = obj
  158. storageResourceSync = sync
  159. }
  160. return storageResourceSync
  161. }
  162. func (g *StorageResourceSync) SyncType() string {
  163. return monitor.METRIC_RES_TYPE_STORAGE
  164. }
  165. func newSyncObj(sync IResourceSync) SyncObject {
  166. return SyncObject{sync: sync}
  167. }
  168. func SetQueryHostType(q *jsonutils.JSONDict) {
  169. q.Set("filter", jsonutils.NewString(
  170. fmt.Sprintf("host_type.in(%s,%s)",
  171. hostconsts.TELEGRAF_TAG_KEY_HYPERVISOR,
  172. hostconsts.TELEGRAF_TAG_KEY_CONTAINER)))
  173. }