From d77139b172b518659187442a4220203ddb0480ea Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Sun, 12 Oct 2025 16:14:38 +0200 Subject: [PATCH] Testing --- agent/src/collectors/service.rs | 47 ++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/agent/src/collectors/service.rs b/agent/src/collectors/service.rs index f051fe5..c4dbe1e 100644 --- a/agent/src/collectors/service.rs +++ b/agent/src/collectors/service.rs @@ -465,15 +465,21 @@ impl ServiceCollector { let update_interval = 30; // seconds let service_offset = service_hash % update_interval; - if (now + service_offset) % update_interval == 0 { + let should_update = (now + service_offset) % update_interval == 0; + + if should_update { + tracing::debug!("Service {} updating description (throttle check passed)", service); self.get_service_description(service).await } else { + let next_update = update_interval - ((now + service_offset) % update_interval); + tracing::trace!("Service {} throttled, next update in {} seconds", service, next_update); None // Return None to indicate no new description this cycle } } async fn get_service_description(&self, service: &str) -> Option> { - match service { + tracing::debug!("Getting description for service: {}", service); + let result = match service { "sshd" | "ssh" => self.get_ssh_active_users().await.map(|s| vec![s]), "nginx" => self.get_nginx_sites().await, "apache2" | "httpd" => self.get_web_server_connections().await.map(|s| vec![s]), @@ -481,7 +487,14 @@ impl ServiceCollector { "postgresql" | "postgres" => self.get_postgres_connections().await.map(|s| vec![s]), "mysql" | "mariadb" => self.get_mysql_connections().await.map(|s| vec![s]), _ => None, + }; + + match &result { + Some(descriptions) => tracing::info!("Service {} got {} descriptions: {:?}", service, descriptions.len(), descriptions), + None => tracing::debug!("Service {} got no description", service), } + + result } async fn get_ssh_active_users(&self) -> Option { @@ -622,44 +635,70 @@ impl ServiceCollector { } async fn get_nginx_sites(&self) -> Option> { + tracing::debug!("Starting nginx site detection"); + // For NixOS and other systems, get the actual running nginx config - let output = Command::new("nginx") + let output = match Command::new("nginx") .args(["-T"]) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .output() .await - .ok()?; + { + Ok(output) => output, + Err(e) => { + tracing::warn!("Failed to execute nginx -T: {}", e); + return None; + } + }; if !output.status.success() { + let stderr = String::from_utf8_lossy(&output.stderr); + tracing::warn!("nginx -T failed with status {}: {}", output.status, stderr); return None; } let config = String::from_utf8_lossy(&output.stdout); + tracing::debug!("Got nginx config, {} bytes", config.len()); + let mut sites = Vec::new(); + let mut server_name_lines = 0; for line in config.lines() { let trimmed = line.trim(); if trimmed.starts_with("server_name") { + server_name_lines += 1; + tracing::debug!("Found server_name line: {}", trimmed); + // Extract server names from "server_name example.com www.example.com;" if let Some(names_part) = trimmed.strip_prefix("server_name") { let names_clean = names_part.trim().trim_end_matches(';'); + tracing::debug!("Extracted names part: '{}'", names_clean); + for name in names_clean.split_whitespace() { // Skip default catch-all server names if name != "_" && !name.is_empty() && name.contains('.') && !name.starts_with('$') { + tracing::debug!("Adding site: {}", name); sites.push(name.to_string()); + } else { + tracing::debug!("Skipping invalid site: {}", name); } } } } } + tracing::info!("Found {} server_name lines, extracted {} sites", server_name_lines, sites.len()); + // Remove duplicates and limit to reasonable number sites.sort(); sites.dedup(); sites.truncate(15); // Show max 15 sites to avoid overwhelming the UI + tracing::info!("Final nginx sites list: {:?}", sites); + if sites.is_empty() { + tracing::warn!("No nginx sites found"); None } else { Some(sites)