46 lines
834 B
Go
46 lines
834 B
Go
|
package host
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"libvirt.org/go/libvirtxml"
|
||
|
)
|
||
|
|
||
|
// Stats for hosts with channels to subscribe to
|
||
|
|
||
|
// HostStats works as a handler that can be subscribed to.
|
||
|
// It runs a go routine that sends Stats via a channel
|
||
|
// when subscribed to
|
||
|
type HostStats struct {
|
||
|
h *Host
|
||
|
}
|
||
|
|
||
|
type Stats struct {
|
||
|
Memory NodeMemoryInfo
|
||
|
VMs []*libvirtxml.Domain
|
||
|
StoragePools []*libvirtxml.StoragePool
|
||
|
}
|
||
|
|
||
|
func (hs *HostStats) Subscribe(parent context.Context, pubto <-chan Stats) error {
|
||
|
ctx, cancel := context.WithCancel(parent)
|
||
|
go func() {
|
||
|
defer cancel()
|
||
|
for {
|
||
|
select {
|
||
|
case <-ctx.Done():
|
||
|
break
|
||
|
default:
|
||
|
hs.update()
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// update runs as a goroutine, and updates host stats
|
||
|
func (hs *HostStats) update() {
|
||
|
hs.h.memoryInfo()
|
||
|
hs.h.getStoragePools()
|
||
|
hs.h.getDomainInfo()
|
||
|
}
|