From 0eecf38fbf65e7c323e5e763475668d160e312f6 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 16 Jul 2019 13:34:38 +0200 Subject: [PATCH] src/api2/admin/datastore.rs: add status api call --- src/api2/admin/datastore.rs | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index 6bc63afe..6c6d81af 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -160,6 +160,46 @@ fn list_snapshots ( Ok(json!(snapshots)) } +fn status( + param: Value, + _info: &ApiMethod, + _rpcenv: &mut dyn RpcEnvironment, +) -> Result { + + let store = param["store"].as_str().unwrap(); + + let datastore = DataStore::lookup_datastore(store)?; + + let base_path = datastore.base_path(); + + let mut stat: libc::statfs64 = unsafe { std::mem::zeroed() }; + + use nix::NixPath; + + let res = base_path.with_nix_path(|cstr| unsafe { libc::statfs64(cstr.as_ptr(), &mut stat) })?; + nix::errno::Errno::result(res)?; + + let bsize = stat.f_bsize as u64; + Ok(json!({ + "total": stat.f_blocks*bsize, + "used": (stat.f_blocks-stat.f_bfree)*bsize, + "avail": stat.f_bavail*bsize, + })) +} + +fn api_method_status() -> ApiMethod { + ApiMethod::new( + status, + add_common_prune_prameters( + ObjectSchema::new("Get datastore status.") + .required( + "store", + StringSchema::new("Datastore name.") + ) + ) + ) +} + fn prune( param: Value, _info: &ApiMethod, @@ -527,6 +567,11 @@ pub fn router() -> Router { Router::new() .post(api_method_prune()) ) + .subdir( + "status", + Router::new() + .get(api_method_status()) + ) .list_subdirs();