This commit is contained in:
Christoffer Martinsson 2025-10-12 16:29:31 +02:00
parent b287eb35fd
commit e9d42568fb

View File

@ -637,17 +637,50 @@ impl ServiceCollector {
async fn get_nginx_sites(&self) -> Option<Vec<String>> {
tracing::debug!("Starting nginx site detection");
// For NixOS and other systems, get the actual running nginx config
let output = match Command::new("sudo")
// 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", "-c", &config_path])
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.await
{
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<String> {
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<Vec<String>> {
let mut sites = Vec::new();
let mut server_name_lines = 0;