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
fix: Only log 'Migration complete' when inline allowed_nodes actually migrated. Related to Discussion #946
rcourtman277aca3e4e87
diff --git a/cmd/pulse-sensor-proxy/config_cmd.go b/cmd/pulse-sensor-proxy/config_cmd.goindex 1f39c911fd..d90bf1e8fd 100644--- a/cmd/pulse-sensor-proxy/config_cmd.go+++ b/cmd/pulse-sensor-proxy/config_cmd.go@@ -140,12 +140,16 @@ Safe to run multiple times (idempotent).allowedNodesPath = filepath.Join(filepath.Dir(cfgPath), "allowed_nodes.yaml")}- if err := migrateInlineToFile(cfgPath, allowedNodesPath); err != nil {+ migrated, err := migrateInlineToFile(cfgPath, allowedNodesPath)+ if err != nil {fmt.Fprintf(os.Stderr, "Migration failed: %v\n", err)return err}- fmt.Println("Migration complete: inline allowed_nodes moved to file")+ if migrated {+ fmt.Println("Migration complete: inline allowed_nodes moved to file")+ }+ // Silent success when nothing to migrate (idempotent)return nil},}@@ -539,13 +543,16 @@ func extractNodesFromYAML(data []byte) []string {}// migrateInlineToFile atomically migrates inline allowed_nodes from config.yaml to allowed_nodes.yaml-func migrateInlineToFile(configPath, allowedNodesPath string) error {+// Returns (true, nil) if migration was performed, (false, nil) if nothing to migrate, or (false, err) on error.+func migrateInlineToFile(configPath, allowedNodesPath string) (bool, error) {configLockPath := configPath + ".lock"allowedNodesLockPath := allowedNodesPath + ".lock"+ var migrated bool+// Lock both files in consistent order to prevent deadlocks// Always lock config.yaml before allowed_nodes.yaml- return withLockedFile(configLockPath, func(configLock *os.File) error {+ err := withLockedFile(configLockPath, func(configLock *os.File) error {return withLockedFile(allowedNodesLockPath, func(allowedNodesLock *os.File) error {// Read current configconfigData, err := os.ReadFile(configPath)@@ -562,6 +569,15 @@ func migrateInlineToFile(configPath, allowedNodesPath string) error {return fmt.Errorf("failed to parse config: %w", err)}+ // Check if inline allowed_nodes exists - this determines if migration is needed+ _, hasInlineNodes := config["allowed_nodes"]+ _, hasFileRef := config["allowed_nodes_file"]++ // If already using file mode and no inline nodes, nothing to migrate+ if !hasInlineNodes && hasFileRef {+ return nil+ }+// Extract inline nodes (if any)var inlineNodes []stringif allowedNodes, ok := config["allowed_nodes"]; ok {@@ -574,6 +590,9 @@ func migrateInlineToFile(configPath, allowedNodesPath string) error {}}+ // Mark that migration is being performed+ migrated = hasInlineNodes+// Remove inline allowed_nodes block from configdelete(config, "allowed_nodes")@@ -616,4 +635,5 @@ func migrateInlineToFile(configPath, allowedNodesPath string) error {return atomicWriteFile(allowedNodesPath, finalData, 0644)})})+ return migrated, err}diff --git a/internal/ai/alert_triggered_test.go b/internal/ai/alert_triggered_test.goindex c01b9c2886..22124bad65 100644--- a/internal/ai/alert_triggered_test.go+++ b/internal/ai/alert_triggered_test.go@@ -72,6 +72,38 @@ func TestAlertTriggeredAnalyzer_AnalyzeNodeFromAlert(t *testing.T) {}}+func TestAlertTriggeredAnalyzer_AnalyzeNodeFromAlert_ByNodeField(t *testing.T) {+ stateProvider := &mockStateProvider{+ state: models.StateSnapshot{+ Nodes: []models.Node{+ {ID: "node/pve1", Name: "pve1", Status: "online", CPU: 0.95},+ },+ },+ }+ patrolService := &PatrolService{thresholds: DefaultPatrolThresholds()}+ analyzer := NewAlertTriggeredAnalyzer(patrolService, stateProvider)++ alert := &alerts.Alert{+ ID: "alert-1",+ Type: "node_cpu",+ ResourceID: "node/other",+ Node: "pve1",+ }++ findings := analyzer.analyzeNodeFromAlert(context.Background(), alert)+ if len(findings) == 0 {+ t.Error("Expected findings when node matches alert.Node")+ }+}++func TestAlertTriggeredAnalyzer_AnalyzeNodeFromAlert_NoState(t *testing.T) {+ analyzer := NewAlertTriggeredAnalyzer(&PatrolService{}, nil)+ alert := &alerts.Alert{ID: "alert-1", ResourceID: "node/pve1"}+ if findings := analyzer.analyzeNodeFromAlert(context.Background(), alert); findings != nil {+ t.Errorf("Expected nil findings with no state provider, got %v", findings)+ }+}+func TestAlertTriggeredAnalyzer_AnalyzeGuestFromAlert(t *testing.T) {stateProvider := &mockStateProvider{state: models.StateSnapshot{@@ -149,6 +181,44 @@ func TestAlertTriggeredAnalyzer_AnalyzeGuestFromAlert(t *testing.T) {}}+func TestAlertTriggeredAnalyzer_AnalyzeGuestFromAlert_NoState(t *testing.T) {+ analyzer := NewAlertTriggeredAnalyzer(&PatrolService{}, nil)+ alert := &alerts.Alert{ID: "alert-1", ResourceID: "qemu/100"}+ if findings := analyzer.analyzeGuestFromAlert(context.Background(), alert); findings != nil {+ t.Errorf("Expected nil findings with no state provider, got %v", findings)+ }+}++func TestAlertTriggeredAnalyzer_AnalyzeGuestFromAlert_ByName(t *testing.T) {+ stateProvider := &mockStateProvider{+ state: models.StateSnapshot{+ VMs: []models.VM{+ {+ ID: "qemu/100",+ Name: "vm-name",+ Node: "pve1",+ Status: "running",+ CPU: 0.9,+ Memory: models.Memory{Usage: 95.0},+ Disk: models.Disk{Usage: 90.0},+ },+ },+ },+ }+ patrolService := &PatrolService{thresholds: DefaultPatrolThresholds()}+ analyzer := NewAlertTriggeredAnalyzer(patrolService, stateProvider)++ alert := &alerts.Alert{+ ID: "vm-alert",+ Type: "vm_memory",+ ResourceName: "vm-name",+ }+ findings := analyzer.analyzeGuestFromAlert(context.Background(), alert)+ if len(findings) == 0 {+ t.Error("Expected findings when guest matches ResourceName")+ }+}+func TestAlertTriggeredAnalyzer_AnalyzeDockerFromAlert(t *testing.T) {stateProvider := &mockStateProvider{state: models.StateSnapshot{@@ -219,6 +289,14 @@ func TestAlertTriggeredAnalyzer_AnalyzeDockerFromAlert(t *testing.T) {}}+func TestAlertTriggeredAnalyzer_AnalyzeDockerFromAlert_NoState(t *testing.T) {+ analyzer := NewAlertTriggeredAnalyzer(&PatrolService{}, nil)+ alert := &alerts.Alert{ID: "alert-1", ResourceID: "docker-host"}+ if findings := analyzer.analyzeDockerFromAlert(context.Background(), alert); findings != nil {+ t.Errorf("Expected nil findings with no state provider, got %v", findings)+ }+}+func TestAlertTriggeredAnalyzer_AnalyzeStorageFromAlert(t *testing.T) {stateProvider := &mockStateProvider{state: models.StateSnapshot{@@ -264,6 +342,14 @@ func TestAlertTriggeredAnalyzer_AnalyzeStorageFromAlert(t *testing.T) {}}+func TestAlertTriggeredAnalyzer_AnalyzeStorageFromAlert_NoState(t *testing.T) {+ analyzer := NewAlertTriggeredAnalyzer(&PatrolService{}, nil)+ alert := &alerts.Alert{ID: "alert-1", ResourceID: "storage-1"}+ if findings := analyzer.analyzeStorageFromAlert(context.Background(), alert); findings != nil {+ t.Errorf("Expected nil findings with no state provider, got %v", findings)+ }+}+func TestAlertTriggeredAnalyzer_AnalyzeGenericResourceFromAlert(t *testing.T) {stateProvider := &mockStateProvider{state: models.StateSnapshot{@@ -689,6 +775,18 @@ func TestAlertTriggeredAnalyzer_AnalyzeResourceByAlert(t *testing.T) {t.Error("Expected findings for container/VM alert, got 0")}+ // Test generic disk alert routing+ alertDisk := &alerts.Alert{+ ID: "disk-alert",+ Type: "disk",+ ResourceID: "test-vm",+ ResourceName: "test-vm",+ }+ findingsDisk := analyzer.analyzeResourceByAlert(context.Background(), alertDisk)+ if len(findingsDisk) == 0 {+ t.Error("Expected findings for generic disk alert, got 0")+ }+// Test unknown alert typealertUnknown := &alerts.Alert{ID: "unknown-alert",@@ -699,6 +797,48 @@ func TestAlertTriggeredAnalyzer_AnalyzeResourceByAlert(t *testing.T) {t.Errorf("Expected 0 findings for unknown alert type, got %d", len(findingsUnknown))}+ // Test Docker alert type+ stateProvider.state.DockerHosts = []models.DockerHost{+ {ID: "dh-1", Hostname: "docker-host", Status: "offline"},+ }+ alertDocker := &alerts.Alert{+ ID: "docker-alert",+ Type: "docker_offline",+ ResourceID: "dh-1",+ ResourceName: "docker-host",+ }+ findingsDocker := analyzer.analyzeResourceByAlert(context.Background(), alertDocker)+ if len(findingsDocker) == 0 {+ t.Error("Expected findings for docker alert, got 0")+ }++ // Test storage alert type+ stateProvider.state.Storage = []models.Storage{+ {ID: "storage-1", Name: "local", Usage: 95.0, Total: 100, Used: 95},+ }+ alertStorage := &alerts.Alert{+ ID: "storage-alert",+ Type: "storage_usage",+ ResourceID: "storage-1",+ ResourceName: "local",+ }+ findingsStorage := analyzer.analyzeResourceByAlert(context.Background(), alertStorage)+ if len(findingsStorage) == 0 {+ t.Error("Expected findings for storage alert, got 0")+ }++ // Test cpu alert with node resource ID+ alertNodeCPU := &alerts.Alert{+ ID: "node-cpu",+ Type: "cpu",+ ResourceID: "cluster/node/pve1",+ ResourceName: "pve1",+ }+ findingsNodeCPU := analyzer.analyzeResourceByAlert(context.Background(), alertNodeCPU)+ if len(findingsNodeCPU) == 0 {+ t.Error("Expected findings for node cpu alert, got 0")+ }+// Test with nil patrol serviceanalyzerNoPatrol := NewAlertTriggeredAnalyzer(nil, stateProvider)findingsNilPatrol := analyzerNoPatrol.analyzeResourceByAlert(context.Background(), alertNode)@@ -707,6 +847,92 @@ func TestAlertTriggeredAnalyzer_AnalyzeResourceByAlert(t *testing.T) {}}+func TestAlertTriggeredAnalyzer_AnalyzeResource(t *testing.T) {+ stateProvider := &mockStateProvider{+ state: models.StateSnapshot{+ Nodes: []models.Node{+ {ID: "node/pve1", Name: "pve1", Status: "online", CPU: 0.95},+ },+ },+ }+ patrolService := NewPatrolService(nil, nil)+ patrolService.aiService = &Service{}++ analyzer := NewAlertTriggeredAnalyzer(patrolService, stateProvider)++ alert := &alerts.Alert{+ ID: "alert-1",+ Type: "node_cpu",+ ResourceID: "node/pve1",+ ResourceName: "pve1",+ }+ analyzer.analyzeResource(alert, "node/pve1")++ if analyzer.pending["node/pve1"] {+ t.Error("Expected pending to be cleared after analysis")+ }+ if _, exists := analyzer.lastAnalyzed["node/pve1"]; !exists {+ t.Error("Expected lastAnalyzed to be updated after analysis")+ }+ if len(patrolService.findings.GetAll(nil)) == 0 {+ t.Error("Expected findings to be added after analysis")+ }+ for _, finding := range patrolService.findings.GetAll(nil) {+ if finding.AlertID != "alert-1" {+ t.Errorf("Expected AlertID to be set, got %s", finding.AlertID)+ }+ }+}++func TestAlertTriggeredAnalyzer_AnalyzeResource_NoFindings(t *testing.T) {+ stateProvider := &mockStateProvider{+ state: models.StateSnapshot{+ Nodes: []models.Node{+ {ID: "node/pve1", Name: "pve1", Status: "online", CPU: 0.05},+ },+ },+ }+ patrolService := NewPatrolService(nil, nil)+ patrolService.aiService = &Service{}++ analyzer := NewAlertTriggeredAnalyzer(patrolService, stateProvider)++ alert := &alerts.Alert{+ ID: "alert-1",+ Type: "node_cpu",+ ResourceID: "node/pve1",+ ResourceName: "pve1",+ }+ analyzer.analyzeResource(alert, "node/pve1")++ if len(patrolService.findings.GetAll(nil)) != 0 {+ t.Error("Expected no findings for healthy resource")+ }+}++func TestAlertTriggeredAnalyzer_AnalyzeGenericResourceFromAlert_FallbackToNode(t *testing.T) {+ stateProvider := &mockStateProvider{+ state: models.StateSnapshot{+ Nodes: []models.Node{+ {ID: "node/pve1", Name: "pve1", Status: "online", CPU: 0.95},+ },+ },+ }+ patrolService := &PatrolService{thresholds: DefaultPatrolThresholds()}+ analyzer := NewAlertTriggeredAnalyzer(patrolService, stateProvider)++ alert := &alerts.Alert{+ ID: "generic-alert",+ Type: "cpu",+ ResourceID: "pve1",+ ResourceName: "pve1",+ }+ findings := analyzer.analyzeGenericResourceFromAlert(context.Background(), alert)+ if len(findings) == 0 {+ t.Error("Expected findings from node fallback")+ }+}+type mockStateProvider struct {state models.StateSnapshot}@@ -785,3 +1011,41 @@ func TestAlertTriggeredAnalyzer_AnalyzeGuestFromAlert_PreservesBackup(t *testingt.Error("Expected 'backup-stale' finding for very old backup, but didn't find it. LastBackup might not be getting passed correctly.")}}++func TestAlertTriggeredAnalyzer_AnalyzeGuestFromAlert_ContainerBackup(t *testing.T) {+ lastBackup := time.Now().Add(-2 * time.Hour)++ stateProvider := &mockStateProvider{+ state: models.StateSnapshot{+ Containers: []models.Container{+ {+ ID: "lxc/200",+ Name: "backup-ct",+ Status: "running",+ CPU: 0.5,+ Memory: models.Memory{Usage: 95.0},+ Disk: models.Disk{Usage: 90.0},+ LastBackup: lastBackup,+ },+ },+ },+ }++ patrolService := &PatrolService{+ thresholds: DefaultPatrolThresholds(),+ }++ analyzer := NewAlertTriggeredAnalyzer(patrolService, stateProvider)++ alertCT := &alerts.Alert{+ ID: "ct-alert",+ Type: "lxc_memory",+ ResourceID: "lxc/200",+ ResourceName: "backup-ct",+ }++ findings := analyzer.analyzeGuestFromAlert(context.Background(), alertCT)+ if len(findings) == 0 {+ t.Error("Expected findings for container with backup set, got 0")+ }+}diff --git a/internal/ai/patrol_test.go b/internal/ai/patrol_test.goindex 8e9297deb1..1c02c28d71 100644--- a/internal/ai/patrol_test.go+++ b/internal/ai/patrol_test.go@@ -1,10 +1,12 @@package aiimport (+ "errors""strings""testing""time"+ "github.com/rcourtman/pulse-go-rewrite/internal/ai/memory""github.com/rcourtman/pulse-go-rewrite/internal/models")@@ -1066,6 +1068,118 @@ func TestPatrolService_SetRunHistoryPersistence_Nil(t *testing.T) {}}+type mockFindingsPersistence struct {+ loadErr error+}++func (m *mockFindingsPersistence) SaveFindings(findings map[string]*Finding) error {+ return nil+}++func (m *mockFindingsPersistence) LoadFindings() (map[string]*Finding, error) {+ if m.loadErr != nil {+ return nil, m.loadErr+ }+ return make(map[string]*Finding), nil+}++func TestPatrolService_SetFindingsPersistence(t *testing.T) {+ ps := NewPatrolService(nil, nil)++ err := ps.SetFindingsPersistence(&mockFindingsPersistence{})+ if err != nil {+ t.Errorf("Expected no error with persistence, got: %v", err)+ }+}++func TestPatrolService_SetFindingsPersistence_Error(t *testing.T) {+ ps := NewPatrolService(nil, nil)++ err := ps.SetFindingsPersistence(&mockFindingsPersistence{loadErr: errors.New("load failed")})+ if err == nil {+ t.Error("Expected error when persistence load fails")+ }+}++func TestPatrolService_SetRunHistoryPersistence(t *testing.T) {+ ps := NewPatrolService(nil, nil)+ mockPersistence := &mockPatrolHistoryPersistence{+ runs: []PatrolRunRecord{{ID: "run-1"}},+ }++ err := ps.SetRunHistoryPersistence(mockPersistence)+ if err != nil {+ t.Errorf("Expected no error with persistence, got: %v", err)+ }+}++func TestPatrolService_SetRunHistoryPersistence_Error(t *testing.T) {+ ps := NewPatrolService(nil, nil)+ mockPersistence := &mockPatrolHistoryPersistence{+ loadErr: errors.New("load failed"),+ }++ err := ps.SetRunHistoryPersistence(mockPersistence)+ if err == nil {+ t.Error("Expected error when run history persistence load fails")+ }+}++func TestPatrolService_IncidentStore(t *testing.T) {+ ps := NewPatrolService(nil, nil)+ store := memory.NewIncidentStore(memory.IncidentStoreConfig{})+ ps.SetIncidentStore(store)++ if got := ps.GetIncidentStore(); got != store {+ t.Errorf("Expected incident store to match")+ }+}++func TestPatrolService_GetThresholds(t *testing.T) {+ ps := NewPatrolService(nil, nil)+ thresholds := ps.GetThresholds()+ if thresholds.StorageWarning == 0 {+ t.Errorf("Expected thresholds to be initialized")+ }+}++func TestPatrolService_GetIntelligence(t *testing.T) {+ ps := NewPatrolService(nil, nil)+ ps.stateProvider = &mockStateProvider{}++ intel := ps.GetIntelligence()+ if intel == nil {+ t.Fatal("Expected intelligence facade to be created")+ }+ if intel != ps.GetIntelligence() {+ t.Fatal("Expected GetIntelligence to return cached instance")+ }+}++func TestPatrolService_Stop(t *testing.T) {+ ps := NewPatrolService(nil, nil)++ // Stop should no-op when not running+ ps.Stop()++ ps.running = true+ ps.stopCh = make(chan struct{})+ ps.Stop()++ ps.mu.RLock()+ running := ps.running+ ps.mu.RUnlock()+ if running {+ t.Error("Expected patrol service to be stopped")+ }++ select {+ case <-ps.stopCh:+ default:+ t.Error("Expected stop channel to be closed")+ }+}+func TestPatrolService_SetKnowledgeStore(t *testing.T) {ps := NewPatrolService(nil, nil)