From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 25A7B1FF199 for ; Tue, 15 Oct 2024 10:46:23 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 92DE51165C; Tue, 15 Oct 2024 10:46:51 +0200 (CEST) From: Lukas Wagner To: pbs-devel@lists.proxmox.com Date: Tue, 15 Oct 2024 10:46:34 +0200 Message-Id: <20241015084636.57106-12-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241015084636.57106-1-l.wagner@proxmox.com> References: <20241015084636.57106-1-l.wagner@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.009 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy 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 Subject: [pbs-devel] [PATCH proxmox-backup v2 11/13] metric collection: initialize metric cache on startup 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: , Reply-To: Proxmox Backup Server development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pbs-devel-bounces@lists.proxmox.com Sender: "pbs-devel" Signed-off-by: Lukas Wagner --- 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 = 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 _______________________________________________ pbs-devel mailing list pbs-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel