mirror of
https://github.com/henrygd/beszel.git
synced 2025-12-17 10:46:16 +01:00
144 lines
4.0 KiB
Go
144 lines
4.0 KiB
Go
package agent
|
|
|
|
import (
|
|
"beszel/internal/entities/system"
|
|
"context"
|
|
"log/slog"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/shirou/gopsutil/v4/common"
|
|
"github.com/shirou/gopsutil/v4/sensors"
|
|
)
|
|
|
|
type SensorConfig struct {
|
|
context context.Context
|
|
sensors map[string]struct{}
|
|
primarySensor string
|
|
isBlacklist bool
|
|
hasWildcards bool
|
|
skipCollection bool
|
|
}
|
|
|
|
func (a *Agent) newSensorConfig() *SensorConfig {
|
|
primarySensor, _ := GetEnv("PRIMARY_SENSOR")
|
|
sysSensors, _ := GetEnv("SYS_SENSORS")
|
|
sensorsEnvVal, sensorsSet := GetEnv("SENSORS")
|
|
skipCollection := sensorsSet && sensorsEnvVal == ""
|
|
|
|
return a.newSensorConfigWithEnv(primarySensor, sysSensors, sensorsEnvVal, skipCollection)
|
|
}
|
|
|
|
// newSensorConfigWithEnv creates a SensorConfig with the provided environment variables
|
|
// sensorsSet indicates if the SENSORS environment variable was explicitly set (even to empty string)
|
|
func (a *Agent) newSensorConfigWithEnv(primarySensor, sysSensors, sensorsEnvVal string, skipCollection bool) *SensorConfig {
|
|
config := &SensorConfig{
|
|
context: context.Background(),
|
|
primarySensor: primarySensor,
|
|
skipCollection: skipCollection,
|
|
sensors: make(map[string]struct{}),
|
|
}
|
|
|
|
// Set sensors context (allows overriding sys location for sensors)
|
|
if sysSensors != "" {
|
|
slog.Info("SYS_SENSORS", "path", sysSensors)
|
|
config.context = context.WithValue(config.context,
|
|
common.EnvKey, common.EnvMap{common.HostSysEnvKey: sysSensors},
|
|
)
|
|
}
|
|
|
|
// handle blacklist
|
|
if strings.HasPrefix(sensorsEnvVal, "-") {
|
|
config.isBlacklist = true
|
|
sensorsEnvVal = sensorsEnvVal[1:]
|
|
}
|
|
|
|
for sensor := range strings.SplitSeq(sensorsEnvVal, ",") {
|
|
sensor = strings.TrimSpace(sensor)
|
|
if sensor != "" {
|
|
config.sensors[sensor] = struct{}{}
|
|
if strings.Contains(sensor, "*") {
|
|
config.hasWildcards = true
|
|
}
|
|
}
|
|
}
|
|
|
|
return config
|
|
}
|
|
|
|
// updateTemperatures updates the agent with the latest sensor temperatures
|
|
func (a *Agent) updateTemperatures(systemStats *system.Stats) {
|
|
// skip if sensors whitelist is set to empty string
|
|
if a.sensorConfig.skipCollection {
|
|
slog.Debug("Skipping temperature collection")
|
|
return
|
|
}
|
|
|
|
// reset high temp
|
|
a.systemInfo.DashboardTemp = 0
|
|
|
|
// get sensor data
|
|
temps, _ := sensors.TemperaturesWithContext(a.sensorConfig.context)
|
|
slog.Debug("Temperature", "sensors", temps)
|
|
|
|
// return if no sensors
|
|
if len(temps) == 0 {
|
|
return
|
|
}
|
|
|
|
systemStats.Temperatures = make(map[string]float64, len(temps))
|
|
for i, sensor := range temps {
|
|
// skip if temperature is unreasonable
|
|
if sensor.Temperature <= 0 || sensor.Temperature >= 200 {
|
|
continue
|
|
}
|
|
sensorName := sensor.SensorKey
|
|
if _, ok := systemStats.Temperatures[sensorName]; ok {
|
|
// if key already exists, append int to key
|
|
sensorName = sensorName + "_" + strconv.Itoa(i)
|
|
}
|
|
// skip if not in whitelist or blacklist
|
|
if !isValidSensor(sensorName, a.sensorConfig) {
|
|
continue
|
|
}
|
|
// set dashboard temperature
|
|
if a.sensorConfig.primarySensor == "" {
|
|
a.systemInfo.DashboardTemp = max(a.systemInfo.DashboardTemp, sensor.Temperature)
|
|
} else if a.sensorConfig.primarySensor == sensorName {
|
|
a.systemInfo.DashboardTemp = sensor.Temperature
|
|
}
|
|
systemStats.Temperatures[sensorName] = twoDecimals(sensor.Temperature)
|
|
}
|
|
}
|
|
|
|
// isValidSensor checks if a sensor is valid based on the sensor name and the sensor config
|
|
func isValidSensor(sensorName string, config *SensorConfig) bool {
|
|
// if no sensors configured, everything is valid
|
|
if len(config.sensors) == 0 {
|
|
return true
|
|
}
|
|
|
|
// Exact match - return true if whitelist, false if blacklist
|
|
if _, exactMatch := config.sensors[sensorName]; exactMatch {
|
|
return !config.isBlacklist
|
|
}
|
|
|
|
// If no wildcards, return true if blacklist, false if whitelist
|
|
if !config.hasWildcards {
|
|
return config.isBlacklist
|
|
}
|
|
|
|
// Check for wildcard patterns
|
|
for pattern := range config.sensors {
|
|
if !strings.Contains(pattern, "*") {
|
|
continue
|
|
}
|
|
if match, _ := path.Match(pattern, sensorName); match {
|
|
return !config.isBlacklist
|
|
}
|
|
}
|
|
|
|
return config.isBlacklist
|
|
}
|