diff --git a/agent/src/collectors/service.rs b/agent/src/collectors/service.rs index 2a902ea..81458a8 100644 --- a/agent/src/collectors/service.rs +++ b/agent/src/collectors/service.rs @@ -637,9 +637,42 @@ 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 + // Get the actual nginx config file path from systemd (NixOS uses custom config) + let config_path = match self.get_nginx_config_from_systemd().await { + Some(path) => { + tracing::debug!("Found nginx config path from systemd: {}", path); + path + } + None => { + tracing::warn!("Could not find nginx config path from systemd, using default"); + // Fallback to default nginx -T + match Command::new("sudo") + .args(["nginx", "-T"]) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .await + { + Ok(output) => { + 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); + return self.parse_nginx_config(&config); + } + Err(e) => { + tracing::warn!("Failed to execute sudo nginx -T: {}", e); + return None; + } + } + } + }; + + // Use the specific config file let output = match Command::new("sudo") - .args(["nginx", "-T"]) + .args(["nginx", "-T", "-c", &config_path]) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .output() @@ -647,7 +680,7 @@ impl ServiceCollector { { Ok(output) => output, Err(e) => { - tracing::warn!("Failed to execute sudo nginx -T: {}", e); + tracing::warn!("Failed to execute sudo nginx -T -c {}: {}", config_path, e); return None; } }; @@ -661,6 +694,43 @@ impl ServiceCollector { let config = String::from_utf8_lossy(&output.stdout); tracing::debug!("Got nginx config, {} bytes", config.len()); + self.parse_nginx_config(&config) + } + + async fn get_nginx_config_from_systemd(&self) -> Option { + let output = Command::new("systemctl") + .args(["show", "nginx", "--property=ExecStart", "--no-pager"]) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .await + .ok()?; + + if !output.status.success() { + return None; + } + + let stdout = String::from_utf8_lossy(&output.stdout); + // Parse ExecStart to extract -c config path + for line in stdout.lines() { + if line.starts_with("ExecStart=") { + // Look for "-c /path/to/config" in the command line + if let Some(c_index) = line.find(" -c ") { + let after_c = &line[c_index + 4..]; + if let Some(space_index) = after_c.find(' ') { + return Some(after_c[..space_index].to_string()); + } else { + // -c is the last argument, clean up systemd formatting + let cleaned = after_c.trim_end_matches(" ; ignore_errors=no ; start_time=[n/a] ; stop_time=[n/a] ; pid=0 ; code=(null) ; status=0/0 }"); + return Some(cleaned.to_string()); + } + } + } + } + None + } + + fn parse_nginx_config(&self, config: &str) -> Option> { let mut sites = Vec::new(); let mut server_name_lines = 0;