From dd15c0aa3b78daf47239e29a29986d16c70e1ae9 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Mon, 25 May 2020 11:10:07 +0200 Subject: [PATCH] src/bin/proxmox-backup-proxy.rs: gather root disk stats --- src/bin/proxmox-backup-proxy.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/bin/proxmox-backup-proxy.rs b/src/bin/proxmox-backup-proxy.rs index 748958a7..347e6b7b 100644 --- a/src/bin/proxmox-backup-proxy.rs +++ b/src/bin/proxmox-backup-proxy.rs @@ -658,5 +658,35 @@ async fn generate_host_stats() { eprintln!("read_prox_net_dev failed - {}", err); } } + + match disk_usage(std::path::Path::new("/")) { + Ok((total, used, _avail)) => { + if let Err(err) = rrd::update_value("host/roottotal", total as f64, rrd::DST::Gauge) { + eprintln!("rrd::update_value 'host/roottotal' failed - {}", err); + } + if let Err(err) = rrd::update_value("host/rootused", used as f64, rrd::DST::Gauge) { + eprintln!("rrd::update_value 'host/rootused' failed - {}", err); + } + } + Err(err) => { + eprintln!("read root disk_usage failed - {}", err); + } + } + }); } + +// Returns (total, used, avail) +fn disk_usage(path: &std::path::Path) -> Result<(u64, u64, u64), Error> { + + let mut stat: libc::statfs64 = unsafe { std::mem::zeroed() }; + + use nix::NixPath; + + let res = 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((stat.f_blocks*bsize, (stat.f_blocks-stat.f_bfree)*bsize, stat.f_bavail*bsize)) +}