From 996b89aa478924b3fdf63812655d14fa473cb58d Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Wed, 15 Oct 2025 12:12:45 +0200 Subject: [PATCH] Fix critical cache key mismatch in smart agent Cache storage was using keys like 'hostname_service' but lookup was using 'hostname_CollectorName', causing all non-System collectors to fail. Changes: - Standardize cache keys to use collector names ('SystemCollector', 'ServiceCollector', etc.) - Add cache_key() getter method to CachedCollector - Fix cache lookup to use consistent keys This should resolve the issue where srv01 only shows System data but no Services/SMART/Backup data in the dashboard. --- agent/src/cached_collector.rs | 5 +++++ agent/src/smart_agent.rs | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/agent/src/cached_collector.rs b/agent/src/cached_collector.rs index 540f10c..80bbc8b 100644 --- a/agent/src/cached_collector.rs +++ b/agent/src/cached_collector.rs @@ -55,6 +55,11 @@ impl CachedCollector { self.cache.needs_refresh(&self.cache_key, &self.inner.agent_type()).await } + /// Get the cache key for this collector + pub fn cache_key(&self) -> &str { + &self.cache_key + } + /// Perform actual collection, bypassing cache pub async fn collect_fresh(&self) -> Result { let start = std::time::Instant::now(); diff --git a/agent/src/smart_agent.rs b/agent/src/smart_agent.rs index 2bfef18..3d76c3c 100644 --- a/agent/src/smart_agent.rs +++ b/agent/src/smart_agent.rs @@ -72,7 +72,7 @@ impl SmartAgent { let cached = CachedCollector::with_smart_interval( Box::new(smart_collector), Arc::clone(&cache), - format!("{}_smart", hostname), + "SmartCollector".to_string(), ); cached_collectors.push(cached); info!("SMART monitoring: {:?} (15min intervals)", valid_devices); @@ -85,7 +85,7 @@ impl SmartAgent { let cached = CachedCollector::with_smart_interval( Box::new(system_collector), Arc::clone(&cache), - format!("{}_system", hostname), + "SystemCollector".to_string(), ); cached_collectors.push(cached); info!("System monitoring: CPU, memory, temperature, C-states (5s intervals)"); @@ -101,7 +101,7 @@ impl SmartAgent { let cached = CachedCollector::with_smart_interval( Box::new(service_collector), Arc::clone(&cache), - format!("{}_services", hostname), + "ServiceCollector".to_string(), ); cached_collectors.push(cached); info!("Service monitoring: {:?} (5min intervals)", service_list); @@ -114,7 +114,7 @@ impl SmartAgent { let cached = CachedCollector::with_smart_interval( Box::new(backup_collector), Arc::clone(&cache), - format!("{}_backup", hostname), + "BackupCollector".to_string(), ); cached_collectors.push(cached); info!("Backup monitoring: repo={:?}, service={} (15min intervals)", restic_repo, backup_service); @@ -246,7 +246,7 @@ impl SmartAgent { } } else { // Use cached data - if let Some(cached_output) = self.cache.get(&format!("{}_{}", self.hostname, collector.name())).await { + if let Some(cached_output) = self.cache.get(collector.cache_key()).await { if let Err(e) = self.send_metrics(&cached_output.agent_type, &cached_output.data).await { error!("Failed to send cached metrics for {}: {}", collector.name(), e); }