report: factor out getting first 30 lines of top output

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2023-11-28 12:24:30 +01:00
parent c911678b94
commit 5736fa917c

View File

@ -2,6 +2,19 @@ use std::fmt::Write;
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
fn get_top_processes() -> String {
let (exe, args) = ("top", vec!["-b", "-c", "-w512", "-n", "1", "-o", "TIME"]);
let output = Command::new(exe)
.args(&args)
.output();
let output = match output {
Ok(output) => String::from_utf8_lossy(&output.stdout).to_string(),
Err(err) => err.to_string(),
};
let output = output.lines().take(30).collect::<Vec<&str>>().join("\n");
format!("$ `{exe} {}`\n```\n{output}\n```", args.join(" "))
}
fn files() -> Vec<(&'static str, Vec<&'static str>)> { fn files() -> Vec<(&'static str, Vec<&'static str>)> {
vec![ vec![
( (
@ -94,16 +107,7 @@ fn function_calls() -> Vec<FunctionMapping> {
} }
list.join(", ") list.join(", ")
}), }),
("System Load & Uptime", || { ("System Load & Uptime", get_top_processes),
let output = Command::new("top")
.args(vec!["-b", "-c", "-w512", "-n", "1", "-o", "TIME"])
.output();
let output = match output {
Ok(output) => String::from_utf8_lossy(&output.stdout).to_string(),
Err(err) => err.to_string(),
};
output.lines().take(30).collect::<Vec<&str>>().join("\n")
}),
] ]
} }