2020-05-23 09:10:02 +00:00
|
|
|
use anyhow::Error;
|
2020-06-10 10:02:57 +00:00
|
|
|
use serde_json::{Value, json};
|
2020-05-23 09:10:02 +00:00
|
|
|
|
|
|
|
use proxmox::api::{api, Router};
|
|
|
|
|
|
|
|
use crate::api2::types::*;
|
2020-06-10 10:02:57 +00:00
|
|
|
use crate::rrd::{extract_cached_data, RRD_DATA_ENTRIES};
|
|
|
|
|
|
|
|
pub fn create_value_from_rrd(
|
|
|
|
basedir: &str,
|
|
|
|
list: &[&str],
|
|
|
|
timeframe: RRDTimeFrameResolution,
|
|
|
|
cf: RRDMode,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
|
|
|
let mut result = Vec::new();
|
2020-09-12 13:10:47 +00:00
|
|
|
let now = proxmox::tools::time::epoch_f64();
|
2020-06-10 10:02:57 +00:00
|
|
|
|
|
|
|
for name in list {
|
|
|
|
let (start, reso, list) = match 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())
|
|
|
|
}
|
2020-05-23 09:10:02 +00:00
|
|
|
|
|
|
|
#[api(
|
|
|
|
input: {
|
|
|
|
properties: {
|
|
|
|
node: {
|
|
|
|
schema: NODE_SCHEMA,
|
|
|
|
},
|
|
|
|
timeframe: {
|
|
|
|
type: RRDTimeFrameResolution,
|
|
|
|
},
|
|
|
|
cf: {
|
|
|
|
type: RRDMode,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)]
|
2020-05-23 12:03:44 +00:00
|
|
|
/// Read node stats
|
|
|
|
fn get_node_stats(
|
2020-05-23 09:10:02 +00:00
|
|
|
timeframe: RRDTimeFrameResolution,
|
|
|
|
cf: RRDMode,
|
|
|
|
_param: Value,
|
|
|
|
) -> Result<Value, Error> {
|
|
|
|
|
2020-06-10 10:02:57 +00:00
|
|
|
create_value_from_rrd(
|
2020-05-23 12:03:44 +00:00
|
|
|
"host",
|
2020-05-25 09:10:44 +00:00
|
|
|
&[
|
|
|
|
"cpu", "iowait",
|
|
|
|
"memtotal", "memused",
|
|
|
|
"swaptotal", "swapused",
|
|
|
|
"netin", "netout",
|
2020-05-25 09:49:30 +00:00
|
|
|
"loadavg",
|
2020-05-28 10:30:54 +00:00
|
|
|
"total", "used",
|
2020-05-28 17:11:37 +00:00
|
|
|
"read_ios", "read_bytes",
|
|
|
|
"write_ios", "write_bytes",
|
|
|
|
"io_ticks",
|
2020-05-28 10:30:54 +00:00
|
|
|
],
|
2020-05-23 12:03:44 +00:00
|
|
|
timeframe,
|
|
|
|
cf,
|
|
|
|
)
|
2020-05-23 09:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub const ROUTER: Router = Router::new()
|
2020-05-23 12:03:44 +00:00
|
|
|
.get(&API_METHOD_GET_NODE_STATS);
|