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 895377F46F for ; Fri, 12 Nov 2021 13:31:00 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 7D336199D1 for ; Fri, 12 Nov 2021 13:30:30 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (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 D483C1998B for ; Fri, 12 Nov 2021 13:30:28 +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 AB38744BAF for ; Fri, 12 Nov 2021 13:30:28 +0100 (CET) From: Hannes Laimer To: pbs-devel@lists.proxmox.com Date: Fri, 12 Nov 2021 13:30:18 +0100 Message-Id: <20211112123019.15384-6-h.laimer@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211112123019.15384-1-h.laimer@proxmox.com> References: <20211112123019.15384-1-h.laimer@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.076 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment 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. [lib.rs, datastore.rs] Subject: [pbs-devel] [PATCH v4 proxmox-backup 5/6] api2: add get_active_operations endpoint 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: Fri, 12 Nov 2021 12:31:00 -0000 --- new in v4: needed for displaying the currently conbflicting tasks when setting a new maintenance type. pbs-datastore/src/datastore.rs | 41 ++++++++++++++++++++++++++++++++++ pbs-datastore/src/lib.rs | 2 +- src/api2/admin/datastore.rs | 31 +++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs index d8c2f3f5..a0cc1a03 100644 --- a/pbs-datastore/src/datastore.rs +++ b/pbs-datastore/src/datastore.rs @@ -79,6 +79,47 @@ impl Clone for DataStore { } } +pub fn get_active_operations(name: &str, operation: Operation) -> Result { + let mut path = PathBuf::from(crate::ACTIVE_OPERATIONS_DIR); + let mut lock_path = PathBuf::from(crate::ACTIVE_OPERATIONS_DIR); + path.push(name); + lock_path.push(format!(".{}.lock", name)); + + let user = pbs_config::backup_user()?; + let options = proxmox::tools::fs::CreateOptions::new() + .group(user.gid) + .owner(user.uid) + .perm(nix::sys::stat::Mode::from_bits_truncate(0o660)); + + let timeout = std::time::Duration::new(10, 0); + proxmox::tools::fs::open_file_locked(&lock_path, timeout, true, options.clone())?; + + match proxmox::tools::fs::file_get_optional_contents(&path) { + Ok(Some(data)) => { + let mut count = 0; + if let Ok(str_data) = String::from_utf8(data) { + for line in str_data.lines() { + let split = line.split(" ").collect::>(); + match split[0].parse::() { + Ok(line_pid) + if procfs::check_process_running(line_pid).is_some() + && split.len() == 3 => + { + count += match operation { + Operation::Read => split[1].parse::().unwrap_or(0), + Operation::Write => split[2].parse::().unwrap_or(0), + } + } + _ => {} + } + } + }; + Ok(count) + } + _ => Ok(0), + } +} + fn update_active_operations(name: &str, operation: Operation, count: i64) -> Result<(), Error> { let mut path = PathBuf::from(crate::ACTIVE_OPERATIONS_DIR); let mut lock_path = PathBuf::from(crate::ACTIVE_OPERATIONS_DIR); diff --git a/pbs-datastore/src/lib.rs b/pbs-datastore/src/lib.rs index 159642fd..c0f9c0d8 100644 --- a/pbs-datastore/src/lib.rs +++ b/pbs-datastore/src/lib.rs @@ -200,7 +200,7 @@ pub use manifest::BackupManifest; pub use store_progress::StoreProgress; mod datastore; -pub use datastore::{check_backup_owner, DataStore}; +pub use datastore::{get_active_operations, check_backup_owner, DataStore}; mod snapshot_reader; pub use snapshot_reader::SnapshotReader; diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index 02a42369..ab962e23 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -1576,6 +1576,32 @@ pub fn get_rrd_stats( ) } +#[api( + input: { + properties: { + store: { + schema: DATASTORE_SCHEMA, + }, + }, + }, + access: { + permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_AUDIT, true), + }, +)] +/// Read datastore stats +pub fn get_active_operations( + store: String, + _param: Value, +) -> Result { + + let reading = pbs_datastore::get_active_operations(&store, Operation::Read)?; + let writing = pbs_datastore::get_active_operations(&store, Operation::Write)?; + Ok(json!({ + "read": reading, + "write": writing + })) +} + #[api( input: { properties: { @@ -1933,6 +1959,11 @@ pub fn set_backup_owner( #[sortable] const DATASTORE_INFO_SUBDIRS: SubdirMap = &[ + ( + "active-operations", + &Router::new() + .get(&API_METHOD_GET_ACTIVE_OPERATIONS) + ), ( "catalog", &Router::new() -- 2.30.2