From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id AC4901FF0B3 for ; Wed, 14 Oct 2026 16:44:39 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id CE68D2167A; Wed, 14 Oct 2026 16:44:22 +0200 (CEST) From: Christian Ebner To: pbs-devel@lists.proxmox.com Subject: [PATCH proxmox-backup v2 12/14] api: admin: add endpoint to fetch fine-grained datastore usage stats Date: Wed, 14 Oct 2026 16:43:55 +0200 Message-ID: <20261014144357.771051-13-c.ebner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261014144357.771051-1-c.ebner@proxmox.com> References: <20261014144357.771051-1-c.ebner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1791989043836 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.518 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) 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 Message-ID-Hash: K7VDA4HH6VZNKTVUOYKSVKA5KSU7HE36 X-Message-ID-Hash: K7VDA4HH6VZNKTVUOYKSVKA5KSU7HE36 X-MailFrom: c.ebner@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox Backup Server development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Provides the endpoint to read the per-namespace/group/snapshot usage statistics as persisted during the last garbage collection run. Signed-off-by: Christian Ebner --- pbs-datastore/src/datastore.rs | 13 ++++++++++++- src/api2/admin/datastore.rs | 35 +++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs index f568ea2b3..a35b3721f 100644 --- a/pbs-datastore/src/datastore.rs +++ b/pbs-datastore/src/datastore.rs @@ -24,7 +24,7 @@ use proxmox_s3_client::{ use proxmox_schema::ApiType; use proxmox_sys::error::SysError; -use proxmox_sys::fs::{CreateOptions, file_read_optional_string, replace_file}; +use proxmox_sys::fs::{CreateOptions, file_get_contents, file_read_optional_string, replace_file}; use proxmox_sys::linux::procfs::MountInfo; use proxmox_sys::process_locker::{ProcessLockExclusiveGuard, ProcessLockSharedGuard}; use proxmox_time::{TimeSpan, epoch_i64}; @@ -3546,6 +3546,17 @@ impl DataStore { replace_file(&path, &contents, CreateOptions::new(), true)?; Ok(()) } + + pub fn read_statistics(&self) -> Result, Error> { + let path = self.base_path().join(DATASTORE_STATISTICS_FILENAME); + if !path.exists() { + // no statistics generated yet + return Ok(Vec::new()); + } + let contents = file_get_contents(&path)?; + let list = serde_json::from_slice(&contents)?; + Ok(list) + } } /// Track S3 object keys to be deleted by garbage collection while holding their file lock. diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index 3367fb2c1..b111af5fa 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -44,9 +44,10 @@ use pbs_api_types::{ IGNORE_VERIFIED_BACKUPS_SCHEMA, JobScheduleStatus, KeepOptions, MAX_NAMESPACE_DEPTH, MaintenanceMode, MaintenanceType, NS_MAX_DEPTH_SCHEMA, Operation, PRIV_DATASTORE_AUDIT, PRIV_DATASTORE_BACKUP, PRIV_DATASTORE_MODIFY, PRIV_DATASTORE_PRUNE, PRIV_DATASTORE_READ, - PRIV_DATASTORE_VERIFY, PRIV_SYS_MODIFY, PruneJobOptions, SnapshotListItem, SyncJobConfig, UPID, - UPID_SCHEMA, VERIFICATION_OUTDATED_AFTER_SCHEMA, VERIFY_JOB_READ_THREADS_SCHEMA, - VERIFY_JOB_VERIFY_THREADS_SCHEMA, print_ns_and_snapshot, print_store_and_ns, + PRIV_DATASTORE_VERIFY, PRIV_SYS_MODIFY, PruneJobOptions, SnapshotListItem, StatisticsListItem, + SyncJobConfig, UPID, UPID_SCHEMA, VERIFICATION_OUTDATED_AFTER_SCHEMA, + VERIFY_JOB_READ_THREADS_SCHEMA, VERIFY_JOB_VERIFY_THREADS_SCHEMA, print_ns_and_snapshot, + print_store_and_ns, }; use pbs_client::pxar::{create_tar, create_zip}; use pbs_config::CachedUserInfo; @@ -523,6 +524,30 @@ pub async fn list_snapshots( .map_err(|err| format_err!("failed to await blocking task: {err}"))? } +#[api( + input: { + properties: { + store: { schema: DATASTORE_SCHEMA }, + }, + }, + access: { + permission: &Permission::Privilege(&["datastore", "{store}"], PRIV_DATASTORE_AUDIT, false), + }, + returns: pbs_api_types::ADMIN_DATASTORE_LIST_STATISTICS_RETURN_TYPE, +)] +/// List backup usage statistics. +pub fn list_statistics( + store: String, + _info: &ApiMethod, + _rpcenv: &mut dyn RpcEnvironment, +) -> Result, Error> { + let datastore = DataStore::lookup_datastore(lookup_with(&store, Operation::Read))?; + let stats = datastore + .read_statistics() + .map_err(|err| format_err!("failed to read statistics: {err}"))?; + Ok(stats) +} + fn get_group_owner( store: &str, ns: &BackupNamespace, @@ -2984,6 +3009,10 @@ const DATASTORE_INFO_SUBDIRS: SubdirMap = &[ .get(&API_METHOD_LIST_GROUPS) .delete(&API_METHOD_DELETE_GROUP), ), + ( + "list-statistics", + &Router::new().get(&API_METHOD_LIST_STATISTICS), + ), ("mount", &Router::new().post(&API_METHOD_MOUNT)), ("move-group", &Router::new().post(&API_METHOD_MOVE_GROUP)), ( -- 2.47.3