]> git.proxmox.com Git - proxmox-backup.git/commitdiff
report: factor out getting first 30 lines of top output
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 28 Nov 2023 11:24:30 +0000 (12:24 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 23 Jan 2024 16:07:54 +0000 (17:07 +0100)
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
(cherry picked from commit 5736fa917c97254409a0edadd801603f4f75722c)
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
src/server/report.rs

index 4b16d8319c62b09739ebdbcf1b5dae40d52b950f..2889bd1d337a8d9121f1bc02f843369763012d11 100644 (file)
@@ -2,6 +2,19 @@ use std::fmt::Write;
 use std::path::Path;
 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>)> {
     vec![
         (
@@ -94,16 +107,7 @@ fn function_calls() -> Vec<FunctionMapping> {
             }
             list.join(", ")
         }),
-        ("System Load & Uptime", || {
-            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")
-        }),
+        ("System Load & Uptime", get_top_processes),
     ]
 }