add experimental rrd api to get cpu stats
This commit is contained in:
		@ -9,11 +9,13 @@ mod syslog;
 | 
			
		||||
mod journal;
 | 
			
		||||
mod services;
 | 
			
		||||
mod status;
 | 
			
		||||
mod rrd;
 | 
			
		||||
 | 
			
		||||
pub const SUBDIRS: SubdirMap = &[
 | 
			
		||||
    ("dns", &dns::ROUTER),
 | 
			
		||||
    ("journal", &journal::ROUTER),
 | 
			
		||||
    ("network", &network::ROUTER),
 | 
			
		||||
    ("rrd", &rrd::ROUTER),
 | 
			
		||||
    ("services", &services::ROUTER),
 | 
			
		||||
    ("status", &status::ROUTER),
 | 
			
		||||
    ("syslog", &syslog::ROUTER),
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										34
									
								
								src/api2/node/rrd.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/api2/node/rrd.rs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,34 @@
 | 
			
		||||
use anyhow::Error;
 | 
			
		||||
use serde_json::Value;
 | 
			
		||||
 | 
			
		||||
use proxmox::api::{api, Router};
 | 
			
		||||
 | 
			
		||||
use crate::api2::types::*;
 | 
			
		||||
 | 
			
		||||
#[api(
 | 
			
		||||
    input: {
 | 
			
		||||
        properties: {
 | 
			
		||||
            node: {
 | 
			
		||||
                schema: NODE_SCHEMA,
 | 
			
		||||
            },
 | 
			
		||||
            timeframe: {
 | 
			
		||||
                type: RRDTimeFrameResolution,
 | 
			
		||||
            },
 | 
			
		||||
            cf: {
 | 
			
		||||
                type: RRDMode,
 | 
			
		||||
            },
 | 
			
		||||
        },
 | 
			
		||||
    },
 | 
			
		||||
)]
 | 
			
		||||
/// Read CPU stats
 | 
			
		||||
fn get_cpu_stats(
 | 
			
		||||
    timeframe: RRDTimeFrameResolution,
 | 
			
		||||
    cf: RRDMode,
 | 
			
		||||
    _param: Value,
 | 
			
		||||
) -> Result<Value, Error> {
 | 
			
		||||
 | 
			
		||||
    crate::rrd::extract_data("host/cpu", timeframe, cf)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub const ROUTER: Router = Router::new()
 | 
			
		||||
    .get(&API_METHOD_GET_CPU_STATS);
 | 
			
		||||
@ -878,3 +878,31 @@ fn test_proxmox_user_id_schema() -> Result<(), anyhow::Error> {
 | 
			
		||||
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[api()]
 | 
			
		||||
#[derive(Copy, Clone, Serialize, Deserialize)]
 | 
			
		||||
#[serde(rename_all = "UPPERCASE")]
 | 
			
		||||
pub enum RRDMode {
 | 
			
		||||
    /// Maximum
 | 
			
		||||
    Max,
 | 
			
		||||
    /// Average
 | 
			
		||||
    Average,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#[api()]
 | 
			
		||||
#[repr(u64)]
 | 
			
		||||
#[derive(Copy, Clone, Serialize, Deserialize)]
 | 
			
		||||
#[serde(rename_all = "lowercase")]
 | 
			
		||||
pub enum RRDTimeFrameResolution {
 | 
			
		||||
    ///  1 min => last 70 minutes
 | 
			
		||||
    Hour = 60,
 | 
			
		||||
    /// 30 min => last 35 hours
 | 
			
		||||
    Day = 60*30,
 | 
			
		||||
    /// 3 hours => about 8 days
 | 
			
		||||
    Week = 60*180,
 | 
			
		||||
    /// 12 hours => last 35 days
 | 
			
		||||
    Month = 60*720,
 | 
			
		||||
    /// 1 week => last 490 days
 | 
			
		||||
    Year = 60*10080,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -9,6 +9,8 @@ use serde_json::Value;
 | 
			
		||||
 | 
			
		||||
use proxmox::tools::fs::{create_path, CreateOptions};
 | 
			
		||||
 | 
			
		||||
use crate::api2::types::{RRDMode, RRDTimeFrameResolution};
 | 
			
		||||
 | 
			
		||||
use super::*;
 | 
			
		||||
 | 
			
		||||
const PBS_RRD_BASEDIR: &str = "/var/lib/proxmox-backup/rrdb";
 | 
			
		||||
 | 
			
		||||
@ -4,23 +4,10 @@ use std::path::Path;
 | 
			
		||||
use anyhow::{bail, Error};
 | 
			
		||||
use serde_json::{json, Value};
 | 
			
		||||
 | 
			
		||||
use crate::api2::types::{RRDMode, RRDTimeFrameResolution};
 | 
			
		||||
 | 
			
		||||
const RRD_DATA_ENTRIES: usize = 70;
 | 
			
		||||
 | 
			
		||||
#[derive(Copy, Clone)]
 | 
			
		||||
pub enum RRDMode {
 | 
			
		||||
    Max,
 | 
			
		||||
    Average,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[repr(u64)]
 | 
			
		||||
#[derive(Copy, Clone)]
 | 
			
		||||
pub enum RRDTimeFrameResolution {
 | 
			
		||||
    Hour = 60,       // 1 min => last 70 minutes
 | 
			
		||||
    Day = 60*30,     // 30 min => last 35 hours
 | 
			
		||||
    Week = 60*180,   // 3 hours => about 8 days
 | 
			
		||||
    Month = 60*720,  // 12 hours => last 35 days
 | 
			
		||||
    Year = 60*10080, // 1 week => last 490 days
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[repr(C)]
 | 
			
		||||
#[derive(Default, Copy, Clone)]
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user