From a2f862eed64ce046cff823eaa21c7914fb467ec3 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sat, 23 May 2020 11:10:02 +0200 Subject: [PATCH] add experimental rrd api to get cpu stats --- src/api2/node.rs | 2 ++ src/api2/node/rrd.rs | 34 ++++++++++++++++++++++++++++++++++ src/api2/types.rs | 28 ++++++++++++++++++++++++++++ src/rrd/cache.rs | 2 ++ src/rrd/rrd.rs | 17 ++--------------- 5 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 src/api2/node/rrd.rs diff --git a/src/api2/node.rs b/src/api2/node.rs index 36d92119..0a6d894e 100644 --- a/src/api2/node.rs +++ b/src/api2/node.rs @@ -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), diff --git a/src/api2/node/rrd.rs b/src/api2/node/rrd.rs new file mode 100644 index 00000000..acfd072b --- /dev/null +++ b/src/api2/node/rrd.rs @@ -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 { + + crate::rrd::extract_data("host/cpu", timeframe, cf) +} + +pub const ROUTER: Router = Router::new() + .get(&API_METHOD_GET_CPU_STATS); diff --git a/src/api2/types.rs b/src/api2/types.rs index 85a647dc..5ce38838 100644 --- a/src/api2/types.rs +++ b/src/api2/types.rs @@ -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, +} diff --git a/src/rrd/cache.rs b/src/rrd/cache.rs index 9f8ef9f4..6baa2e33 100644 --- a/src/rrd/cache.rs +++ b/src/rrd/cache.rs @@ -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"; diff --git a/src/rrd/rrd.rs b/src/rrd/rrd.rs index 4fcf64f8..681d5be1 100644 --- a/src/rrd/rrd.rs +++ b/src/rrd/rrd.rs @@ -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)]