[pbs-devel] [PATCH proxmox-backup v2 11/13] metric collection: initialize metric cache on startup

Lukas Wagner l.wagner at proxmox.com
Tue Oct 15 10:46:34 CEST 2024


Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
 Cargo.toml                                   |  2 ++
 src/server/metric_collection/mod.rs          | 10 ++++--
 src/server/metric_collection/pull_metrics.rs | 35 ++++++++++++++++++++
 3 files changed, 45 insertions(+), 2 deletions(-)
 create mode 100644 src/server/metric_collection/pull_metrics.rs

diff --git a/Cargo.toml b/Cargo.toml
index 2536550c..c531607f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -81,6 +81,7 @@ proxmox-rrd-api-types = "1.0.2"
 proxmox-schema = "3"
 proxmox-section-config = "2"
 proxmox-serde = "0.1.1"
+proxmox-shared-cache = "0.1"
 proxmox-shared-memory = "0.3.0"
 proxmox-sortable-macro = "0.1.2"
 proxmox-subscription = { version = "0.4.2", features = [ "api-types" ] }
@@ -225,6 +226,7 @@ proxmox-router = { workspace = true, features = [ "cli", "server"] }
 proxmox-schema = { workspace = true, features = [ "api-macro" ] }
 proxmox-section-config.workspace = true
 proxmox-serde = { workspace = true, features = [ "serde_json" ] }
+proxmox-shared-cache.workspace = true
 proxmox-shared-memory.workspace = true
 proxmox-sortable-macro.workspace = true
 proxmox-subscription.workspace = true
diff --git a/src/server/metric_collection/mod.rs b/src/server/metric_collection/mod.rs
index 8278e001..3be73c22 100644
--- a/src/server/metric_collection/mod.rs
+++ b/src/server/metric_collection/mod.rs
@@ -6,9 +6,9 @@ use std::{
 };
 
 use anyhow::Error;
-use pbs_api_types::{DataStoreConfig, Operation};
 use tokio::join;
 
+use pbs_api_types::{DataStoreConfig, Operation};
 use proxmox_sys::{
     fs::FileSystemInformation,
     linux::procfs::{Loadavg, ProcFsMemInfo, ProcFsNetDev, ProcFsStat},
@@ -17,14 +17,20 @@ use proxmox_sys::{
 use crate::tools::disks::{zfs_dataset_stats, BlockDevStat, DiskManage};
 
 mod metric_server;
+mod pull_metrics;
 pub mod rrd;
 
+const METRIC_COLLECTION_INTERVAL: Duration = Duration::from_secs(10);
+
 /// Initialize the metric collection subsystem.
 ///
 /// Any datapoints in the RRD journal will be committed.
 pub fn init() -> Result<(), Error> {
     let rrd_cache = rrd::init()?;
     rrd_cache.apply_journal()?;
+
+    pull_metrics::init()?;
+
     Ok(())
 }
 
@@ -43,7 +49,7 @@ pub fn start_collection_task() {
 
 async fn run_stat_generator() {
     loop {
-        let delay_target = Instant::now() + Duration::from_secs(10);
+        let delay_target = Instant::now() + METRIC_COLLECTION_INTERVAL;
 
         let stats_future = tokio::task::spawn_blocking(|| {
             let hoststats = collect_host_stats_sync();
diff --git a/src/server/metric_collection/pull_metrics.rs b/src/server/metric_collection/pull_metrics.rs
new file mode 100644
index 00000000..707cb27c
--- /dev/null
+++ b/src/server/metric_collection/pull_metrics.rs
@@ -0,0 +1,35 @@
+use std::{path::Path, sync::OnceLock, time::Duration};
+
+use anyhow::{format_err, Error};
+
+use nix::sys::stat::Mode;
+use pbs_buildcfg::PROXMOX_BACKUP_RUN_DIR;
+use proxmox_shared_cache::SharedCache;
+use proxmox_sys::fs::CreateOptions;
+
+use super::METRIC_COLLECTION_INTERVAL;
+
+const METRIC_CACHE_TIME: Duration = Duration::from_secs(30 * 60);
+const STORED_METRIC_GENERATIONS: u64 =
+    METRIC_CACHE_TIME.as_secs() / METRIC_COLLECTION_INTERVAL.as_secs();
+
+static METRIC_CACHE: OnceLock<SharedCache> = OnceLock::new();
+
+/// Initialize the metric cache.
+pub(super) fn init() -> Result<(), Error> {
+    let backup_user = pbs_config::backup_user()?;
+    let file_opts = CreateOptions::new()
+        .owner(backup_user.uid)
+        .group(backup_user.gid)
+        .perm(Mode::from_bits_truncate(0o660));
+
+    let cache_location = Path::new(PROXMOX_BACKUP_RUN_DIR).join("metrics");
+
+    let cache = SharedCache::new(cache_location, file_opts, STORED_METRIC_GENERATIONS as u32)?;
+
+    METRIC_CACHE
+        .set(cache)
+        .map_err(|_e| format_err!("metric cache already initialized"))?;
+
+    Ok(())
+}
-- 
2.39.5





More information about the pbs-devel mailing list