Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
Add agent ID file persistence
rcourtmana8428da1ea7d
diff --git a/cmd/pulse-agent/main.go b/cmd/pulse-agent/main.goindex a7861bf1dc..d2dc1e5ca0 100644--- a/cmd/pulse-agent/main.go+++ b/cmd/pulse-agent/main.go@@ -2,8 +2,10 @@ package mainimport ("context"+ "errors""flag""fmt"+ "io/fs""math""net/http""os"@@ -142,6 +144,20 @@ func run(ctx context.Context, args []string, getenv func(string) string) error {// 2b. Compute Agent ID if missing (needed for remote config)// We replicate the logic from hostagent.New to ensure we get the same IDlookupHostname := strings.TrimSpace(cfg.HostnameOverride)+ if cfg.AgentID == "" && cfg.AgentIDFile != "" {+ if persisted, err := readAgentIDFile(cfg.AgentIDFile); err == nil && persisted != "" {+ cfg.AgentID = persisted+ logger.Info().+ Str("path", cfg.AgentIDFile).+ Str("agentID", cfg.AgentID).+ Msg("Loaded persisted agent ID")+ } else if err != nil && !errors.Is(err, fs.ErrNotExist) {+ logger.Warn().+ Err(err).+ Str("path", cfg.AgentIDFile).+ Msg("Failed to read agent-id-file; will fall back to machine-id and rewrite the file")+ }+ }if cfg.AgentID == "" {// Use a short timeout for host infohCtx, hCancel := context.WithTimeout(ctx, 5*time.Second)@@ -166,6 +182,14 @@ func run(ctx context.Context, args []string, getenv func(string) string) error {Msg("Failed to fetch host info for Agent ID generation")}}+ if cfg.AgentID != "" && cfg.AgentIDFile != "" {+ if err := writeAgentIDFile(cfg.AgentIDFile, cfg.AgentID); err != nil {+ logger.Warn().+ Err(err).+ Str("path", cfg.AgentIDFile).+ Msg("Failed to persist agent ID; ID will be re-derived on next start")+ }+ }if lookupHostname == "" {lookupHostname = strings.TrimSpace(cfg.HostnameOverride)if lookupHostname == "" {@@ -459,6 +483,59 @@ func run(ctx context.Context, args []string, getenv func(string) string) error {return nil}+// readAgentIDFile reads a persisted agent identifier from the given path.+func readAgentIDFile(path string) (string, error) {+ if path == "" {+ return "", nil+ }+ data, err := os.ReadFile(path)+ if err != nil {+ return "", err+ }+ return strings.TrimSpace(string(data)), nil+}++// writeAgentIDFile persists the agent identifier atomically for future starts.+func writeAgentIDFile(path, id string) error {+ if path == "" || id == "" {+ return nil+ }++ dir := filepath.Dir(path)+ if dir != "" && dir != "." {+ if err := os.MkdirAll(dir, 0o700); err != nil {+ return fmt.Errorf("create agent-id-file directory: %w", err)+ }+ }++ tmp, err := os.CreateTemp(dir, ".agent-id-*")+ if err != nil {+ return fmt.Errorf("create agent-id-file temp: %w", err)+ }+ tmpPath := tmp.Name()+ cleanup := func() { _ = os.Remove(tmpPath) }++ if _, err := tmp.WriteString(id + "\n"); err != nil {+ _ = tmp.Close()+ cleanup()+ return fmt.Errorf("write agent-id-file: %w", err)+ }+ if err := tmp.Chmod(0o600); err != nil {+ _ = tmp.Close()+ cleanup()+ return fmt.Errorf("chmod agent-id-file: %w", err)+ }+ if err := tmp.Close(); err != nil {+ cleanup()+ return fmt.Errorf("close agent-id-file: %w", err)+ }+ if err := os.Rename(tmpPath, path); err != nil {+ cleanup()+ return fmt.Errorf("rename agent-id-file: %w", err)+ }+ return nil+}+func runRemoteConfigLoop(ctx context.Context, client *remoteconfig.Client, appliers []RemoteConfigApplier, logger *zerolog.Logger) {if client == nil || len(appliers) == 0 {return@@ -580,6 +657,7 @@ type Config struct {Interval time.DurationHostnameOverride stringAgentID string+ AgentIDFile stringTags []stringInsecureSkipVerify boolCACertPath string@@ -638,6 +716,7 @@ func loadConfig(args []string, getenv func(string) string) (Config, error) {envInterval := strings.TrimSpace(getenv("PULSE_INTERVAL"))envHostname := strings.TrimSpace(getenv("PULSE_HOSTNAME"))envAgentID := strings.TrimSpace(getenv("PULSE_AGENT_ID"))+ envAgentIDFile := strings.TrimSpace(getenv("PULSE_AGENT_ID_FILE"))envInsecure := strings.TrimSpace(getenv("PULSE_INSECURE_SKIP_VERIFY"))envCACertPath := strings.TrimSpace(getenv("PULSE_CACERT"))envServerFingerprint := strings.TrimSpace(getenv("PULSE_SERVER_FINGERPRINT"))@@ -712,6 +791,7 @@ func loadConfig(args []string, getenv func(string) string) (Config, error) {intervalFlag := fs.Duration("interval", defaultInterval, "Reporting interval")hostnameFlag := fs.String("hostname", envHostname, "Override hostname")agentIDFlag := fs.String("agent-id", envAgentID, "Override agent identifier")+ agentIDFileFlag := fs.String("agent-id-file", envAgentIDFile, "Path to a file storing the agent identifier (read on start, written on first start). Mount this file as a volume to keep the agent identity stable across container recreation.")insecureFlag := fs.Bool("insecure", utils.ParseBool(envInsecure), "Skip TLS verification")caCertFlag := fs.String("cacert", envCACertPath, "Path to custom CA bundle for agent HTTPS transport")serverFingerprintFlag := fs.String("server-fingerprint", envServerFingerprint, "Expected Pulse server TLS certificate fingerprint (SHA256)")@@ -831,6 +911,7 @@ func loadConfig(args []string, getenv func(string) string) (Config, error) {Interval: interval,HostnameOverride: strings.TrimSpace(*hostnameFlag),AgentID: strings.TrimSpace(*agentIDFlag),+ AgentIDFile: strings.TrimSpace(*agentIDFileFlag),Tags: tags,InsecureSkipVerify: *insecureFlag,CACertPath: strings.TrimSpace(*caCertFlag),diff --git a/cmd/pulse-agent/main_test.go b/cmd/pulse-agent/main_test.goindex b9463bc948..f67a29366d 100644--- a/cmd/pulse-agent/main_test.go+++ b/cmd/pulse-agent/main_test.go@@ -5,9 +5,11 @@ import ("errors""flag""io"+ "io/fs""net/http""net/http/httptest""os"+ "path/filepath""reflect""strings""sync/atomic"@@ -1879,3 +1881,78 @@ func TestRemoteDurationSettingPreservesFractionalSeconds(t *testing.T) {t.Fatalf("remoteDurationSetting() = %s, want 250ms", got)}}++func TestAgentIDFilePersistence(t *testing.T) {+ t.Run("read returns empty when path is empty", func(t *testing.T) {+ id, err := readAgentIDFile("")+ if err != nil {+ t.Fatalf("expected no error for empty path, got %v", err)+ }+ if id != "" {+ t.Errorf("expected empty id, got %q", id)+ }+ })++ t.Run("read returns fs.ErrNotExist for missing file", func(t *testing.T) {+ dir := t.TempDir()+ _, err := readAgentIDFile(filepath.Join(dir, "missing"))+ if !errors.Is(err, fs.ErrNotExist) {+ t.Fatalf("expected fs.ErrNotExist, got %v", err)+ }+ })++ t.Run("write then read round-trips the ID", func(t *testing.T) {+ dir := t.TempDir()+ path := filepath.Join(dir, "subdir", "agent-id")+ const id = "1234abcd-5678-90ef-1234-567890abcdef"++ if err := writeAgentIDFile(path, id); err != nil {+ t.Fatalf("write failed: %v", err)+ }++ got, err := readAgentIDFile(path)+ if err != nil {+ t.Fatalf("read failed: %v", err)+ }+ if got != id {+ t.Errorf("round-trip mismatch: got %q, want %q", got, id)+ }++ info, err := os.Stat(path)+ if err != nil {+ t.Fatalf("stat: %v", err)+ }+ if mode := info.Mode().Perm(); mode != 0o600 {+ t.Errorf("file permissions = %v, want 0600", mode)+ }+ })++ t.Run("read trims whitespace", func(t *testing.T) {+ dir := t.TempDir()+ path := filepath.Join(dir, "agent-id")+ if err := os.WriteFile(path, []byte(" abc-123 \n\n"), 0o600); err != nil {+ t.Fatalf("seed file: %v", err)+ }+ got, err := readAgentIDFile(path)+ if err != nil {+ t.Fatalf("read: %v", err)+ }+ if got != "abc-123" {+ t.Errorf("expected trimmed value, got %q", got)+ }+ })++ t.Run("write is a no-op when path is empty or id is empty", func(t *testing.T) {+ if err := writeAgentIDFile("", "some-id"); err != nil {+ t.Errorf("expected no-op for empty path, got %v", err)+ }+ dir := t.TempDir()+ path := filepath.Join(dir, "agent-id")+ if err := writeAgentIDFile(path, ""); err != nil {+ t.Errorf("expected no-op for empty id, got %v", err)+ }+ if _, err := os.Stat(path); !errors.Is(err, fs.ErrNotExist) {+ t.Errorf("expected file not to be created, got err=%v", err)+ }+ })+}