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 <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2019-12-18 17:24:04 +01:00
parent 6060a57578
commit 2337df7b3d
2 changed files with 79 additions and 0 deletions

View File

@ -8,12 +8,14 @@ mod dns;
mod syslog; mod syslog;
mod journal; mod journal;
mod services; mod services;
mod status;
pub const SUBDIRS: SubdirMap = &[ pub const SUBDIRS: SubdirMap = &[
("dns", &dns::ROUTER), ("dns", &dns::ROUTER),
("journal", &journal::ROUTER), ("journal", &journal::ROUTER),
("network", &network::ROUTER), ("network", &network::ROUTER),
("services", &services::ROUTER), ("services", &services::ROUTER),
("status", &status::ROUTER),
("syslog", &syslog::ROUTER), ("syslog", &syslog::ROUTER),
("tasks", &tasks::ROUTER), ("tasks", &tasks::ROUTER),
("time", &time::ROUTER), ("time", &time::ROUTER),

77
src/api2/node/status.rs Normal file
View File

@ -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<Value, Error> {
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);