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