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()