From 33070956aff77218614b03fd1d2cb4085c97ed2e Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Tue, 9 Jun 2020 10:01:09 +0200 Subject: [PATCH] let disk_usage return StorageStatus and use it for datastores/nodes disk_usage returned the same values as defined in StorageStatus, so simply use that with that we can replace the logic of the datastore status with that function and also use it for root disk usage of the nodes Signed-off-by: Dominik Csapak --- src/api2/admin/datastore.rs | 19 +------------------ src/api2/node/status.rs | 7 +++++++ src/bin/proxmox-backup-proxy.rs | 6 +++--- src/tools/disks.rs | 10 +++++++--- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/api2/admin/datastore.rs b/src/api2/admin/datastore.rs index d9d9c5ca..222e4d21 100644 --- a/src/api2/admin/datastore.rs +++ b/src/api2/admin/datastore.rs @@ -382,25 +382,8 @@ pub fn status( _info: &ApiMethod, _rpcenv: &mut dyn RpcEnvironment, ) -> Result { - 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(StorageStatus { - total: stat.f_blocks*bsize, - used: (stat.f_blocks-stat.f_bfree)*bsize, - avail: stat.f_bavail*bsize, - }) + crate::tools::disks::disk_usage(&datastore.base_path()) } #[macro_export] diff --git a/src/api2/node/status.rs b/src/api2/node/status.rs index 5af49708..0fe9ef4a 100644 --- a/src/api2/node/status.rs +++ b/src/api2/node/status.rs @@ -1,4 +1,5 @@ use std::process::Command; +use std::path::Path; use anyhow::{Error, format_err, bail}; use serde_json::{json, Value}; @@ -60,6 +61,7 @@ fn get_usage( let meminfo: procfs::ProcFsMemInfo = procfs::read_meminfo()?; let kstat: procfs::ProcFsStat = procfs::read_proc_stat()?; + let disk_usage = crate::tools::disks::disk_usage(Path::new("/"))?; Ok(json!({ "memory": { @@ -68,6 +70,11 @@ fn get_usage( "free": meminfo.memfree, }, "cpu": kstat.cpu, + "root": { + "total": disk_usage.total, + "used": disk_usage.used, + "free": disk_usage.avail, + } })) } diff --git a/src/bin/proxmox-backup-proxy.rs b/src/bin/proxmox-backup-proxy.rs index b9f926ed..a6efe3cc 100644 --- a/src/bin/proxmox-backup-proxy.rs +++ b/src/bin/proxmox-backup-proxy.rs @@ -711,11 +711,11 @@ async fn generate_host_stats(save: bool) { fn gather_disk_stats(disk_manager: Arc, path: &Path, rrd_prefix: &str, save: bool) { match proxmox_backup::tools::disks::disk_usage(path) { - Ok((total, used, _avail)) => { + Ok(status) => { let rrd_key = format!("{}/total", rrd_prefix); - rrd_update_gauge(&rrd_key, total as f64, save); + rrd_update_gauge(&rrd_key, status.total as f64, save); let rrd_key = format!("{}/used", rrd_prefix); - rrd_update_gauge(&rrd_key, used as f64, save); + rrd_update_gauge(&rrd_key, status.used as f64, save); } Err(err) => { eprintln!("read disk_usage on {:?} failed - {}", path, err); diff --git a/src/tools/disks.rs b/src/tools/disks.rs index 3045c14e..70dc05c4 100644 --- a/src/tools/disks.rs +++ b/src/tools/disks.rs @@ -19,7 +19,7 @@ use proxmox::sys::linux::procfs::{MountInfo, mountinfo::Device}; use proxmox::{io_bail, io_format_err}; use proxmox::api::api; -use crate::api2::types::BLOCKDEVICE_NAME_REGEX; +use crate::api2::types::{BLOCKDEVICE_NAME_REGEX, StorageStatus}; mod zfs; pub use zfs::*; @@ -511,7 +511,7 @@ impl Disk { } /// Returns disk usage information (total, used, avail) -pub fn disk_usage(path: &std::path::Path) -> Result<(u64, u64, u64), Error> { +pub fn disk_usage(path: &std::path::Path) -> Result { let mut stat: libc::statfs64 = unsafe { std::mem::zeroed() }; @@ -522,7 +522,11 @@ pub fn disk_usage(path: &std::path::Path) -> Result<(u64, u64, u64), Error> { let bsize = stat.f_bsize as u64; - Ok((stat.f_blocks*bsize, (stat.f_blocks-stat.f_bfree)*bsize, stat.f_bavail*bsize)) + Ok(StorageStatus{ + total: stat.f_blocks*bsize, + used: (stat.f_blocks-stat.f_bfree)*bsize, + avail: stat.f_bavail*bsize, + }) } #[api()]