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

107 lines
2.5 KiB
Rust
Raw Normal View History

use anyhow::{bail, Error};
use serde_json::{Value, json};
use std::collections::BTreeMap;
use proxmox_router::{Permission, Router};
use proxmox_schema::api;
use pbs_api_types::{
NODE_SCHEMA, RRDMode, RRDTimeFrameResolution, PRIV_SYS_AUDIT,
};
use crate::get_rrd_cache;
pub fn create_value_from_rrd(
basedir: &str,
list: &[&str],
timeframe: RRDTimeFrameResolution,
cf: RRDMode,
) -> Result<Value, Error> {
let mut result: Vec<Value> = Vec::new();
let now = proxmox_time::epoch_f64();
let rrd_cache = get_rrd_cache()?;
let mut timemap = BTreeMap::new();
let mut last_resolution = None;
for name in list {
let (start, reso, data) = match rrd_cache.extract_cached_data(basedir, name, now, timeframe, cf)? {
Some(result) => result,
None => continue,
};
if let Some(expected_resolution) = last_resolution {
if reso != expected_resolution {
bail!("got unexpected RRD resolution ({} != {})", reso, expected_resolution);
}
} else {
last_resolution = Some(reso);
}
let mut t = start;
for index in 0..data.len() {
let entry = timemap.entry(t).or_insert(json!({ "time": t }));
if let Some(value) = data[index] {
entry[*name] = value.into();
}
t += reso;
}
}
for item in timemap.values() {
result.push(item.clone());
}
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);