From 819ca4ad73e2f1097f84ad93c2a748433cecb5df Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Tue, 14 Oct 2025 22:05:44 +0200 Subject: [PATCH] Fix SystemCollector method placement and remove duplicates - Move get_top_cpu_process() and get_top_ram_process() methods inside SystemCollector impl block - Remove duplicate method definitions that were placed after trait implementation - Ensures methods are properly accessible during compilation --- agent/src/collectors/system.rs | 108 ++++++++++++++++----------------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/agent/src/collectors/system.rs b/agent/src/collectors/system.rs index 9984ba3..06e4170 100644 --- a/agent/src/collectors/system.rs +++ b/agent/src/collectors/system.rs @@ -262,6 +262,60 @@ impl SystemCollector { "ok".to_string() } } + + async fn get_top_cpu_process(&self) -> Option { + // Get top CPU process using ps command + let output = Command::new("/run/current-system/sw/bin/ps") + .args(["aux", "--sort=-pcpu"]) + .output() + .await + .ok()?; + + if output.status.success() { + let stdout = String::from_utf8_lossy(&output.stdout); + // Skip header line and get first process + for line in stdout.lines().skip(1) { + let fields: Vec<&str> = line.split_whitespace().collect(); + if fields.len() >= 11 { + let cpu_percent = fields[2]; + let command = fields[10]; + // Skip kernel threads (in brackets) and low CPU processes + if !command.starts_with('[') && cpu_percent.parse::().unwrap_or(0.0) > 0.1 { + return Some(format!("{} {:.1}%", command, cpu_percent.parse::().unwrap_or(0.0))); + } + } + } + } + + None + } + + async fn get_top_ram_process(&self) -> Option { + // Get top RAM process using ps command + let output = Command::new("/run/current-system/sw/bin/ps") + .args(["aux", "--sort=-rss"]) + .output() + .await + .ok()?; + + if output.status.success() { + let stdout = String::from_utf8_lossy(&output.stdout); + // Skip header line and get first process + for line in stdout.lines().skip(1) { + let fields: Vec<&str> = line.split_whitespace().collect(); + if fields.len() >= 11 { + let mem_percent = fields[3]; + let command = fields[10]; + // Skip kernel threads (in brackets) and low memory processes + if !command.starts_with('[') && mem_percent.parse::().unwrap_or(0.0) > 0.1 { + return Some(format!("{} {:.1}%", command, mem_percent.parse::().unwrap_or(0.0))); + } + } + } + } + + None + } } #[async_trait] @@ -352,58 +406,4 @@ impl Collector for SystemCollector { data: system_metrics, }) } - - async fn get_top_cpu_process(&self) -> Option { - // Get top CPU process using ps command - let output = Command::new("/run/current-system/sw/bin/ps") - .args(["aux", "--sort=-pcpu"]) - .output() - .await - .ok()?; - - if output.status.success() { - let stdout = String::from_utf8_lossy(&output.stdout); - // Skip header line and get first process - for line in stdout.lines().skip(1) { - let fields: Vec<&str> = line.split_whitespace().collect(); - if fields.len() >= 11 { - let cpu_percent = fields[2]; - let command = fields[10]; - // Skip kernel threads (in brackets) and low CPU processes - if !command.starts_with('[') && cpu_percent.parse::().unwrap_or(0.0) > 0.1 { - return Some(format!("{} {:.1}%", command, cpu_percent.parse::().unwrap_or(0.0))); - } - } - } - } - - None - } - - async fn get_top_ram_process(&self) -> Option { - // Get top RAM process using ps command - let output = Command::new("/run/current-system/sw/bin/ps") - .args(["aux", "--sort=-rss"]) - .output() - .await - .ok()?; - - if output.status.success() { - let stdout = String::from_utf8_lossy(&output.stdout); - // Skip header line and get first process - for line in stdout.lines().skip(1) { - let fields: Vec<&str> = line.split_whitespace().collect(); - if fields.len() >= 11 { - let mem_percent = fields[3]; - let command = fields[10]; - // Skip kernel threads (in brackets) and low memory processes - if !command.starts_with('[') && mem_percent.parse::().unwrap_or(0.0) > 0.1 { - return Some(format!("{} {:.1}%", command, mem_percent.parse::().unwrap_or(0.0))); - } - } - } - } - - None - } } \ No newline at end of file