]> git.proxmox.com Git - proxmox-backup.git/blob - src/api2/node/status.rs
switch from failure to anyhow
[proxmox-backup.git] / src / api2 / node / status.rs
1 use anyhow::{Error};
2 use serde_json::{json, Value};
3
4 use proxmox::sys::linux::procfs;
5
6 use proxmox::api::{api, ApiMethod, Router, RpcEnvironment, SubdirMap, Permission};
7 use proxmox::list_subdirs_api_method;
8
9 use crate::api2::types::*;
10 use crate::config::acl::PRIV_SYS_AUDIT;
11
12 #[api(
13 input: {
14 properties: {
15 node: {
16 schema: NODE_SCHEMA,
17 },
18 },
19 },
20 returns: {
21 type: Object,
22 description: "Returns node memory, CPU and (root) disk usage",
23 properties: {
24 memory: {
25 type: Object,
26 description: "node memory usage counters",
27 properties: {
28 total: {
29 description: "total memory",
30 type: Integer,
31 },
32 used: {
33 description: "total memory",
34 type: Integer,
35 },
36 free: {
37 description: "free memory",
38 type: Integer,
39 },
40 },
41 },
42 cpu: {
43 type: Number,
44 description: "Total CPU usage since last query.",
45 optional: true,
46 },
47 }
48 },
49 access: {
50 permission: &Permission::Privilege(&[], PRIV_SYS_AUDIT, false),
51 },
52 )]
53 /// Read node memory, CPU and (root) disk usage
54 fn get_usage(
55 _param: Value,
56 _info: &ApiMethod,
57 _rpcenv: &mut dyn RpcEnvironment,
58 ) -> Result<Value, Error> {
59
60 let meminfo: procfs::ProcFsMemInfo = procfs::read_meminfo()?;
61 let kstat: procfs::ProcFsStat = procfs::read_proc_stat()?;
62
63 Ok(json!({
64 "memory": {
65 "total": meminfo.memtotal,
66 "used": meminfo.memused,
67 "free": meminfo.memfree,
68 },
69 "cpu": kstat.cpu,
70 }))
71 }
72
73 pub const USAGE_ROUTER: Router = Router::new()
74 .get(&API_METHOD_GET_USAGE);
75
76 pub const SUBDIRS: SubdirMap = &[
77 ("usage", &USAGE_ROUTER),
78 ];
79 pub const ROUTER: Router = Router::new()
80 .get(&list_subdirs_api_method!(SUBDIRS))
81 .subdirs(SUBDIRS);