| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package resources |
| 4 | |
| 5 | import ( |
| 6 | "time" |
| 7 | |
| 8 | "github.com/vmware/govmomi/performance" |
| 9 | "github.com/vmware/govmomi/vim25/types" |
| 10 | ) |
| 11 | |
| 12 | /* |
| 13 | |
| 14 | ``` |
| 15 | Virtual Datacenter Architecture Representation (partial). |
| 16 | |
| 17 | <root> |
| 18 | +-DC0 # Virtual datacenter |
| 19 | +-datastore # Datastore folder (created by system) |
| 20 | | +-Datastore1 |
| 21 | | |
| 22 | +-host # Host folder (created by system) |
| 23 | | +-Folder1 # Host and Cluster folder |
| 24 | | | +-NestedFolder1 |
| 25 | | | | +-Cluster1 |
| 26 | | | | | +-Host1 |
| 27 | | +-Cluster2 |
| 28 | | | +-Host2 |
| 29 | | | | +-VM1 |
| 30 | | | | +-VM2 |
| 31 | | | | +-hadoop1 |
| 32 | | +-Host3 # Dummy folder for non-clustered host (created by system) |
| 33 | | | +-Host3 |
| 34 | | | | +-VM3 |
| 35 | | | | +-VM4 |
| 36 | | | | |
| 37 | +-vm # VM folder (created by system) |
| 38 | | +-VM1 |
| 39 | | +-VM2 |
| 40 | | +-Folder2 # VM and Template folder |
| 41 | | | +-hadoop1 |
| 42 | | | +-NestedFolder1 |
| 43 | | | | +-VM3 |
| 44 | | | | +-VM4 |
| 45 | ``` |
| 46 | */ |
| 47 | |
| 48 | type Resources struct { |
| 49 | DataCenters DataCenters |
| 50 | Folders Folders |
| 51 | Clusters Clusters |
| 52 | Hosts Hosts |
| 53 | VMs VMs |
| 54 | Datastores Datastores |
| 55 | Networks Networks |
| 56 | StoragePods StoragePods |
| 57 | ResourcePools ResourcePools |
| 58 | } |
| 59 | |
| 60 | type ( |
| 61 | Datacenter struct { |
| 62 | Name string |
| 63 | ID string |
| 64 | } |
| 65 | |
| 66 | Folder struct { |
| 67 | Name string |
| 68 | ID string |
| 69 | ParentID string |
| 70 | } |
| 71 | |
| 72 | HierarchyValue struct { |
| 73 | ID, Name string |
| 74 | } |
| 75 | |
| 76 | ClusterHierarchy struct { |
| 77 | DC HierarchyValue |
| 78 | } |
| 79 | Cluster struct { |
| 80 | Name string |
| 81 | ID string |
| 82 | ParentID string |
| 83 | Hier ClusterHierarchy |
| 84 | Labels map[string]string |
| 85 | CustomValues map[int32]string |
| 86 | OverallStatus string |
| 87 | NumHosts int32 |
| 88 | NumEffectiveHosts int32 |
| 89 | TotalCpu int32 // MHz |
| 90 | TotalMemory int64 // bytes |
| 91 | EffectiveCpu int32 // MHz |
| 92 | EffectiveMemory int64 // MB |
| 93 | NumCpuCores int16 |
| 94 | NumCpuThreads int16 |
| 95 | NumVmotions int32 // cumulative count |
| 96 | DrsEnabled bool |
| 97 | DrsMode string // fullyAutomated/partiallyAutomated/manual |
| 98 | DrsVmotionRate int32 // 1-5 recommendation threshold |
| 99 | DrsScore int32 // 0-100, vSphere 7.0+, 0 if unavailable |
| 100 | CurrentBalance int32 // thousandths of std dev |
| 101 | TargetBalance int32 // thousandths of std dev |
| 102 | HaEnabled bool |
| 103 | HaAdmCtrlEnabled bool |
| 104 | HaHostMonitoring string |
| 105 | HaVMMonitoring string |
| 106 | HaVMComponentProtection string |
| 107 | VSANEnabled bool |
| 108 | VSANUUID string |
| 109 | // UsageSummary fields (nil when DRS disabled) |
| 110 | UsageCpuDemandMhz int32 |
| 111 | UsageMemDemandMB int32 |
| 112 | UsageCpuEntitledMhz int32 |
| 113 | UsageMemEntitledMB int32 |
| 114 | UsageCpuReservationMhz int32 |
| 115 | UsageMemReservationMB int32 |
| 116 | UsageTotalVmCount int32 |
| 117 | UsagePoweredOffVmCount int32 |
| 118 | MetricList performance.MetricList |
| 119 | Ref types.ManagedObjectReference |
| 120 | } |
| 121 | |
| 122 | ResourcePoolHierarchy struct { |
| 123 | DC HierarchyValue |
| 124 | Cluster HierarchyValue |
| 125 | } |
| 126 | ResourcePool struct { |
| 127 | Name string |
| 128 | ID string |
| 129 | ParentID string // owner cluster ref value |
| 130 | Hier ResourcePoolHierarchy |
| 131 | Labels map[string]string |
| 132 | CustomValues map[int32]string |
| 133 | // QuickStats (polled via PropertyCollector) |
| 134 | OverallCpuUsage int64 // MHz |
| 135 | OverallCpuDemand int64 // MHz |
| 136 | GuestMemoryUsage int64 // MB |
| 137 | HostMemoryUsage int64 // MB |
| 138 | DistributedCpuEntitlement int64 // MHz |
| 139 | DistributedMemoryEntitlement int64 // MB |
| 140 | PrivateMemory int64 // MB |
| 141 | SharedMemory int64 // MB |
| 142 | SwappedMemory int64 // MB |
| 143 | BalloonedMemory int64 // MB |
| 144 | OverheadMemory int64 // MB |
| 145 | ConsumedOverheadMemory int64 // MB |
| 146 | CompressedMemory int64 // KiB |
| 147 | // Runtime |
| 148 | CpuReservationUsed int64 // MHz |
| 149 | CpuMaxUsage int64 // MHz |
| 150 | CpuUnreservedForVm int64 // MHz |
| 151 | MemReservationUsed int64 // bytes |
| 152 | MemMaxUsage int64 // bytes |
| 153 | MemUnreservedForVm int64 // bytes |
| 154 | // Config |
| 155 | CpuReservation int64 // MHz, 0 if nil |
| 156 | CpuLimit int64 // MHz, -1 = unlimited |
| 157 | MemReservation int64 // MB, 0 if nil |
| 158 | MemLimit int64 // MB, -1 = unlimited |
| 159 | OverallStatus string |
| 160 | Ref types.ManagedObjectReference |
| 161 | } |
| 162 | |
| 163 | HostHierarchy struct { |
| 164 | DC HierarchyValue |
| 165 | Cluster HierarchyValue |
| 166 | } |
| 167 | Host struct { |
| 168 | Name string |
| 169 | ID string |
| 170 | ParentID string |
| 171 | Hier HostHierarchy |
| 172 | Labels map[string]string |
| 173 | CustomValues map[int32]string |
| 174 | ConnectionState string |
| 175 | PowerState string |
| 176 | InMaintenanceMode bool |
| 177 | OverallStatus string |
| 178 | VSANNodeUUID string |
| 179 | MetricList performance.MetricList |
| 180 | Ref types.ManagedObjectReference |
| 181 | } |
| 182 | |
| 183 | VMHierarchy struct { |
| 184 | DC HierarchyValue |
| 185 | Cluster HierarchyValue |
| 186 | Host HierarchyValue |
| 187 | } |
| 188 | |
| 189 | VM struct { |
| 190 | Name string |
| 191 | ID string |
| 192 | ParentID string |
| 193 | FolderParentID string |
| 194 | Hier VMHierarchy |
| 195 | Labels map[string]string |
| 196 | CustomValues map[int32]string |
| 197 | ConnectionState string |
| 198 | PowerState string |
| 199 | ToolsRunningStatus string |
| 200 | ToolsVersionStatus string |
| 201 | InstanceUUID string |
| 202 | ConsolidationNeeded bool |
| 203 | ConfigCPU int64 |
| 204 | ConfigMemory int64 |
| 205 | ConfigDisks int64 |
| 206 | ConfigNICs int64 |
| 207 | StorageCommitted int64 |
| 208 | StorageUncommitted int64 |
| 209 | StorageUnshared int64 |
| 210 | OverallStatus string |
| 211 | SnapshotCount int64 |
| 212 | SnapshotMaxChainDepth int64 |
| 213 | SnapshotOldestCreateTime time.Time |
| 214 | MetricList performance.MetricList |
| 215 | Ref types.ManagedObjectReference |
| 216 | } |
| 217 | |
| 218 | DatastoreHierarchy struct { |
| 219 | DC HierarchyValue |
| 220 | } |
| 221 | Datastore struct { |
| 222 | Name string |
| 223 | ID string |
| 224 | ParentID string |
| 225 | Hier DatastoreHierarchy |
| 226 | Labels map[string]string |
| 227 | CustomValues map[int32]string |
| 228 | OverallStatus string |
| 229 | Type string // VMFS, NFS, NFS41, vsan, VVOL, PMEM |
| 230 | Capacity int64 // bytes |
| 231 | FreeSpace int64 // bytes |
| 232 | Uncommitted int64 // bytes |
| 233 | Accessible bool |
| 234 | MaintenanceMode string |
| 235 | MultipleHostAccess *bool |
| 236 | MetricList performance.MetricList |
| 237 | Ref types.ManagedObjectReference |
| 238 | } |
| 239 | |
| 240 | NetworkHierarchy struct { |
| 241 | DC HierarchyValue |
| 242 | } |
| 243 | Network struct { |
| 244 | Name string |
| 245 | ID string |
| 246 | Type string |
| 247 | ParentID string |
| 248 | Hier NetworkHierarchy |
| 249 | Labels map[string]string |
| 250 | CustomValues map[int32]string |
| 251 | Accessible bool |
| 252 | IPPoolName string |
| 253 | HostIDs []string |
| 254 | VMIDs []string |
| 255 | OverallStatus string |
| 256 | Ref types.ManagedObjectReference |
| 257 | } |
| 258 | |
| 259 | StoragePodHierarchy struct { |
| 260 | DC HierarchyValue |
| 261 | } |
| 262 | StoragePod struct { |
| 263 | Name string |
| 264 | ID string |
| 265 | ParentID string |
| 266 | Hier StoragePodHierarchy |
| 267 | Labels map[string]string |
| 268 | CustomValues map[int32]string |
| 269 | Capacity int64 |
| 270 | FreeSpace int64 |
| 271 | StorageDRSEnabled *bool |
| 272 | OverallStatus string |
| 273 | Ref types.ManagedObjectReference |
| 274 | } |
| 275 | ) |
| 276 | |
| 277 | func (v *HierarchyValue) IsSet() bool { return v.ID != "" && v.Name != "" } |
| 278 | func (v *HierarchyValue) Set(id, name string) { v.ID = id; v.Name = name } |
| 279 | |
| 280 | func (h ClusterHierarchy) IsSet() bool { return h.DC.IsSet() } |
| 281 | func (h HostHierarchy) IsSet() bool { return h.DC.IsSet() && h.Cluster.IsSet() } |
| 282 | func (h VMHierarchy) IsSet() bool { return h.DC.IsSet() && h.Cluster.IsSet() && h.Host.IsSet() } |
| 283 | func (h DatastoreHierarchy) IsSet() bool { return h.DC.IsSet() } |
| 284 | func (h NetworkHierarchy) IsSet() bool { return h.DC.IsSet() } |
| 285 | func (h StoragePodHierarchy) IsSet() bool { return h.DC.IsSet() } |
| 286 | func (h ResourcePoolHierarchy) IsSet() bool { return h.DC.IsSet() && h.Cluster.IsSet() } |
| 287 | |
| 288 | func (h *Host) IsPoweredOn() bool { |
| 289 | return h != nil && h.PowerState == string(types.HostSystemPowerStatePoweredOn) |
| 290 | } |
| 291 | |
| 292 | func (v *VM) IsPoweredOn() bool { |
| 293 | return v != nil && v.PowerState == string(types.VirtualMachinePowerStatePoweredOn) |
| 294 | } |
| 295 | |
| 296 | func SetClusterVSANInfo(cluster *Cluster, config types.BaseComputeResourceConfigInfo) { |
| 297 | if cluster == nil { |
| 298 | return |
| 299 | } |
| 300 | |
| 301 | cluster.VSANEnabled = false |
| 302 | cluster.VSANUUID = "" |
| 303 | |
| 304 | cfg, ok := config.(*types.ClusterConfigInfoEx) |
| 305 | if !ok || cfg.VsanConfigInfo == nil { |
| 306 | return |
| 307 | } |
| 308 | if cfg.VsanConfigInfo.Enabled != nil { |
| 309 | cluster.VSANEnabled = *cfg.VsanConfigInfo.Enabled |
| 310 | } |
| 311 | if cfg.VsanConfigInfo.DefaultConfig != nil { |
| 312 | cluster.VSANUUID = cfg.VsanConfigInfo.DefaultConfig.Uuid |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | type ( |
| 317 | DataCenters map[string]*Datacenter |
| 318 | Folders map[string]*Folder |
| 319 | Clusters map[string]*Cluster |
| 320 | Hosts map[string]*Host |
| 321 | VMs map[string]*VM |
| 322 | Datastores map[string]*Datastore |
| 323 | Networks map[string]*Network |
| 324 | StoragePods map[string]*StoragePod |
| 325 | ResourcePools map[string]*ResourcePool |
| 326 | ) |
| 327 | |
| 328 | func (dcs DataCenters) Put(dc *Datacenter) { dcs[dc.ID] = dc } |
| 329 | func (dcs DataCenters) Get(id string) *Datacenter { return dcs[id] } |
| 330 | func (fs Folders) Put(folder *Folder) { fs[folder.ID] = folder } |
| 331 | func (fs Folders) Get(id string) *Folder { return fs[id] } |
| 332 | func (cs Clusters) Put(cluster *Cluster) { cs[cluster.ID] = cluster } |
| 333 | func (cs Clusters) Remove(id string) { delete(cs, id) } |
| 334 | func (cs Clusters) Get(id string) *Cluster { return cs[id] } |
| 335 | func (hs Hosts) Put(host *Host) { hs[host.ID] = host } |
| 336 | func (hs Hosts) Remove(id string) { delete(hs, id) } |
| 337 | func (hs Hosts) Get(id string) *Host { return hs[id] } |
| 338 | func (vs VMs) Put(vm *VM) { vs[vm.ID] = vm } |
| 339 | func (vs VMs) Remove(id string) { delete(vs, id) } |
| 340 | func (vs VMs) Get(id string) *VM { return vs[id] } |
| 341 | func (ds Datastores) Put(d *Datastore) { ds[d.ID] = d } |
| 342 | func (ds Datastores) Remove(id string) { delete(ds, id) } |
| 343 | func (ds Datastores) Get(id string) *Datastore { return ds[id] } |
| 344 | func (ns Networks) Put(n *Network) { ns[n.ID] = n } |
| 345 | func (ns Networks) Remove(id string) { delete(ns, id) } |
| 346 | func (ns Networks) Get(id string) *Network { return ns[id] } |
| 347 | func (sps StoragePods) Put(sp *StoragePod) { sps[sp.ID] = sp } |
| 348 | func (sps StoragePods) Remove(id string) { delete(sps, id) } |
| 349 | func (sps StoragePods) Get(id string) *StoragePod { return sps[id] } |
| 350 | func (rp ResourcePools) Put(p *ResourcePool) { rp[p.ID] = p } |
| 351 | func (rp ResourcePools) Remove(id string) { delete(rp, id) } |
| 352 | func (rp ResourcePools) Get(id string) *ResourcePool { return rp[id] } |