fix foramt

This commit is contained in:
Matthew Stobbs 2024-03-18 22:46:14 -06:00
parent 3bcbfd3577
commit 2b9fd75474

View File

@ -9,25 +9,25 @@ type ClusterStats struct {
Allocated uint32 Allocated uint32
} }
Memory struct { Memory struct {
Total uint64 Total uint64
Free uint64 Free uint64
Buffers uint64 Buffers uint64
Cached uint64 Cached uint64
Allocated uint64 Allocated uint64
} }
Storage struct { Storage struct {
Total uint64 Total uint64
Used uint64 Used uint64
Free uint64 Free uint64
Active uint32 Active uint32
Inactive uint32 Inactive uint32
Pools uint32 Pools uint32
Volumes struct { Volumes struct {
Total uint32 Total uint32
Active uint32 Active uint32
Inactive uint32 Inactive uint32
} }
} }
VM struct { VM struct {
Count uint32 Count uint32
@ -39,13 +39,13 @@ type ClusterStats struct {
Available uint32 Available uint32
} }
Network struct { Network struct {
Count uint32 Count uint32
Active uint32 Active uint32
Inactive uint32 Inactive uint32
} }
old *ClusterStats old *ClusterStats
c *Cluster c *Cluster
} }
// ClusterStats is used to gather stats for the entire cluster // ClusterStats is used to gather stats for the entire cluster
@ -57,25 +57,25 @@ type StatDiff struct {
Allocated int Allocated int
} }
Memory struct { Memory struct {
Total int Total int
Free int Free int
Buffers int Buffers int
Cached int Cached int
Allocated int Allocated int
} }
Storage struct { Storage struct {
Total int Total int
Used int Used int
Free int Free int
Active int Active int
Inactive int Inactive int
Pools int Pools int
Volumes struct { Volumes struct {
Total int Total int
Active int Active int
Inactive int Inactive int
} }
} }
VM struct { VM struct {
Count int Count int
@ -87,13 +87,12 @@ type StatDiff struct {
Available int Available int
} }
Network struct { Network struct {
Count int Count int
Active int Active int
Inactive int Inactive int
} }
} }
// Init is given a cluster, which it then uses to load the initial statistics // Init is given a cluster, which it then uses to load the initial statistics
// Does not close connections, but uses the host connections available to the // Does not close connections, but uses the host connections available to the
// cluster to add statistics together. // cluster to add statistics together.
@ -105,155 +104,191 @@ func Init(c *Cluster) *ClusterStats {
// Update triggers the stats collector to refresh it's statistics // Update triggers the stats collector to refresh it's statistics
func (cs *ClusterStats) Update() { func (cs *ClusterStats) Update() {
cs.old = cs.copy() cs.old = cs.copy()
cs.reset() cs.reset()
// Start looping through each host in the cluster, adding to the total // Start looping through each host in the cluster, adding to the total
for _, h := range cs.c.hosts { for _, h := range cs.c.hosts {
cs.Host.Count++ cs.Host.Count++
cs.Host.Available++ cs.Host.Available++
cs.CPU.Sockets += h.HostInfo.Sockets cs.CPU.Sockets += h.HostInfo.Sockets
cs.CPU.Cores += h.HostInfo.Cores cs.CPU.Cores += h.HostInfo.Cores
cs.CPU.Threads += h.HostInfo.Threads cs.CPU.Threads += h.HostInfo.Threads
cs.Memory.Total += h.NodeMemory.Total cs.Memory.Total += h.NodeMemory.Total
cs.Memory.Free += h.NodeMemory.Free cs.Memory.Free += h.NodeMemory.Free
cs.Memory.Buffers += h.NodeMemory.Buffers cs.Memory.Buffers += h.NodeMemory.Buffers
cs.Memory.Cached += h.NodeMemory.Cached cs.Memory.Cached += h.NodeMemory.Cached
// Storage Pool counting
cs.Storage.Pools += uint32(len(h.StoragePoolList))
countedSharedPools := map[string]struct{}{}
// Loop through available storage pools
for _, sp := range h.StoragePoolList {
if _, ok := countedSharedPools[sp.Name]; ok {
// Already counted this shared pool, move on
continue
}
if sp.HAEnabled {
countedSharedPools[sp.Name] = struct{}{}
}
if !sp.Active {
cs.Storage.Inactive++
continue
}
cs.Storage.Active++
cs.Storage.Total += sp.Capacity
cs.Storage.Used += sp.Allocation
cs.Storage.Free += sp.Capacity - sp.Allocation
// Volumes in the pool
cs.Storage.Volumes.Total += uint32(len(sp.Volumes))
for range sp.Volumes {
cs.Storage.Volumes.Active++
}
}
// VM Count
cs.VM.Count += uint32(len(h.VMList))
for _, vm := range h.VMList {
cs.CPU.Allocated += uint32(vm.VCPUs)
cs.Memory.Allocated += uint64(vm.Memory)
if vm.Active {
cs.VM.Started++
continue
}
cs.VM.Stopped++
}
// Storage Pool counting // Network count
cs.Storage.Pools += uint32(len(h.StoragePoolList)) cs.Network.Count += uint32(len(h.NetworkList))
countedSharedPools := map[string]struct{}{} for _, ni := range h.NetworkList {
// Loop through available storage pools if ni.Active {
for _, sp := range h.StoragePoolList { cs.Network.Active++
if _, ok := countedSharedPools[sp.Name]; ok { continue
// Already counted this shared pool, move on }
continue cs.Network.Inactive++
} }
if sp.HAEnabled == true {
countedSharedPools[sp.Name] = struct{}{}
}
if sp.Active == false {
cs.Storage.Inactive++
continue
}
cs.Storage.Active++
cs.Storage.Total += sp.Capacity
cs.Storage.Used += sp.Allocation
cs.Storage.Free += sp.Capacity - sp.Allocation
// Volumes in the pool
cs.Storage.Volumes.Total += uint32(len(sp.Volumes))
for range sp.Volumes {
cs.Storage.Volumes.Active++
}
}
// VM Count
cs.VM.Count += uint32(len(h.VMList))
for _, vm := range h.VMList {
cs.CPU.Allocated += uint32(vm.VCPUs)
cs.Memory.Allocated += uint64(vm.Memory)
if vm.Active {
cs.VM.Started++
continue
}
cs.VM.Stopped++
}
// Network count
cs.Network.Count += uint32(len(h.NetworkList))
for _, ni := range h.NetworkList {
if ni.Active {
cs.Network.Active++
continue
}
cs.Network.Inactive++
}
} }
} }
// Diff returns a map of all the field and how they changed // Diff returns a map of all the field and how they changed
func (cs *ClusterStats) Diff() StatDiff { func (cs *ClusterStats) Diff() StatDiff {
return StatDiff{ return StatDiff{
CPU: struct{Sockets int; Cores int; Threads int; Allocated int}{ CPU: struct {
Sockets: int(cs.old.CPU.Sockets - cs.CPU.Sockets), Sockets int
Cores: int(cs.old.CPU.Cores - cs.CPU.Cores), Cores int
Threads: int(cs.old.CPU.Threads - cs.CPU.Threads), Threads int
Allocated: int(cs.old.CPU.Allocated - cs.CPU.Allocated), Allocated int
}, }{
Memory: struct{Total int; Free int; Buffers int; Cached int; Allocated int}{ Sockets: int(cs.old.CPU.Sockets - cs.CPU.Sockets),
Total: int(cs.old.Memory.Total - cs.Memory.Total), Cores: int(cs.old.CPU.Cores - cs.CPU.Cores),
Free: int(cs.old.Memory.Free - cs.Memory.Free), Threads: int(cs.old.CPU.Threads - cs.CPU.Threads),
Buffers: int(cs.old.Memory.Buffers - cs.Memory.Buffers), Allocated: int(cs.old.CPU.Allocated - cs.CPU.Allocated),
Cached: int(cs.old.Memory.Cached - cs.Memory.Cached), },
Allocated: int(cs.old.Memory.Allocated - cs.Memory.Allocated), Memory: struct {
}, Total int
Storage: struct{Total int; Used int; Free int; Active int; Inactive int; Pools int; Volumes struct{Total int; Active int; Inactive int}}{ Free int
Total: int(cs.old.Storage.Total - cs.Storage.Total), Buffers int
Used: int(cs.old.Storage.Used - cs.Storage.Used), Cached int
Free: int(cs.old.Storage.Free - cs.Storage.Free), Allocated int
Active: int(cs.old.Storage.Active - cs.Storage.Active), }{
Inactive: int(cs.old.Storage.Inactive - cs.Storage.Inactive), Total: int(cs.old.Memory.Total - cs.Memory.Total),
Pools: int(cs.old.Storage.Pools - cs.Storage.Pools), Free: int(cs.old.Memory.Free - cs.Memory.Free),
Volumes: struct{Total int; Active int; Inactive int}{ Buffers: int(cs.old.Memory.Buffers - cs.Memory.Buffers),
Total: int(cs.old.Storage.Volumes.Total - cs.Storage.Volumes.Total), Cached: int(cs.old.Memory.Cached - cs.Memory.Cached),
Active: int(cs.old.Storage.Volumes.Active - cs.Storage.Volumes.Active), Allocated: int(cs.old.Memory.Allocated - cs.Memory.Allocated),
Inactive: int(cs.old.Storage.Volumes.Inactive - cs.Storage.Volumes.Inactive), },
}, Storage: struct {
}, Total int
VM: struct{Count int; Started int; Stopped int}{ Used int
Count: int(cs.old.VM.Count - cs.VM.Count), Free int
Started: int(cs.old.VM.Started - cs.VM.Started), Active int
Stopped: int(cs.old.VM.Stopped - cs.VM.Stopped), Inactive int
}, Pools int
Host: struct{Count int; Available int}{ Volumes struct {
Count: int(cs.old.Host.Count - cs.Host.Count), Total int
Available: int(cs.old.Host.Available - cs.Host.Available), Active int
}, Inactive int
Network: struct{Count int; Active int; Inactive int}{ }
Count: int(cs.old.Network.Count - cs.Network.Count), }{
Active: int(cs.old.Network.Active - cs.Network.Active), Total: int(cs.old.Storage.Total - cs.Storage.Total),
Inactive: int(cs.old.Network.Inactive - cs.Network.Inactive), Used: int(cs.old.Storage.Used - cs.Storage.Used),
}, Free: int(cs.old.Storage.Free - cs.Storage.Free),
} Active: int(cs.old.Storage.Active - cs.Storage.Active),
Inactive: int(cs.old.Storage.Inactive - cs.Storage.Inactive),
Pools: int(cs.old.Storage.Pools - cs.Storage.Pools),
Volumes: struct {
Total int
Active int
Inactive int
}{
Total: int(cs.old.Storage.Volumes.Total - cs.Storage.Volumes.Total),
Active: int(cs.old.Storage.Volumes.Active - cs.Storage.Volumes.Active),
Inactive: int(cs.old.Storage.Volumes.Inactive - cs.Storage.Volumes.Inactive),
},
},
VM: struct {
Count int
Started int
Stopped int
}{
Count: int(cs.old.VM.Count - cs.VM.Count),
Started: int(cs.old.VM.Started - cs.VM.Started),
Stopped: int(cs.old.VM.Stopped - cs.VM.Stopped),
},
Host: struct {
Count int
Available int
}{
Count: int(cs.old.Host.Count - cs.Host.Count),
Available: int(cs.old.Host.Available - cs.Host.Available),
},
Network: struct {
Count int
Active int
Inactive int
}{
Count: int(cs.old.Network.Count - cs.Network.Count),
Active: int(cs.old.Network.Active - cs.Network.Active),
Inactive: int(cs.old.Network.Inactive - cs.Network.Inactive),
},
}
} }
// copy the clusterstats into a new clusterstatus object for comparison purposes // copy the clusterstats into a new clusterstatus object for comparison purposes
func (cs *ClusterStats) copy() *ClusterStats { func (cs *ClusterStats) copy() *ClusterStats {
ncs := *cs ncs := *cs
return &ncs return &ncs
} }
// reset all values to zero value // reset all values to zero value
func (cs *ClusterStats) reset() { func (cs *ClusterStats) reset() {
cs.CPU.Sockets=0 cs.CPU.Sockets = 0
cs.CPU.Cores=0 cs.CPU.Cores = 0
cs.CPU.Threads=0 cs.CPU.Threads = 0
cs.CPU.Allocated=0 cs.CPU.Allocated = 0
cs.Memory.Total=0 cs.Memory.Total = 0
cs.Memory.Free=0 cs.Memory.Free = 0
cs.Memory.Buffers=0 cs.Memory.Buffers = 0
cs.Memory.Cached=0 cs.Memory.Cached = 0
cs.Storage.Total=0 cs.Storage.Total = 0
cs.Storage.Used=0 cs.Storage.Used = 0
cs.Storage.Free=0 cs.Storage.Free = 0
cs.Storage.Active=0 cs.Storage.Active = 0
cs.Storage.Inactive=0 cs.Storage.Inactive = 0
cs.Storage.Pools=0 cs.Storage.Pools = 0
cs.VM.Count=0 cs.VM.Count = 0
cs.VM.Started=0 cs.VM.Started = 0
cs.VM.Stopped=0 cs.VM.Stopped = 0
cs.Host.Count=0 cs.Host.Count = 0
cs.Host.Available=0 cs.Host.Available = 0
cs.Network.Count=0 cs.Network.Count = 0
cs.Network.Active=0 cs.Network.Active = 0
cs.Network.Inactive=0 cs.Network.Inactive = 0
} }