start adding hoststats

This commit is contained in:
Matthew Stobbs 2024-04-21 11:50:26 -06:00
parent 4b9782bb2a
commit 325d87ba27
4 changed files with 87 additions and 31 deletions

1
go.mod
View File

@ -14,6 +14,7 @@ require (
require ( require (
github.com/go-chi/chi/v5 v5.0.12 // indirect github.com/go-chi/chi/v5 v5.0.12 // indirect
github.com/gofrs/uuid/v5 v5.1.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect

2
go.sum
View File

@ -7,6 +7,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s= github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s=
github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gofrs/uuid/v5 v5.1.0 h1:S5rqVKIigghZTCBKPCw0Y+bXkn26K3TB5mvQq2Ix8dk=
github.com/gofrs/uuid/v5 v5.1.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=

45
lib/host/stats.go Normal file
View File

@ -0,0 +1,45 @@
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()
}

View File

@ -1,46 +1,54 @@
// Package storagepool provides utilities and data structures in relation to storage pools.
// This library uses the libvirt and libvirtxml packages from libvirt.org to implement
// wrappers on top of the base library
package storagepool package storagepool
import "libvirt.org/go/libvirt" import (
// log "git.staur.ca/stobbsm/simplelog"
// "github.com/gofrs/uuid/v5"
"libvirt.org/go/libvirt"
// "libvirt.org/go/libvirtxml"
)
var StoragePoolStateMap = map[libvirt.StoragePoolState]string{ var StoragePoolStateMap = map[libvirt.StoragePoolState]string{
libvirt.STORAGE_POOL_INACTIVE: "inactive", libvirt.STORAGE_POOL_INACTIVE: "inactive",
libvirt.STORAGE_POOL_BUILDING: "building", libvirt.STORAGE_POOL_BUILDING: "building",
libvirt.STORAGE_POOL_RUNNING: "running", libvirt.STORAGE_POOL_RUNNING: "running",
libvirt.STORAGE_POOL_DEGRADED: "degraded", libvirt.STORAGE_POOL_DEGRADED: "degraded",
libvirt.STORAGE_POOL_INACCESSIBLE: "inaccessible", libvirt.STORAGE_POOL_INACCESSIBLE: "inaccessible",
} }
var Types = []string{ var Types = []string{
"dir", "dir",
"fs", "fs",
"netfs", "netfs",
"disk", "disk",
"iscsi", "iscsi",
"logical", "logical",
"scsi", "scsi",
"mpath", "mpath",
"rbd", "rbd",
"sheepdog", "sheepdog",
"gluster", "gluster",
"zfs", "zfs",
"iscsi-direct", "iscsi-direct",
} }
var LocalTypes = []string{ var LocalTypes = []string{
"dir", "dir",
"fs", "fs",
"disk", "disk",
"logical", "logical",
"scsi", "scsi",
"zfs", "zfs",
} }
var NetTypes = []string{ var NetTypes = []string{
"netfs", "netfs",
"iscsi", "iscsi",
"mpath", "mpath",
"rbd", "rbd",
"sheepdog", "sheepdog",
"gluster", "gluster",
"iscsi-direct", "iscsi-direct",
} }