metric collection: initialize metric cache on startup

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
This commit is contained in:
Lukas Wagner 2024-10-15 10:46:34 +02:00 committed by Wolfgang Bumiller
parent 8ecbb5f152
commit 20753e1b53
3 changed files with 45 additions and 2 deletions

View File

@ -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

View File

@ -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();

View File

@ -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(())
}