diff --git a/src/api2/node.rs b/src/api2/node.rs index 5d5a9af3..ff90d284 100644 --- a/src/api2/node.rs +++ b/src/api2/node.rs @@ -8,12 +8,14 @@ mod dns; mod syslog; mod journal; mod services; +mod status; pub const SUBDIRS: SubdirMap = &[ ("dns", &dns::ROUTER), ("journal", &journal::ROUTER), ("network", &network::ROUTER), ("services", &services::ROUTER), + ("status", &status::ROUTER), ("syslog", &syslog::ROUTER), ("tasks", &tasks::ROUTER), ("time", &time::ROUTER), diff --git a/src/api2/node/status.rs b/src/api2/node/status.rs new file mode 100644 index 00000000..7013cf4b --- /dev/null +++ b/src/api2/node/status.rs @@ -0,0 +1,77 @@ +use failure::*; +use serde_json::{json, Value}; + +use proxmox::sys::linux::procfs; + +use proxmox::api::{api, ApiMethod, Router, RpcEnvironment, SubdirMap}; +use proxmox::api::list_subdirs_api_method; + +use crate::api2::types::*; + +#[api( + input: { + properties: { + node: { + schema: NODE_SCHEMA, + }, + }, + }, + returns: { + type: Object, + description: "Returns node memory, CPU and (root) disk usage", + properties: { + memory: { + type: Object, + description: "node memory usage counters", + properties: { + total: { + description: "total memory", + type: Integer, + }, + used: { + description: "total memory", + type: Integer, + }, + free: { + description: "free memory", + type: Integer, + }, + }, + }, + cpu: { + type: Number, + description: "Total CPU usage since last query.", + optional: true, + }, + } + } +)] +/// Read node memory, CPU and (root) disk usage +fn get_usage( + _param: Value, + _info: &ApiMethod, + _rpcenv: &mut dyn RpcEnvironment, +) -> Result { + + let meminfo: procfs::ProcFsMemInfo = procfs::read_meminfo()?; + let kstat: procfs::ProcFsStat = procfs::read_proc_stat()?; + + Ok(json!({ + "memory": { + "total": meminfo.memtotal, + "used": meminfo.memused, + "free": meminfo.memfree, + }, + "cpu": kstat.cpu, + })) +} + +pub const USAGE_ROUTER: Router = Router::new() + .get(&API_METHOD_GET_USAGE); + +pub const SUBDIRS: SubdirMap = &[ + ("usage", &USAGE_ROUTER), +]; +pub const ROUTER: Router = Router::new() + .get(&list_subdirs_api_method!(SUBDIRS)) + .subdirs(SUBDIRS);