From 92d6b428372acd8bc1f76f4159c3c37b7158a2ec Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Mon, 13 Oct 2025 12:47:28 +0200 Subject: [PATCH] Fix nginx detection when running as root - skip sudo --- agent/src/collectors/service.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/agent/src/collectors/service.rs b/agent/src/collectors/service.rs index a2f5007..75a786d 100644 --- a/agent/src/collectors/service.rs +++ b/agent/src/collectors/service.rs @@ -572,6 +572,11 @@ impl ServiceCollector { } } + fn is_running_as_root(&self) -> bool { + std::env::var("USER").unwrap_or_default() == "root" || + std::env::var("UID").unwrap_or_default() == "0" + } + async fn get_nginx_sites(&self) -> Option> { // Get the actual nginx config file path from systemd (NixOS uses custom config) @@ -579,8 +584,16 @@ impl ServiceCollector { Some(path) => path, None => { // Fallback to default nginx -T - match Command::new("sudo") - .args(["nginx", "-T"]) + let mut cmd = if self.is_running_as_root() { + Command::new("nginx") + } else { + let mut cmd = Command::new("sudo"); + cmd.arg("nginx"); + cmd + }; + + match cmd + .args(["-T"]) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .output() @@ -601,8 +614,16 @@ impl ServiceCollector { }; // Use the specific config file - let output = match Command::new("sudo") - .args(["nginx", "-T", "-c", &config_path]) + let mut cmd = if self.is_running_as_root() { + Command::new("nginx") + } else { + let mut cmd = Command::new("sudo"); + cmd.arg("nginx"); + cmd + }; + + let output = match cmd + .args(["-T", "-c", &config_path]) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .output()