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

@@ -22,11 +22,12 @@ func main() {
}
// Try to get the key from the KEY environment variable.
pubKey := []byte(os.Getenv("KEY"))
key, _ := agent.GetEnv("KEY")
pubKey := []byte(key)
// If KEY is not set, try to read the key from the file specified by KEY_FILE.
if len(pubKey) == 0 {
keyFile, exists := os.LookupEnv("KEY_FILE")
keyFile, exists := agent.GetEnv("KEY_FILE")
if !exists {
log.Fatal("Must set KEY or KEY_FILE environment variable")
}
@@ -38,7 +39,8 @@ func main() {
}
addr := ":45876"
if portEnvVar, exists := os.LookupEnv("PORT"); exists {
// TODO: change env var to ADDR
if portEnvVar, exists := agent.GetEnv("PORT"); exists {
// allow passing an address in the form of "127.0.0.1:45876"
if !strings.Contains(portEnvVar, ":") {
portEnvVar = ":" + portEnvVar

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 != "" {

View File

@@ -3,18 +3,17 @@ package agent
import (
"beszel/internal/entities/system"
"log/slog"
"time"
"os"
"path/filepath"
"strings"
"time"
"github.com/shirou/gopsutil/v4/disk"
)
// Sets up the filesystems to monitor for disk usage and I/O.
func (a *Agent) initializeDiskInfo() {
filesystem := os.Getenv("FILESYSTEM")
filesystem, _ := GetEnv("FILESYSTEM")
efPath := "/extra-filesystems"
hasRoot := false
@@ -79,7 +78,7 @@ func (a *Agent) initializeDiskInfo() {
}
// Add EXTRA_FILESYSTEMS env var values to fsStats
if extraFilesystems, exists := os.LookupEnv("EXTRA_FILESYSTEMS"); exists {
if extraFilesystems, exists := GetEnv("EXTRA_FILESYSTEMS"); exists {
for _, fs := range strings.Split(extraFilesystems, ",") {
found := false
for _, p := range partitions {

View File

@@ -208,7 +208,7 @@ func (dm *dockerManager) deleteContainerStatsSync(id string) {
// Creates a new http client for Docker or Podman API
func newDockerManager(a *Agent) *dockerManager {
dockerHost, exists := os.LookupEnv("DOCKER_HOST")
dockerHost, exists := GetEnv("DOCKER_HOST")
if exists {
slog.Info("DOCKER_HOST", "host", dockerHost)
} else {
@@ -242,7 +242,7 @@ func newDockerManager(a *Agent) *dockerManager {
// configurable timeout
timeout := time.Millisecond * 2100
if t, set := os.LookupEnv("DOCKER_TIMEOUT"); set {
if t, set := GetEnv("DOCKER_TIMEOUT"); set {
timeout, err = time.ParseDuration(t)
if err != nil {
slog.Error(err.Error())

View File

@@ -2,7 +2,6 @@ package agent
import (
"log/slog"
"os"
"strings"
"time"
@@ -15,7 +14,7 @@ func (a *Agent) initializeNetIoStats() {
// map of network interface names passed in via NICS env var
var nicsMap map[string]struct{}
nics, nicsEnvExists := os.LookupEnv("NICS")
nics, nicsEnvExists := GetEnv("NICS")
if nicsEnvExists {
nicsMap = make(map[string]struct{}, 0)
for _, nic := range strings.Split(nics, ",") {