]> git.proxmox.com Git - proxmox-backup.git/blob - src/server/report.rs
report: avoid lazy_static for command/files/.. definitions
[proxmox-backup.git] / src / server / report.rs
1 use std::path::Path;
2 use std::process::Command;
3
4 use crate::config::datastore;
5 use crate::tools::subscription::read_subscription;
6
7 fn files() -> Vec<&'static str> {
8 vec![
9 "/etc/hosts",
10 "/etc/network/interfaces",
11 ]
12 }
13
14 fn commands() -> Vec<(&'static str, Vec<&'static str>)> {
15 vec![
16 // ("<command>", vec![<arg [, arg]>])
17 ("df", vec!["-h"]),
18 ("lsblk", vec!["--ascii"]),
19 ]
20 }
21
22 // (<description>, <function to call>)
23 fn function_calls() -> Vec<(&'static str, fn() -> String)> {
24 vec![
25 ("Subscription status", || match read_subscription() {
26 Ok(Some(sub_info)) => sub_info.status.to_string(),
27 _ => String::from("No subscription found"),
28 }),
29 ("Datastores", || {
30 let config = match datastore::config() {
31 Ok((config, _digest)) => config,
32 _ => return String::from("could not read datastore config"),
33 };
34
35 let mut list = Vec::new();
36 for (store, _) in &config.sections {
37 list.push(store.as_str());
38 }
39 list.join(", ")
40 })
41 ]
42 }
43
44 pub fn generate_report() -> String {
45 use proxmox::tools::fs::file_read_optional_string;
46
47 let file_contents = files()
48 .iter()
49 .map(|file_name| {
50 let content = match file_read_optional_string(Path::new(file_name)) {
51 Ok(Some(content)) => content,
52 Err(err) => err.to_string(),
53 _ => String::from("Could not be read!"),
54 };
55 format!("# {}\n{}", file_name, content)
56 })
57 .collect::<Vec<String>>()
58 .join("\n\n");
59
60 let command_outputs = commands()
61 .iter()
62 .map(|(command, args)| {
63 let output = match Command::new(command).args(args).output() {
64 Ok(output) => String::from_utf8_lossy(&output.stdout).to_string(),
65 Err(err) => err.to_string(),
66 };
67 format!("# {} {}\n{}", command, args.join(" "), output)
68 })
69 .collect::<Vec<String>>()
70 .join("\n\n");
71
72 let function_outputs = function_calls()
73 .iter()
74 .map(|(desc, function)| format!("# {}\n{}", desc, function()))
75 .collect::<Vec<String>>()
76 .join("\n\n");
77
78 format!(
79 " FILES\n{}\n COMMANDS\n{}\n FUNCTIONS\n{}",
80 file_contents, command_outputs, function_outputs
81 )
82 }