From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 363B26555E for ; Tue, 3 Nov 2020 13:29:16 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 342882C11B for ; Tue, 3 Nov 2020 13:29:16 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id F23AF2C105 for ; Tue, 3 Nov 2020 13:29:14 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id B2A7C46021 for ; Tue, 3 Nov 2020 13:29:14 +0100 (CET) From: Hannes Laimer To: pbs-devel@lists.proxmox.com Date: Tue, 3 Nov 2020 13:29:06 +0100 Message-Id: <20201103122908.114524-2-h.laimer@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201103122908.114524-1-h.laimer@proxmox.com> References: <20201103122908.114524-1-h.laimer@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [report.rs, server.rs, node.rs] Subject: [pbs-devel] [PATCH proxmox-backup 1/3] report: add api endpoint and function to generate report X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 03 Nov 2020 12:29:16 -0000 Signed-off-by: Hannes Laimer --- src/api2/node.rs | 2 ++ src/api2/node/report.rs | 35 +++++++++++++++++++ src/server.rs | 3 ++ src/server/report.rs | 77 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 src/api2/node/report.rs create mode 100644 src/server/report.rs diff --git a/src/api2/node.rs b/src/api2/node.rs index 30800d05..a19bea7e 100644 --- a/src/api2/node.rs +++ b/src/api2/node.rs @@ -38,6 +38,7 @@ mod services; mod status; mod syslog; mod time; +mod report; pub const SHELL_CMD_SCHEMA: Schema = StringSchema::new("The command to run.") .format(&ApiStringFormat::Enum(&[ @@ -310,6 +311,7 @@ pub const SUBDIRS: SubdirMap = &[ ("dns", &dns::ROUTER), ("journal", &journal::ROUTER), ("network", &network::ROUTER), + ("report", &report::ROUTER), ("rrd", &rrd::ROUTER), ("services", &services::ROUTER), ("status", &status::ROUTER), diff --git a/src/api2/node/report.rs b/src/api2/node/report.rs new file mode 100644 index 00000000..b58427d7 --- /dev/null +++ b/src/api2/node/report.rs @@ -0,0 +1,35 @@ +use anyhow::Error; +use proxmox::api::{api, ApiMethod, Permission, Router, RpcEnvironment}; +use serde_json::{json, Value}; + +use crate::api2::types::*; +use crate::config::acl::PRIV_SYS_AUDIT; +use crate::server::generate_report; + +#[api( + input: { + properties: { + node: { + schema: NODE_SCHEMA, + }, + }, + }, + returns: { + type: String, + description: "Returns report of the node" + }, + access: { + permission: &Permission::Privilege(&["system", "status"], PRIV_SYS_AUDIT, false), + }, +)] +/// Generate a report +fn get_report( + _param: Value, + _info: &ApiMethod, + _rpcenv: &mut dyn RpcEnvironment, +) -> Result { + Ok(json!(generate_report())) +} + +pub const ROUTER: Router = Router::new() + .get(&API_METHOD_GET_REPORT); diff --git a/src/server.rs b/src/server.rs index 05fd25d9..983a300d 100644 --- a/src/server.rs +++ b/src/server.rs @@ -84,3 +84,6 @@ pub use gc_job::*; mod email_notifications; pub use email_notifications::*; + +mod report; +pub use report::*; diff --git a/src/server/report.rs b/src/server/report.rs new file mode 100644 index 00000000..8c109660 --- /dev/null +++ b/src/server/report.rs @@ -0,0 +1,77 @@ +use std::path::Path; +use std::process::Command; + +use lazy_static::lazy_static; + +use crate::config::datastore; +use crate::tools::subscription::read_subscription; + +lazy_static! { + static ref FILES: Vec<&'static str> = vec!["/etc/hosts", "/etc/network/interfaces"]; + + // (, ) + static ref COMMANDS: Vec<(&'static str, Vec<&'static str>)> = vec![ + ("/usr/bin/df", vec!["-h"]), + ("/usr/bin/lsblk", vec!["-ascii"]) + ]; + + // (, ) + static ref FUNCTION_CALLS: Vec<(&'static str, fn() -> String)> = vec![ + ("Subscription status", || match read_subscription() { + Ok(Some(sub_info)) => sub_info.status.to_string(), + _ => String::from("No subscription found"), + }), + ("Datastores", || { + let config = match datastore::config() { + Ok((config, _digest)) => config, + _ => return String::from("could not read datastore config"), + }; + + let mut list = Vec::new(); + for (store, _) in &config.sections { + list.push(store.as_str()); + } + list.join(", ") + }) + ]; +} + +pub fn generate_report() -> String { + use proxmox::tools::fs::file_read_optional_string; + + let file_contents = FILES + .iter() + .map(|file_name| { + let content = match file_read_optional_string(Path::new(file_name)) { + Ok(Some(content)) => content, + Err(err) => err.to_string(), + _ => String::from("Could not be read!"), + }; + format!("# {}\n{}", file_name, content) + }) + .collect::>() + .join("\n\n"); + + let command_outputs = COMMANDS + .iter() + .map(|(command, args)| { + let output = match Command::new(command).args(args).output() { + Ok(output) => String::from_utf8_lossy(&output.stdout).to_string(), + Err(err) => err.to_string(), + }; + format!("# {} {}\n{}", command, args.join(" "), output) + }) + .collect::>() + .join("\n\n"); + + let function_outputs = FUNCTION_CALLS + .iter() + .map(|(desc, function)| format!("# {}\n{}", desc, function())) + .collect::>() + .join("\n\n"); + + format!( + " FILES\n{}\n COMMANDS\n{}\n FUNCTIONS\n{}", + file_contents, command_outputs, function_outputs + ) +} -- 2.20.1