add MEM_CALC env var

This commit is contained in:
Henry Dollman
2024-10-05 15:29:27 -04:00
parent c407fe9af0
commit af4877ca30
2 changed files with 15 additions and 4 deletions

View File

@@ -13,6 +13,7 @@ import (
type Agent struct { type Agent struct {
debug bool // true if LOG_LEVEL is set to debug debug bool // true if LOG_LEVEL is set to debug
memCalc string // Memory calculation formula
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
netInterfaces map[string]struct{} // Stores all valid network interfaces netInterfaces map[string]struct{} // Stores all valid network interfaces
@@ -26,6 +27,7 @@ type Agent struct {
func NewAgent() *Agent { func NewAgent() *Agent {
return &Agent{ return &Agent{
sensorsContext: context.Background(), sensorsContext: context.Background(),
memCalc: os.Getenv("MEM_CALC"),
} }
} }

View File

@@ -52,12 +52,21 @@ func (a *Agent) getSystemStats() system.Stats {
// memory // memory
if v, err := mem.VirtualMemory(); err == nil { if v, err := mem.VirtualMemory(); err == nil {
// cache + buffers value for default mem calculation
cacheBuff := v.Total - v.Free - v.Used
// htop memory calculation overrides
if a.memCalc == "htop" {
// note: gopsutil automatically adds SReclaimable to v.Cached
cacheBuff = v.Cached + v.Buffers - v.Shared
v.Used = v.Total - (v.Free + cacheBuff)
v.UsedPercent = float64(v.Used) / float64(v.Total) * 100.0
}
systemStats.Mem = bytesToGigabytes(v.Total) systemStats.Mem = bytesToGigabytes(v.Total)
systemStats.MemUsed = bytesToGigabytes(v.Used)
systemStats.MemBuffCache = bytesToGigabytes(v.Total - v.Free - v.Used)
systemStats.MemPct = twoDecimals(v.UsedPercent)
systemStats.Swap = bytesToGigabytes(v.SwapTotal) systemStats.Swap = bytesToGigabytes(v.SwapTotal)
systemStats.SwapUsed = bytesToGigabytes(v.SwapTotal - v.SwapFree) systemStats.SwapUsed = bytesToGigabytes(v.SwapTotal - v.SwapFree - v.SwapCached)
systemStats.MemBuffCache = bytesToGigabytes(cacheBuff)
systemStats.MemUsed = bytesToGigabytes(v.Used)
systemStats.MemPct = twoDecimals(v.UsedPercent)
} }
// disk usage // disk usage