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
This commit is contained in:
parent
f3b6d12f68
commit
819ca4ad73
@ -262,6 +262,60 @@ impl SystemCollector {
|
||||
"ok".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_top_cpu_process(&self) -> Option<String> {
|
||||
// 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::<f32>().unwrap_or(0.0) > 0.1 {
|
||||
return Some(format!("{} {:.1}%", command, cpu_percent.parse::<f32>().unwrap_or(0.0)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
async fn get_top_ram_process(&self) -> Option<String> {
|
||||
// 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::<f32>().unwrap_or(0.0) > 0.1 {
|
||||
return Some(format!("{} {:.1}%", command, mem_percent.parse::<f32>().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<String> {
|
||||
// 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::<f32>().unwrap_or(0.0) > 0.1 {
|
||||
return Some(format!("{} {:.1}%", command, cpu_percent.parse::<f32>().unwrap_or(0.0)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
async fn get_top_ram_process(&self) -> Option<String> {
|
||||
// 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::<f32>().unwrap_or(0.0) > 0.1 {
|
||||
return Some(format!("{} {:.1}%", command, mem_percent.parse::<f32>().unwrap_or(0.0)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user