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.
This commit is contained in:
Christoffer Martinsson 2025-10-15 12:12:45 +02:00
parent b0112dd8ab
commit 996b89aa47
2 changed files with 10 additions and 5 deletions

View File

@ -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<CollectorOutput, CollectorError> {
let start = std::time::Instant::now();

View File

@ -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);
}