Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

feat: add `SYNC_SYSTEM_NAMES` env var to sync system names with hostname (#1917)

Sven van Ginkel4d10ea2e0367
diff --git a/internal/hub/systems/system.go b/internal/hub/systems/system.go
index a93d69e33..469c1e068 100644
--- a/internal/hub/systems/system.go
+++ b/internal/hub/systems/system.go
@@ -279,6 +279,10 @@ func (sys *System) createRecords(data *system.CombinedData) (*core.Record, error
if err := createSystemDetailsRecord(txApp, data.Details, sys.Id); err != nil {
return err
}
+ // sync display name with hostname if enabled (details are fetched once per agent connection)
+ if syncNames, _ := utils.GetEnv("SYNC_SYSTEM_NAMES"); syncNames == "true" && data.Details.Hostname != "" {
+ systemRecord.Set("name", data.Details.Hostname)
+ }
}
if data.Monitors != nil {
diff --git a/internal/hub/systems/system_sync_name_test.go b/internal/hub/systems/system_sync_name_test.go
new file mode 100644
index 000000000..339b65664
--- /dev/null
+++ b/internal/hub/systems/system_sync_name_test.go
@@ -0,0 +1,34 @@
+//go:build testing
+
+package systems
+
+import (
+ "testing"
+
+ "github.com/henrygd/beszel/internal/entities/system"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestCreateRecordsSyncSystemNames(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ env string
+ hostname string
+ expected string
+ }{
+ {"disabled", "", "new-host", "test-system"},
+ {"enabled", "true", "new-host", "new-host"},
+ {"enabled with empty hostname", "true", "", "test-system"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ t.Setenv("SYNC_SYSTEM_NAMES", tc.env)
+ sys, app := newTestSystemWithHub(t)
+ _, err := sys.createRecords(&system.CombinedData{Details: &system.Details{Hostname: tc.hostname}})
+ require.NoError(t, err)
+ record, err := app.FindRecordById("systems", sys.Id)
+ require.NoError(t, err)
+ assert.Equal(t, tc.expected, record.GetString("name"))
+ })
+ }
+}