Skip to content

armgit / Pulse

Public Imported from GitHub
0

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.

Fix patrol scoping and config propagation

rcourtmanff2841a5c6af
diff --git a/internal/ai/patrol.go b/internal/ai/patrol.go
index d331a33ef6..b1db63e351 100644
--- a/internal/ai/patrol.go
+++ b/internal/ai/patrol.go
@@ -1165,9 +1165,8 @@ func (p *PatrolService) runScopedPatrol(ctx context.Context, scope PatrolScope)
// Run AI analysis on filtered state
aiResult, aiErr := p.runAIAnalysis(ctx, filteredState)
if aiErr != nil {
- log.Warn().Err(aiErr).Msg("AI Patrol (scoped): LLM analysis failed")
- } else if aiResult != nil {
- for _, f := range aiResult.Findings {
+ log.Warn().Err(aiErr).Msg("AI Patrol (scoped): LLM analysis failed, running heuristics as safety net")
+ for _, f := range p.runHeuristicAnalysis(filteredState) {
isNew := p.findings.Add(f)
if isNew && (f.Severity == FindingSeverityWarning || f.Severity == FindingSeverityCritical) {
log.Info().
@@ -1176,9 +1175,7 @@ func (p *PatrolService) runScopedPatrol(ctx context.Context, scope PatrolScope)
Str("resource", f.ResourceName).
Str("title", f.Title).
Msg("AI Patrol (scoped): New finding")
- if !(f.Key == "ai-patrol-error" || f.ResourceID == "ai-service") {
- p.generateRemediationPlan(f)
- }
+ p.generateRemediationPlan(f)
}
// Push to unified store if active
if p.unifiedFindingCallback != nil {
@@ -1187,6 +1184,49 @@ func (p *PatrolService) runScopedPatrol(ctx context.Context, scope PatrolScope)
}
}
}
+ } else if aiResult != nil {
+ if len(aiResult.Findings) == 0 {
+ log.Warn().Msg("AI Patrol (scoped): LLM response contained no parseable findings, running heuristics as safety net")
+ for _, f := range p.runHeuristicAnalysis(filteredState) {
+ isNew := p.findings.Add(f)
+ if isNew && (f.Severity == FindingSeverityWarning || f.Severity == FindingSeverityCritical) {
+ log.Info().
+ Str("finding_id", f.ID).
+ Str("severity", string(f.Severity)).
+ Str("resource", f.ResourceName).
+ Str("title", f.Title).
+ Msg("AI Patrol (scoped): New finding")
+ p.generateRemediationPlan(f)
+ }
+ // Push to unified store if active
+ if p.unifiedFindingCallback != nil {
+ if stored := p.findings.Get(f.ID); stored != nil && stored.IsActive() {
+ p.unifiedFindingCallback(stored)
+ }
+ }
+ }
+ } else {
+ for _, f := range aiResult.Findings {
+ isNew := p.findings.Add(f)
+ if isNew && (f.Severity == FindingSeverityWarning || f.Severity == FindingSeverityCritical) {
+ log.Info().
+ Str("finding_id", f.ID).
+ Str("severity", string(f.Severity)).
+ Str("resource", f.ResourceName).
+ Str("title", f.Title).
+ Msg("AI Patrol (scoped): New finding")
+ if !(f.Key == "ai-patrol-error" || f.ResourceID == "ai-service") {
+ p.generateRemediationPlan(f)
+ }
+ }
+ // Push to unified store if active
+ if p.unifiedFindingCallback != nil {
+ if stored := p.findings.Get(f.ID); stored != nil && stored.IsActive() {
+ p.unifiedFindingCallback(stored)
+ }
+ }
+ }
+ }
}
}
@@ -1203,25 +1243,80 @@ func (p *PatrolService) filterStateByScope(state models.StateSnapshot, scope Pat
// Build lookup sets for efficient matching
resourceIDSet := make(map[string]bool)
for _, id := range scope.ResourceIDs {
- resourceIDSet[id] = true
+ trimmed := strings.TrimSpace(id)
+ if trimmed == "" {
+ continue
+ }
+ resourceIDSet[trimmed] = true
}
typeSet := make(map[string]bool)
- for _, t := range scope.ResourceTypes {
- trimmed := strings.TrimSpace(t)
+ addScopeType := func(t string) {
+ trimmed := strings.TrimSpace(strings.ToLower(t))
if trimmed == "" {
- continue
+ return
}
- typeSet[strings.ToLower(trimmed)] = true
+ typeSet[trimmed] = true
+ switch trimmed {
+ case "docker", "docker_host", "docker_container":
+ typeSet["docker"] = true
+ typeSet["docker_host"] = true
+ typeSet["docker_container"] = true
+ case "k8s", "kubernetes", "kubernetes_cluster":
+ typeSet["k8s"] = true
+ typeSet["kubernetes"] = true
+ typeSet["kubernetes_cluster"] = true
+ case "lxc", "container":
+ typeSet["lxc"] = true
+ typeSet["container"] = true
+ case "vm", "qemu":
+ typeSet["vm"] = true
+ typeSet["qemu"] = true
+ case "host", "host_raid", "host_sensor":
+ typeSet["host"] = true
+ typeSet["host_raid"] = true
+ typeSet["host_sensor"] = true
+ case "pbs", "pbs_datastore", "pbs_job":
+ typeSet["pbs"] = true
+ typeSet["pbs_datastore"] = true
+ typeSet["pbs_job"] = true
+ }
+ }
+ for _, t := range scope.ResourceTypes {
+ addScopeType(t)
}
- // Helper to check if a resource matches the scope
- matchesScope := func(id, resourceType string) bool {
- hasIDs := len(resourceIDSet) > 0
- hasTypes := len(typeSet) > 0
- idMatch := !hasIDs || resourceIDSet[id]
- typeMatch := !hasTypes || typeSet[strings.ToLower(resourceType)]
- return idMatch && typeMatch
+ hasIDs := len(resourceIDSet) > 0
+ hasTypes := len(typeSet) > 0
+
+ matchesType := func(candidates ...string) bool {
+ if !hasTypes {
+ return true
+ }
+ for _, candidate := range candidates {
+ if candidate == "" {
+ continue
+ }
+ if typeSet[strings.ToLower(candidate)] {
+ return true
+ }
+ }
+ return false
+ }
+
+ matchesID := func(candidates ...string) bool {
+ if !hasIDs {
+ return true
+ }
+ for _, candidate := range candidates {
+ if candidate == "" {
+ continue
+ }
+ if resourceIDSet[candidate] {
+ return true
+ }
+ }
+ return false
}
filtered := models.StateSnapshot{
@@ -1234,42 +1329,117 @@ func (p *PatrolService) filterStateByScope(state models.StateSnapshot, scope Pat
// Filter each resource type
for _, n := range state.Nodes {
- if matchesScope(n.ID, "node") {
+ if matchesType("node") && matchesID(n.ID, n.Name) {
filtered.Nodes = append(filtered.Nodes, n)
}
}
for _, vm := range state.VMs {
- if matchesScope(vm.ID, "vm") {
+ if matchesType("vm", "qemu") && matchesID(vm.ID, vm.Name) {
filtered.VMs = append(filtered.VMs, vm)
}
}
for _, c := range state.Containers {
- if matchesScope(c.ID, "container") || matchesScope(c.ID, "lxc") {
+ if matchesType("container", "lxc") && matchesID(c.ID, c.Name) {
filtered.Containers = append(filtered.Containers, c)
}
}
for _, d := range state.DockerHosts {
- if matchesScope(d.ID, "docker") {
+ if !matchesType("docker", "docker_host", "docker_container") {
+ continue
+ }
+
+ hostName := d.CustomDisplayName
+ if hostName == "" {
+ hostName = d.DisplayName
+ }
+ if hostName == "" {
+ hostName = d.Hostname
+ }
+
+ hostMatches := matchesID(d.ID, hostName, d.Hostname, d.DisplayName, d.CustomDisplayName)
+ if !hasIDs {
filtered.DockerHosts = append(filtered.DockerHosts, d)
+ continue
+ }
+
+ var matchedContainers []models.DockerContainer
+ for _, c := range d.Containers {
+ if matchesID(c.ID, c.Name) {
+ matchedContainers = append(matchedContainers, c)
+ }
+ }
+
+ if hostMatches {
+ filtered.DockerHosts = append(filtered.DockerHosts, d)
+ continue
+ }
+ if len(matchedContainers) > 0 {
+ hostCopy := d
+ hostCopy.Containers = matchedContainers
+ filtered.DockerHosts = append(filtered.DockerHosts, hostCopy)
}
}
for _, s := range state.Storage {
- if matchesScope(s.ID, "storage") {
+ if matchesType("storage") && matchesID(s.ID, s.Name) {
filtered.Storage = append(filtered.Storage, s)
}
}
for _, pbs := range state.PBSInstances {
- if matchesScope(pbs.ID, "pbs") {
+ if !matchesType("pbs", "pbs_datastore", "pbs_job") {
+ continue
+ }
+
+ pbsName := pbs.Name
+ if pbsName == "" {
+ pbsName = pbs.Host
+ }
+ pbsMatches := matchesID(pbs.ID, pbs.Name, pbsName, pbs.Host)
+ if !hasIDs {
+ filtered.PBSInstances = append(filtered.PBSInstances, pbs)
+ continue
+ }
+ if !pbsMatches {
+ for _, ds := range pbs.Datastores {
+ if matchesID(pbs.ID+":"+ds.Name, ds.Name) {
+ pbsMatches = true
+ break
+ }
+ }
+ }
+ if !pbsMatches {
+ for _, job := range pbs.BackupJobs {
+ if matchesID(pbs.ID+":job:"+job.ID, job.ID) {
+ pbsMatches = true
+ break
+ }
+ }
+ }
+ if !pbsMatches {
+ for _, job := range pbs.VerifyJobs {
+ if matchesID(pbs.ID+":verify:"+job.ID, job.ID) {
+ pbsMatches = true
+ break
+ }
+ }
+ }
+ if pbsMatches {
filtered.PBSInstances = append(filtered.PBSInstances, pbs)
}
}
for _, h := range state.Hosts {
- if matchesScope(h.ID, "host") {
+ if matchesType("host", "host_raid", "host_sensor") && matchesID(h.ID, h.DisplayName, h.Hostname) {
filtered.Hosts = append(filtered.Hosts, h)
}
}
for _, k := range state.KubernetesClusters {
- if matchesScope(k.ID, "kubernetes") || matchesScope(k.ID, "k8s") {
+ clusterName := k.CustomDisplayName
+ if clusterName == "" {
+ clusterName = k.DisplayName
+ }
+ if clusterName == "" {
+ clusterName = k.Name
+ }
+ if matchesType("kubernetes", "k8s", "kubernetes_cluster") && matchesID(k.ID, clusterName) {
filtered.KubernetesClusters = append(filtered.KubernetesClusters, k)
}
}
@@ -1752,8 +1922,15 @@ func (p *PatrolService) runPatrol(ctx context.Context) {
}
} else if aiResult != nil {
runStats.aiAnalysis = aiResult
- for _, f := range aiResult.Findings {
- trackFinding(f)
+ if len(aiResult.Findings) == 0 {
+ log.Warn().Msg("AI Patrol: LLM response contained no parseable findings, running heuristics as safety net")
+ for _, f := range p.runHeuristicAnalysis(state) {
+ trackFinding(f)
+ }
+ } else {
+ for _, f := range aiResult.Findings {
+ trackFinding(f)
+ }
}
}
}
diff --git a/internal/ai/patrol_test.go b/internal/ai/patrol_test.go
index e9c08823b2..55b297ce73 100644
--- a/internal/ai/patrol_test.go
+++ b/internal/ai/patrol_test.go
@@ -238,6 +238,88 @@ func TestPatrolService_GetStatus(t *testing.T) {
}
}
+func TestPatrolService_filterStateByScope_DockerContainer(t *testing.T) {
+ ps := NewPatrolService(nil, nil)
+ state := models.StateSnapshot{
+ DockerHosts: []models.DockerHost{
+ {
+ ID: "host-1",
+ Hostname: "docker-1",
+ Containers: []models.DockerContainer{
+ {ID: "c1", Name: "web"},
+ {ID: "c2", Name: "db"},
+ },
+ },
+ },
+ }
+ scope := PatrolScope{
+ ResourceIDs: []string{"c1"},
+ ResourceTypes: []string{"docker_container"},
+ }
+
+ filtered := ps.filterStateByScope(state, scope)
+
+ if len(filtered.DockerHosts) != 1 {
+ t.Fatalf("expected 1 docker host, got %d", len(filtered.DockerHosts))
+ }
+ if len(filtered.DockerHosts[0].Containers) != 1 {
+ t.Fatalf("expected 1 container, got %d", len(filtered.DockerHosts[0].Containers))
+ }
+ if filtered.DockerHosts[0].Containers[0].ID != "c1" {
+ t.Fatalf("expected container c1, got %s", filtered.DockerHosts[0].Containers[0].ID)
+ }
+}
+
+func TestPatrolService_filterStateByScope_KubernetesClusterType(t *testing.T) {
+ ps := NewPatrolService(nil, nil)
+ state := models.StateSnapshot{
+ KubernetesClusters: []models.KubernetesCluster{
+ {ID: "k1", Name: "cluster-1"},
+ },
+ }
+ scope := PatrolScope{
+ ResourceIDs: []string{"k1"},
+ ResourceTypes: []string{"kubernetes_cluster"},
+ }
+
+ filtered := ps.filterStateByScope(state, scope)
+
+ if len(filtered.KubernetesClusters) != 1 {
+ t.Fatalf("expected 1 kubernetes cluster, got %d", len(filtered.KubernetesClusters))
+ }
+ if filtered.KubernetesClusters[0].ID != "k1" {
+ t.Fatalf("expected cluster k1, got %s", filtered.KubernetesClusters[0].ID)
+ }
+}
+
+func TestPatrolService_filterStateByScope_PBSDatastoreType(t *testing.T) {
+ ps := NewPatrolService(nil, nil)
+ state := models.StateSnapshot{
+ PBSInstances: []models.PBSInstance{
+ {
+ ID: "pbs1",
+ Name: "pbs-main",
+ Datastores: []models.PBSDatastore{
+ {Name: "ds1"},
+ },
+ },
+ },
+ }
+ scope := PatrolScope{
+ ResourceIDs: []string{"pbs1:ds1"},
+ ResourceTypes: []string{"pbs_datastore"},
+ }
+
+ filtered := ps.filterStateByScope(state, scope)
+
+ if len(filtered.PBSInstances) != 1 {
+ t.Fatalf("expected 1 PBS instance, got %d", len(filtered.PBSInstances))
+ }
+ if filtered.PBSInstances[0].ID != "pbs1" {
+ t.Fatalf("expected PBS instance pbs1, got %s", filtered.PBSInstances[0].ID)
+ }
+}
+
func TestPatrolService_GetStatus_WithFindings(t *testing.T) {
ps := NewPatrolService(nil, nil)
diff --git a/internal/ai/service.go b/internal/ai/service.go
index cf0b23dd44..b1de53ebeb 100644
--- a/internal/ai/service.go
+++ b/internal/ai/service.go
@@ -527,15 +527,14 @@ func (s *Service) StartPatrol(ctx context.Context) {
// The patrol service itself should check HasLicenseFeature before LLM calls
}
- // Configure patrol from AI config
- patrolCfg := PatrolConfig{
- Enabled: true,
- Interval: cfg.GetPatrolInterval(),
- AnalyzeNodes: cfg.PatrolAnalyzeNodes,
- AnalyzeGuests: cfg.PatrolAnalyzeGuests,
- AnalyzeDocker: cfg.PatrolAnalyzeDocker,
- AnalyzeStorage: cfg.PatrolAnalyzeStorage,
- }
+ // Configure patrol from AI config (preserve defaults for resource types not in AI config)
+ patrolCfg := DefaultPatrolConfig()
+ patrolCfg.Enabled = true
+ patrolCfg.Interval = cfg.GetPatrolInterval()
+ patrolCfg.AnalyzeNodes = cfg.PatrolAnalyzeNodes
+ patrolCfg.AnalyzeGuests = cfg.PatrolAnalyzeGuests
+ patrolCfg.AnalyzeDocker = cfg.PatrolAnalyzeDocker
+ patrolCfg.AnalyzeStorage = cfg.PatrolAnalyzeStorage
patrol.SetConfig(patrolCfg)
patrol.SetProactiveMode(cfg.UseProactiveThresholds)
patrol.Start(ctx)
@@ -590,15 +589,14 @@ func (s *Service) ReconfigurePatrol() {
return
}
- // Update patrol configuration
- patrolCfg := PatrolConfig{
- Enabled: cfg.IsPatrolEnabled(),
- Interval: cfg.GetPatrolInterval(),
- AnalyzeNodes: cfg.PatrolAnalyzeNodes,
- AnalyzeGuests: cfg.PatrolAnalyzeGuests,
- AnalyzeDocker: cfg.PatrolAnalyzeDocker,
- AnalyzeStorage: cfg.PatrolAnalyzeStorage,
- }
+ // Update patrol configuration (preserve defaults for resource types not in AI config)
+ patrolCfg := DefaultPatrolConfig()
+ patrolCfg.Enabled = cfg.IsPatrolEnabled()
+ patrolCfg.Interval = cfg.GetPatrolInterval()
+ patrolCfg.AnalyzeNodes = cfg.PatrolAnalyzeNodes
+ patrolCfg.AnalyzeGuests = cfg.PatrolAnalyzeGuests
+ patrolCfg.AnalyzeDocker = cfg.PatrolAnalyzeDocker
+ patrolCfg.AnalyzeStorage = cfg.PatrolAnalyzeStorage
patrol.SetConfig(patrolCfg)
// Update proactive threshold mode
diff --git a/internal/api/ai_handlers.go b/internal/api/ai_handlers.go
index 393b8a87f5..1903e42d19 100644
--- a/internal/api/ai_handlers.go
+++ b/internal/api/ai_handlers.go
@@ -3297,20 +3297,15 @@ func (h *AISettingsHandler) HandleForcePatrol(w http.ResponseWriter, r *http.Req
return
}
- // Check for deep=true query parameter
+ // Check for deep=true query parameter (kept for backwards compatibility)
deep := r.URL.Query().Get("deep") == "true"
// Trigger patrol asynchronously
patrol.ForcePatrol(r.Context(), deep)
- patrolType := "quick"
- if deep {
- patrolType = "deep"
- }
-
response := map[string]interface{}{
"success": true,
- "message": fmt.Sprintf("Triggered %s patrol run", patrolType),
+ "message": "Triggered patrol run",
}
if err := utils.WriteJSONResponse(w, response); err != nil {