move system.Info to Agent struct

* cleaner to store entire info struct rather than separate properties for unchanging values
This commit is contained in:
Henry Dollman
2024-10-02 12:34:42 -04:00
parent 94cb5f2798
commit 45e1283b83
3 changed files with 32 additions and 42 deletions

View File

@@ -15,11 +15,6 @@ import (
) )
type Agent struct { type Agent struct {
hostname string // Hostname of the system
kernelVersion string // Kernel version of the system
cpuModel string // CPU model of the system
cores int // Number of cores of the system
threads int // Number of threads of the system
debug bool // true if LOG_LEVEL is set to debug debug bool // true if LOG_LEVEL is set to debug
fsNames []string // List of filesystem device names being monitored fsNames []string // List of filesystem device names being monitored
fsStats map[string]*system.FsStats // Keeps track of disk stats for each filesystem fsStats map[string]*system.FsStats // Keeps track of disk stats for each filesystem
@@ -31,6 +26,7 @@ type Agent struct {
apiContainerList *[]container.ApiInfo // List of containers from docker host apiContainerList *[]container.ApiInfo // List of containers from docker host
sensorsContext context.Context // Sensors context to override sys location sensorsContext context.Context // Sensors context to override sys location
sensorsWhitelist map[string]struct{} // List of sensors to monitor sensorsWhitelist map[string]struct{} // List of sensors to monitor
systemInfo system.Info // Host system info
} }
func NewAgent() *Agent { func NewAgent() *Agent {
@@ -81,14 +77,15 @@ func (a *Agent) Run(pubKey []byte, addr string) {
} }
func (a *Agent) gatherStats() system.CombinedData { func (a *Agent) gatherStats() system.CombinedData {
systemInfo, SystemStats := a.getSystemStats()
systemData := system.CombinedData{ systemData := system.CombinedData{
Stats: SystemStats, Stats: a.getSystemStats(),
Info: systemInfo, Info: a.systemInfo,
} }
// add docker stats // add docker stats
if containerStats, err := a.getDockerStats(); err == nil { if containerStats, err := a.getDockerStats(); err == nil {
systemData.Containers = containerStats systemData.Containers = containerStats
} else {
slog.Debug("Error getting docker stats", "err", err)
} }
// add extra filesystems // add extra filesystems
systemData.Stats.ExtraFs = make(map[string]*system.FsStats) systemData.Stats.ExtraFs = make(map[string]*system.FsStats)

View File

@@ -18,26 +18,28 @@ import (
// Sets initial / non-changing values about the host system // Sets initial / non-changing values about the host system
func (a *Agent) initializeSystemInfo() { func (a *Agent) initializeSystemInfo() {
a.kernelVersion, _ = host.KernelVersion() a.systemInfo.AgentVersion = beszel.Version
a.hostname, _ = os.Hostname() a.systemInfo.Hostname, _ = os.Hostname()
a.systemInfo.KernelVersion, _ = host.KernelVersion()
// add cpu stats // cpu model
if info, err := cpu.Info(); err == nil && len(info) > 0 { if info, err := cpu.Info(); err == nil && len(info) > 0 {
a.cpuModel = info[0].ModelName a.systemInfo.CpuModel = info[0].ModelName
} }
a.cores, _ = cpu.Counts(false) // cores / threads
a.systemInfo.Cores, _ = cpu.Counts(false)
if threads, err := cpu.Counts(true); err == nil { if threads, err := cpu.Counts(true); err == nil {
if threads > 0 && threads < a.cores { if threads > 0 && threads < a.systemInfo.Cores {
// in lxc logical cores reflects container limits, so use that as cores if lower // in lxc logical cores reflects container limits, so use that as cores if lower
a.cores = threads a.systemInfo.Cores = threads
} else { } else {
a.threads = threads a.systemInfo.Threads = threads
} }
} }
} }
// Returns current info, stats about the host system // Returns current info, stats about the host system
func (a *Agent) getSystemStats() (system.Info, system.Stats) { func (a *Agent) getSystemStats() system.Stats {
systemStats := system.Stats{} systemStats := system.Stats{}
// cpu percent // cpu percent
@@ -173,19 +175,11 @@ func (a *Agent) getSystemStats() (system.Info, system.Stats) {
} }
} }
systemInfo := system.Info{ // update base system info
Cpu: systemStats.Cpu, a.systemInfo.Cpu = systemStats.Cpu
MemPct: systemStats.MemPct, a.systemInfo.MemPct = systemStats.MemPct
DiskPct: systemStats.DiskPct, a.systemInfo.DiskPct = systemStats.DiskPct
AgentVersion: beszel.Version, a.systemInfo.Uptime, _ = host.Uptime()
Hostname: a.hostname,
KernelVersion: a.kernelVersion,
CpuModel: a.cpuModel,
Cores: a.cores,
Threads: a.threads,
}
systemInfo.Uptime, _ = host.Uptime() return systemStats
return systemInfo, systemStats
} }

View File

@@ -49,7 +49,6 @@ type Info struct {
Cores int `json:"c"` Cores int `json:"c"`
Threads int `json:"t,omitempty"` Threads int `json:"t,omitempty"`
CpuModel string `json:"m"` CpuModel string `json:"m"`
// Os string `json:"o"`
Uptime uint64 `json:"u"` Uptime uint64 `json:"u"`
Cpu float64 `json:"cpu"` Cpu float64 `json:"cpu"`
MemPct float64 `json:"mp"` MemPct float64 `json:"mp"`