]> git.proxmox.com Git - proxmox-backup.git/blob - src/api2/node/rrd.rs
proxmox-rrd: improve developer docs
[proxmox-backup.git] / src / api2 / node / rrd.rs
1 use anyhow::Error;
2 use serde_json::{Value, json};
3
4 use proxmox::api::{api, Permission, Router};
5
6 use pbs_api_types::{
7 NODE_SCHEMA, RRDMode, RRDTimeFrameResolution, PRIV_SYS_AUDIT,
8 };
9
10 use proxmox_rrd::rrd::RRD_DATA_ENTRIES;
11
12 use crate::RRD_CACHE;
13
14 pub fn create_value_from_rrd(
15 basedir: &str,
16 list: &[&str],
17 timeframe: RRDTimeFrameResolution,
18 cf: RRDMode,
19 ) -> Result<Value, Error> {
20
21 let mut result = Vec::new();
22 let now = proxmox::tools::time::epoch_f64();
23
24 for name in list {
25 let (start, reso, list) = match RRD_CACHE.extract_cached_data(basedir, name, now, timeframe, cf) {
26 Some(result) => result,
27 None => continue,
28 };
29
30 let mut t = start;
31 for index in 0..RRD_DATA_ENTRIES {
32 if result.len() <= index {
33 if let Some(value) = list[index] {
34 result.push(json!({ "time": t, *name: value }));
35 } else {
36 result.push(json!({ "time": t }));
37 }
38 } else if let Some(value) = list[index] {
39 result[index][name] = value.into();
40 }
41 t += reso;
42 }
43 }
44
45 Ok(result.into())
46 }
47
48 #[api(
49 input: {
50 properties: {
51 node: {
52 schema: NODE_SCHEMA,
53 },
54 timeframe: {
55 type: RRDTimeFrameResolution,
56 },
57 cf: {
58 type: RRDMode,
59 },
60 },
61 },
62 access: {
63 permission: &Permission::Privilege(&["system", "status"], PRIV_SYS_AUDIT, false),
64 },
65 )]
66 /// Read node stats
67 fn get_node_stats(
68 timeframe: RRDTimeFrameResolution,
69 cf: RRDMode,
70 _param: Value,
71 ) -> Result<Value, Error> {
72
73 create_value_from_rrd(
74 "host",
75 &[
76 "cpu", "iowait",
77 "memtotal", "memused",
78 "swaptotal", "swapused",
79 "netin", "netout",
80 "loadavg",
81 "total", "used",
82 "read_ios", "read_bytes",
83 "write_ios", "write_bytes",
84 "io_ticks",
85 ],
86 timeframe,
87 cf,
88 )
89 }
90
91 pub const ROUTER: Router = Router::new()
92 .get(&API_METHOD_GET_NODE_STATS);