]>
Commit | Line | Data |
---|---|---|
a2f862ee | 1 | use anyhow::Error; |
431cc7b1 | 2 | use serde_json::{Value, json}; |
a2f862ee | 3 | |
463c0346 | 4 | use proxmox::api::{api, Permission, Router}; |
a2f862ee DM |
5 | |
6 | use crate::api2::types::*; | |
463c0346 | 7 | use crate::config::acl::PRIV_SYS_AUDIT; |
431cc7b1 DC |
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(); | |
6a7be83e | 18 | let now = proxmox::tools::time::epoch_f64(); |
431cc7b1 DC |
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 | } | |
20813274 WB |
34 | } else if let Some(value) = list[index] { |
35 | result[index][name] = value.into(); | |
431cc7b1 DC |
36 | } |
37 | t += reso; | |
38 | } | |
39 | } | |
40 | ||
41 | Ok(result.into()) | |
42 | } | |
a2f862ee DM |
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 | }, | |
463c0346 FG |
58 | access: { |
59 | permission: &Permission::Privilege(&["system", "status"], PRIV_SYS_AUDIT, false), | |
60 | }, | |
a2f862ee | 61 | )] |
a4a3f7ca DM |
62 | /// Read node stats |
63 | fn get_node_stats( | |
a2f862ee DM |
64 | timeframe: RRDTimeFrameResolution, |
65 | cf: RRDMode, | |
66 | _param: Value, | |
67 | ) -> Result<Value, Error> { | |
68 | ||
431cc7b1 | 69 | create_value_from_rrd( |
a4a3f7ca | 70 | "host", |
3e930f2b DM |
71 | &[ |
72 | "cpu", "iowait", | |
73 | "memtotal", "memused", | |
74 | "swaptotal", "swapused", | |
75 | "netin", "netout", | |
d8dc2819 | 76 | "loadavg", |
91e5bb49 | 77 | "total", "used", |
c94e1f65 DM |
78 | "read_ios", "read_bytes", |
79 | "write_ios", "write_bytes", | |
80 | "io_ticks", | |
91e5bb49 | 81 | ], |
a4a3f7ca DM |
82 | timeframe, |
83 | cf, | |
84 | ) | |
a2f862ee DM |
85 | } |
86 | ||
87 | pub const ROUTER: Router = Router::new() | |
a4a3f7ca | 88 | .get(&API_METHOD_GET_NODE_STATS); |