From 9ab359d3cf7a993d93fb49f35dcf9225dc74c756 Mon Sep 17 00:00:00 2001 From: Henry Dollman Date: Sun, 29 Sep 2024 16:36:32 -0400 Subject: [PATCH] add SENSORS env var --- beszel/internal/agent/agent.go | 9 +++++++++ beszel/internal/agent/system.go | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/beszel/internal/agent/agent.go b/beszel/internal/agent/agent.go index 3ab7a57..2fc8b46 100644 --- a/beszel/internal/agent/agent.go +++ b/beszel/internal/agent/agent.go @@ -30,6 +30,7 @@ type Agent struct { dockerClient *http.Client // HTTP client to query docker api apiContainerList *[]container.ApiInfo // List of containers from docker host sensorsContext context.Context // Sensors context to override sys location + sensorsWhitelist map[string]struct{} // List of sensors to monitor } func NewAgent() *Agent { @@ -64,6 +65,14 @@ func (a *Agent) Run(pubKey []byte, addr string) { ) } + // Set sensors whitelist + if sensors, exists := os.LookupEnv("SENSORS"); exists { + a.sensorsWhitelist = make(map[string]struct{}) + for _, sensor := range strings.Split(sensors, ",") { + a.sensorsWhitelist[sensor] = struct{}{} + } + } + a.initializeSystemInfo() a.initializeDiskInfo() a.initializeNetIoStats() diff --git a/beszel/internal/agent/system.go b/beszel/internal/agent/system.go index ec8777b..ba23c09 100644 --- a/beszel/internal/agent/system.go +++ b/beszel/internal/agent/system.go @@ -162,6 +162,15 @@ func (a *Agent) getSystemStats() (system.Info, system.Stats) { systemStats.Temperatures[sensor.SensorKey] = twoDecimals(sensor.Temperature) } } + // remove sensors from systemStats if whitelist exists and sensor is not in whitelist + // (do this here instead of in initial loop so we have correct keys if int was appended) + if a.sensorsWhitelist != nil { + for key := range systemStats.Temperatures { + if _, nameInWhitelist := a.sensorsWhitelist[key]; !nameInWhitelist { + delete(systemStats.Temperatures, key) + } + } + } } systemInfo := system.Info{