proxmox-backup/src/api2/node/rrd.rs

93 lines
2.2 KiB
Rust
Raw Normal View History

use anyhow::Error;
use serde_json::{Value, json};
use proxmox::api::{api, Permission, Router};
use pbs_api_types::{
NODE_SCHEMA, RRDMode, RRDTimeFrameResolution, PRIV_SYS_AUDIT,
};
2021-10-06 10:19:54 +00:00
use proxmox_rrd::rrd::RRD_DATA_ENTRIES;
2021-10-06 05:06:17 +00:00
use crate::RRD_CACHE;
pub fn create_value_from_rrd(
basedir: &str,
list: &[&str],
timeframe: RRDTimeFrameResolution,
cf: RRDMode,
) -> Result<Value, Error> {
let mut result = Vec::new();
let now = proxmox::tools::time::epoch_f64();
for name in list {
2021-10-06 05:06:17 +00:00
let (start, reso, list) = match RRD_CACHE.extract_cached_data(basedir, name, now, timeframe, cf) {
Some(result) => result,
None => continue,
};
let mut t = start;
for index in 0..RRD_DATA_ENTRIES {
if result.len() <= index {
if let Some(value) = list[index] {
result.push(json!({ "time": t, *name: value }));
} else {
result.push(json!({ "time": t }));
}
} else if let Some(value) = list[index] {
result[index][name] = value.into();
}
t += reso;
}
}
Ok(result.into())
}
#[api(
input: {
properties: {
node: {
schema: NODE_SCHEMA,
},
timeframe: {
type: RRDTimeFrameResolution,
},
cf: {
type: RRDMode,
},
},
},
access: {
permission: &Permission::Privilege(&["system", "status"], PRIV_SYS_AUDIT, false),
},
)]
/// Read node stats
fn get_node_stats(
timeframe: RRDTimeFrameResolution,
cf: RRDMode,
_param: Value,
) -> Result<Value, Error> {
create_value_from_rrd(
"host",
&[
"cpu", "iowait",
"memtotal", "memused",
"swaptotal", "swapused",
"netin", "netout",
"loadavg",
"total", "used",
2020-05-28 17:11:37 +00:00
"read_ios", "read_bytes",
"write_ios", "write_bytes",
"io_ticks",
],
timeframe,
cf,
)
}
pub const ROUTER: Router = Router::new()
.get(&API_METHOD_GET_NODE_STATS);