public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Hannes Laimer <h.laimer@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH v4 proxmox-backup 5/6] api2: add get_active_operations endpoint
Date: Fri, 12 Nov 2021 13:30:18 +0100	[thread overview]
Message-ID: <20211112123019.15384-6-h.laimer@proxmox.com> (raw)
In-Reply-To: <20211112123019.15384-1-h.laimer@proxmox.com>

---
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<i64, Error> {
+    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::<Vec<&str>>();
+                    match split[0].parse::<i32>() {
+                        Ok(line_pid)
+                            if procfs::check_process_running(line_pid).is_some()
+                                && split.len() == 3 =>
+                        {
+                            count += match operation {
+                                Operation::Read => split[1].parse::<i64>().unwrap_or(0),
+                                Operation::Write => split[2].parse::<i64>().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<Value, Error> {
+    
+    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





  parent reply	other threads:[~2021-11-12 12:31 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-12 12:30 [pbs-devel] [PATCH v4 proxmox-backup 0/6] closes #3071: maintenance mode for datastore Hannes Laimer
2021-11-12 12:30 ` [pbs-devel] [PATCH v4 proxmox-backup 1/6] pbs-api-types: add maintenance type Hannes Laimer
2021-11-12 12:30 ` [pbs-devel] [PATCH v4 proxmox-backup 2/6] pbs-datastore: add check for maintenance in lookup Hannes Laimer
2021-11-12 12:30 ` [pbs-devel] [PATCH v4 proxmox-backup 3/6] pbs-datastore: add active operations tracking Hannes Laimer
2021-11-19 15:21   ` Dominik Csapak
2021-11-12 12:30 ` [pbs-devel] [PATCH v4 proxmox-backup 4/6] api2: make maintenance_type updatable Hannes Laimer
2021-11-12 12:30 ` Hannes Laimer [this message]
2021-11-12 12:30 ` [pbs-devel] [PATCH v4 proxmox-backup 6/6] ui: add option to change the maintenance type Hannes Laimer
2021-11-22  7:28 ` [pbs-devel] [PATCH v4 proxmox-backup 0/6] closes #3071: maintenance mode for datastore Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211112123019.15384-6-h.laimer@proxmox.com \
    --to=h.laimer@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal