config: prefix environment variables with BESZEL_AGENT_ (#502)

This commit is contained in:
Henry Dollman
2025-01-29 20:13:07 -05:00
parent 7170b24160
commit 120aff0d18
5 changed files with 26 additions and 16 deletions

View File

@@ -28,16 +28,26 @@ type Agent struct {
}
func NewAgent() *Agent {
return &Agent{
newAgent := &Agent{
sensorsContext: context.Background(),
memCalc: os.Getenv("MEM_CALC"),
fsStats: make(map[string]*system.FsStats),
}
newAgent.memCalc, _ = GetEnv("MEM_CALC")
return newAgent
}
// GetEnv retrieves an environment variable with a "BESZEL_AGENT_" prefix, or falls back to the unprefixed key.
func GetEnv(key string) (value string, exists bool) {
if value, exists = os.LookupEnv("BESZEL_AGENT_" + key); exists {
return value, exists
}
// Fallback to the old unprefixed key
return os.LookupEnv(key)
}
func (a *Agent) Run(pubKey []byte, addr string) {
// Set up slog with a log level determined by the LOG_LEVEL env var
if logLevelStr, exists := os.LookupEnv("LOG_LEVEL"); exists {
if logLevelStr, exists := GetEnv("LOG_LEVEL"); exists {
switch strings.ToLower(logLevelStr) {
case "debug":
a.debug = true
@@ -52,7 +62,7 @@ func (a *Agent) Run(pubKey []byte, addr string) {
slog.Debug(beszel.Version)
// Set sensors context (allows overriding sys location for sensors)
if sysSensors, exists := os.LookupEnv("SYS_SENSORS"); exists {
if sysSensors, exists := GetEnv("SYS_SENSORS"); exists {
slog.Info("SYS_SENSORS", "path", sysSensors)
a.sensorsContext = context.WithValue(a.sensorsContext,
common.EnvKey, common.EnvMap{common.HostSysEnvKey: sysSensors},
@@ -60,7 +70,7 @@ func (a *Agent) Run(pubKey []byte, addr string) {
}
// Set sensors whitelist
if sensors, exists := os.LookupEnv("SENSORS"); exists {
if sensors, exists := GetEnv("SENSORS"); exists {
a.sensorsWhitelist = make(map[string]struct{})
for _, sensor := range strings.Split(sensors, ",") {
if sensor != "" {