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. // 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 KEY is not set, try to read the key from the file specified by KEY_FILE.
if len(pubKey) == 0 { if len(pubKey) == 0 {
keyFile, exists := os.LookupEnv("KEY_FILE") keyFile, exists := agent.GetEnv("KEY_FILE")
if !exists { if !exists {
log.Fatal("Must set KEY or KEY_FILE environment variable") log.Fatal("Must set KEY or KEY_FILE environment variable")
} }
@@ -38,7 +39,8 @@ func main() {
} }
addr := ":45876" 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" // allow passing an address in the form of "127.0.0.1:45876"
if !strings.Contains(portEnvVar, ":") { if !strings.Contains(portEnvVar, ":") {
portEnvVar = ":" + portEnvVar portEnvVar = ":" + portEnvVar

View File

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

View File

@@ -3,18 +3,17 @@ package agent
import ( import (
"beszel/internal/entities/system" "beszel/internal/entities/system"
"log/slog" "log/slog"
"time"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"github.com/shirou/gopsutil/v4/disk" "github.com/shirou/gopsutil/v4/disk"
) )
// Sets up the filesystems to monitor for disk usage and I/O. // Sets up the filesystems to monitor for disk usage and I/O.
func (a *Agent) initializeDiskInfo() { func (a *Agent) initializeDiskInfo() {
filesystem := os.Getenv("FILESYSTEM") filesystem, _ := GetEnv("FILESYSTEM")
efPath := "/extra-filesystems" efPath := "/extra-filesystems"
hasRoot := false hasRoot := false
@@ -79,7 +78,7 @@ func (a *Agent) initializeDiskInfo() {
} }
// Add EXTRA_FILESYSTEMS env var values to fsStats // 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, ",") { for _, fs := range strings.Split(extraFilesystems, ",") {
found := false found := false
for _, p := range partitions { 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 // Creates a new http client for Docker or Podman API
func newDockerManager(a *Agent) *dockerManager { func newDockerManager(a *Agent) *dockerManager {
dockerHost, exists := os.LookupEnv("DOCKER_HOST") dockerHost, exists := GetEnv("DOCKER_HOST")
if exists { if exists {
slog.Info("DOCKER_HOST", "host", dockerHost) slog.Info("DOCKER_HOST", "host", dockerHost)
} else { } else {
@@ -242,7 +242,7 @@ func newDockerManager(a *Agent) *dockerManager {
// configurable timeout // configurable timeout
timeout := time.Millisecond * 2100 timeout := time.Millisecond * 2100
if t, set := os.LookupEnv("DOCKER_TIMEOUT"); set { if t, set := GetEnv("DOCKER_TIMEOUT"); set {
timeout, err = time.ParseDuration(t) timeout, err = time.ParseDuration(t)
if err != nil { if err != nil {
slog.Error(err.Error()) slog.Error(err.Error())

View File

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