From 325d87ba274f4f72fc5f6a4dc0895ad5b4904521 Mon Sep 17 00:00:00 2001 From: Matthew Stobbs Date: Sun, 21 Apr 2024 11:50:26 -0600 Subject: [PATCH] start adding hoststats --- go.mod | 1 + go.sum | 2 ++ lib/host/stats.go | 45 +++++++++++++++++++++++++++ lib/storagepool/lib.go | 70 +++++++++++++++++++++++------------------- 4 files changed, 87 insertions(+), 31 deletions(-) create mode 100644 lib/host/stats.go diff --git a/go.mod b/go.mod index a286945..50ba667 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( require ( 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/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect diff --git a/go.sum b/go.sum index 5a8b8aa..2a0b075 100644 --- a/go.sum +++ b/go.sum @@ -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/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= 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/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= diff --git a/lib/host/stats.go b/lib/host/stats.go new file mode 100644 index 0000000..42d2620 --- /dev/null +++ b/lib/host/stats.go @@ -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() +} diff --git a/lib/storagepool/lib.go b/lib/storagepool/lib.go index 0ab9992..25932db 100644 --- a/lib/storagepool/lib.go +++ b/lib/storagepool/lib.go @@ -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 -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{ - libvirt.STORAGE_POOL_INACTIVE: "inactive", - libvirt.STORAGE_POOL_BUILDING: "building", - libvirt.STORAGE_POOL_RUNNING: "running", - libvirt.STORAGE_POOL_DEGRADED: "degraded", + libvirt.STORAGE_POOL_INACTIVE: "inactive", + libvirt.STORAGE_POOL_BUILDING: "building", + libvirt.STORAGE_POOL_RUNNING: "running", + libvirt.STORAGE_POOL_DEGRADED: "degraded", libvirt.STORAGE_POOL_INACCESSIBLE: "inaccessible", } var Types = []string{ - "dir", - "fs", - "netfs", - "disk", - "iscsi", - "logical", - "scsi", - "mpath", - "rbd", - "sheepdog", - "gluster", - "zfs", - "iscsi-direct", + "dir", + "fs", + "netfs", + "disk", + "iscsi", + "logical", + "scsi", + "mpath", + "rbd", + "sheepdog", + "gluster", + "zfs", + "iscsi-direct", } var LocalTypes = []string{ - "dir", - "fs", - "disk", - "logical", - "scsi", - "zfs", + "dir", + "fs", + "disk", + "logical", + "scsi", + "zfs", } var NetTypes = []string{ - "netfs", - "iscsi", - "mpath", - "rbd", - "sheepdog", - "gluster", - "iscsi-direct", + "netfs", + "iscsi", + "mpath", + "rbd", + "sheepdog", + "gluster", + "iscsi-direct", }