From 2337df7b3dc7e4e0bec727b1a6bfd5445747fcb4 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Wed, 18 Dec 2019 17:24:04 +0100 Subject: [PATCH] api2: add very basic 'status/usage' endpoint for nodes For returning the nodes basic "usage status", for now one gets memory and CPU utilization. Signed-off-by: Thomas Lamprecht --- src/api2/node.rs | 2 ++ src/api2/node/status.rs | 77 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/api2/node/status.rs 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);