cleanup: move rrd cache related code into extra file
This commit is contained in:
parent
ddafb28572
commit
fae4f6c509
@ -9,7 +9,7 @@ use pbs_api_types::{
|
||||
NODE_SCHEMA, RRDMode, RRDTimeFrame, PRIV_SYS_AUDIT,
|
||||
};
|
||||
|
||||
use crate::extract_rrd_data;
|
||||
use crate::rrd_cache::extract_rrd_data;
|
||||
|
||||
pub fn create_value_from_rrd(
|
||||
basedir: &str,
|
||||
|
@ -22,7 +22,7 @@ use pbs_datastore::DataStore;
|
||||
use pbs_config::CachedUserInfo;
|
||||
|
||||
use crate::tools::statistics::{linear_regression};
|
||||
use crate::extract_rrd_data;
|
||||
use crate::rrd_cache::extract_rrd_data;
|
||||
|
||||
#[api(
|
||||
returns: {
|
||||
|
@ -24,15 +24,14 @@ use proxmox_router::{RpcEnvironment, RpcEnvironmentType, UserInformation};
|
||||
|
||||
use pbs_tools::{task_log, task_warn};
|
||||
use pbs_datastore::DataStore;
|
||||
use proxmox_rrd::rrd::DST;
|
||||
|
||||
use proxmox_rest_server::{
|
||||
rotate_task_log_archive, extract_cookie , AuthError, ApiConfig, RestServer, RestEnvironment,
|
||||
ServerAdapter, WorkerTask, cleanup_old_tasks,
|
||||
};
|
||||
|
||||
use proxmox_backup::rrd_cache::{ rrd_update_gauge, rrd_update_derive, initialize_rrd_cache};
|
||||
use proxmox_backup::{
|
||||
get_rrd_cache, initialize_rrd_cache,
|
||||
server::{
|
||||
auth::check_pbs_auth,
|
||||
jobstate::{
|
||||
@ -903,22 +902,6 @@ async fn run_stat_generator() {
|
||||
|
||||
}
|
||||
|
||||
fn rrd_update_gauge(name: &str, value: f64) {
|
||||
if let Ok(rrd_cache) = get_rrd_cache() {
|
||||
if let Err(err) = rrd_cache.update_value(name, value, DST::Gauge) {
|
||||
eprintln!("rrd::update_value '{}' failed - {}", name, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn rrd_update_derive(name: &str, value: f64) {
|
||||
if let Ok(rrd_cache) = get_rrd_cache() {
|
||||
if let Err(err) = rrd_cache.update_value(name, value, DST::Derive) {
|
||||
eprintln!("rrd::update_value '{}' failed - {}", name, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn generate_host_stats() {
|
||||
match tokio::task::spawn_blocking(generate_host_stats_sync).await {
|
||||
Ok(()) => (),
|
||||
|
75
src/lib.rs
75
src/lib.rs
@ -5,15 +5,8 @@
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use once_cell::sync::OnceCell;
|
||||
use anyhow::{format_err, Error};
|
||||
|
||||
use proxmox::tools::fs::CreateOptions;
|
||||
|
||||
use pbs_api_types::{RRDMode, RRDTimeFrame};
|
||||
use pbs_buildcfg::configdir;
|
||||
use pbs_tools::cert::CertInfo;
|
||||
use proxmox_rrd::{rrd::CF, RRDCache};
|
||||
|
||||
#[macro_use]
|
||||
pub mod tools;
|
||||
@ -38,73 +31,9 @@ pub mod acme;
|
||||
|
||||
pub mod client_helpers;
|
||||
|
||||
pub mod rrd_cache;
|
||||
|
||||
/// Get the server's certificate info (from `proxy.pem`).
|
||||
pub fn cert_info() -> Result<CertInfo, anyhow::Error> {
|
||||
CertInfo::from_path(PathBuf::from(configdir!("/proxy.pem")))
|
||||
}
|
||||
|
||||
pub static RRD_CACHE: OnceCell<RRDCache> = OnceCell::new();
|
||||
|
||||
/// Get the RRD cache instance
|
||||
pub fn get_rrd_cache() -> Result<&'static RRDCache, Error> {
|
||||
RRD_CACHE.get().ok_or_else(|| format_err!("RRD cache not initialized!"))
|
||||
}
|
||||
|
||||
/// Initialize the RRD cache instance
|
||||
///
|
||||
/// Note: Only a single process must do this (proxmox-backup-proxy)
|
||||
pub fn initialize_rrd_cache() -> Result<&'static RRDCache, Error> {
|
||||
|
||||
let backup_user = pbs_config::backup_user()?;
|
||||
|
||||
let file_options = CreateOptions::new()
|
||||
.owner(backup_user.uid)
|
||||
.group(backup_user.gid);
|
||||
|
||||
let dir_options = CreateOptions::new()
|
||||
.owner(backup_user.uid)
|
||||
.group(backup_user.gid);
|
||||
|
||||
let apply_interval = 30.0*60.0; // 30 minutes
|
||||
|
||||
let cache = RRDCache::new(
|
||||
"/var/lib/proxmox-backup/rrdb",
|
||||
Some(file_options),
|
||||
Some(dir_options),
|
||||
apply_interval,
|
||||
)?;
|
||||
|
||||
RRD_CACHE.set(cache)
|
||||
.map_err(|_| format_err!("RRD cache already initialized!"))?;
|
||||
|
||||
Ok(RRD_CACHE.get().unwrap())
|
||||
}
|
||||
|
||||
/// Extracts data for the specified time frame from from RRD cache
|
||||
pub fn extract_rrd_data(
|
||||
basedir: &str,
|
||||
name: &str,
|
||||
timeframe: RRDTimeFrame,
|
||||
mode: RRDMode,
|
||||
) -> Result<Option<(u64, u64, Vec<Option<f64>>)>, Error> {
|
||||
|
||||
let end = proxmox_time::epoch_f64() as u64;
|
||||
|
||||
let (start, resolution) = match timeframe {
|
||||
RRDTimeFrame::Hour => (end - 3600, 60),
|
||||
RRDTimeFrame::Day => (end - 3600*24, 60),
|
||||
RRDTimeFrame::Week => (end - 3600*24*7, 30*60),
|
||||
RRDTimeFrame::Month => (end - 3600*24*30, 30*60),
|
||||
RRDTimeFrame::Year => (end - 3600*24*365, 6*60*60),
|
||||
RRDTimeFrame::Decade => (end - 10*3600*24*366, 7*86400),
|
||||
};
|
||||
|
||||
let cf = match mode {
|
||||
RRDMode::Max => CF::Maximum,
|
||||
RRDMode::Average => CF::Average,
|
||||
};
|
||||
|
||||
let rrd_cache = get_rrd_cache()?;
|
||||
|
||||
rrd_cache.extract_cached_data(basedir, name, cf, resolution, Some(start), Some(end))
|
||||
}
|
||||
|
92
src/rrd_cache.rs
Normal file
92
src/rrd_cache.rs
Normal file
@ -0,0 +1,92 @@
|
||||
use anyhow::{format_err, Error};
|
||||
use once_cell::sync::OnceCell;
|
||||
|
||||
use proxmox::tools::fs::CreateOptions;
|
||||
use proxmox_rrd::RRDCache;
|
||||
use proxmox_rrd::rrd::{DST, CF};
|
||||
|
||||
use pbs_api_types::{RRDMode, RRDTimeFrame};
|
||||
|
||||
pub static RRD_CACHE: OnceCell<RRDCache> = OnceCell::new();
|
||||
|
||||
/// Get the RRD cache instance
|
||||
pub fn get_rrd_cache() -> Result<&'static RRDCache, Error> {
|
||||
RRD_CACHE.get().ok_or_else(|| format_err!("RRD cache not initialized!"))
|
||||
}
|
||||
|
||||
/// Initialize the RRD cache instance
|
||||
///
|
||||
/// Note: Only a single process must do this (proxmox-backup-proxy)
|
||||
pub fn initialize_rrd_cache() -> Result<&'static RRDCache, Error> {
|
||||
|
||||
let backup_user = pbs_config::backup_user()?;
|
||||
|
||||
let file_options = CreateOptions::new()
|
||||
.owner(backup_user.uid)
|
||||
.group(backup_user.gid);
|
||||
|
||||
let dir_options = CreateOptions::new()
|
||||
.owner(backup_user.uid)
|
||||
.group(backup_user.gid);
|
||||
|
||||
let apply_interval = 30.0*60.0; // 30 minutes
|
||||
|
||||
let cache = RRDCache::new(
|
||||
"/var/lib/proxmox-backup/rrdb",
|
||||
Some(file_options),
|
||||
Some(dir_options),
|
||||
apply_interval,
|
||||
)?;
|
||||
|
||||
RRD_CACHE.set(cache)
|
||||
.map_err(|_| format_err!("RRD cache already initialized!"))?;
|
||||
|
||||
Ok(RRD_CACHE.get().unwrap())
|
||||
}
|
||||
|
||||
/// Extracts data for the specified time frame from from RRD cache
|
||||
pub fn extract_rrd_data(
|
||||
basedir: &str,
|
||||
name: &str,
|
||||
timeframe: RRDTimeFrame,
|
||||
mode: RRDMode,
|
||||
) -> Result<Option<(u64, u64, Vec<Option<f64>>)>, Error> {
|
||||
|
||||
let end = proxmox_time::epoch_f64() as u64;
|
||||
|
||||
let (start, resolution) = match timeframe {
|
||||
RRDTimeFrame::Hour => (end - 3600, 60),
|
||||
RRDTimeFrame::Day => (end - 3600*24, 60),
|
||||
RRDTimeFrame::Week => (end - 3600*24*7, 30*60),
|
||||
RRDTimeFrame::Month => (end - 3600*24*30, 30*60),
|
||||
RRDTimeFrame::Year => (end - 3600*24*365, 6*60*60),
|
||||
RRDTimeFrame::Decade => (end - 10*3600*24*366, 7*86400),
|
||||
};
|
||||
|
||||
let cf = match mode {
|
||||
RRDMode::Max => CF::Maximum,
|
||||
RRDMode::Average => CF::Average,
|
||||
};
|
||||
|
||||
let rrd_cache = get_rrd_cache()?;
|
||||
|
||||
rrd_cache.extract_cached_data(basedir, name, cf, resolution, Some(start), Some(end))
|
||||
}
|
||||
|
||||
/// Update RRD Gauge values
|
||||
pub fn rrd_update_gauge(name: &str, value: f64) {
|
||||
if let Ok(rrd_cache) = get_rrd_cache() {
|
||||
if let Err(err) = rrd_cache.update_value(name, value, DST::Gauge) {
|
||||
log::error!("rrd::update_value '{}' failed - {}", name, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Update RRD Derive values
|
||||
pub fn rrd_update_derive(name: &str, value: f64) {
|
||||
if let Ok(rrd_cache) = get_rrd_cache() {
|
||||
if let Err(err) = rrd_cache.update_value(name, value, DST::Derive) {
|
||||
log::error!("rrd::update_value '{}' failed - {}", name, err);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user