[pbs-devel] [PATCH proxmox-backup v2 05/13] metric collection: rrd: move rrd update function to rrd module

Lukas Wagner l.wagner at proxmox.com
Tue Oct 15 10:46:28 CEST 2024


Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
 src/server/metric_collection/mod.rs | 70 +---------------------------
 src/server/metric_collection/rrd.rs | 72 +++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+), 68 deletions(-)

diff --git a/src/server/metric_collection/mod.rs b/src/server/metric_collection/mod.rs
index 5a516564..2c4c93a8 100644
--- a/src/server/metric_collection/mod.rs
+++ b/src/server/metric_collection/mod.rs
@@ -17,7 +17,7 @@ use proxmox_sys::{
 
 use crate::tools::disks::{zfs_dataset_stats, BlockDevStat, DiskManage};
 
-use rrd::{initialize_rrd_cache, rrd_sync_journal, rrd_update_derive, rrd_update_gauge};
+use rrd::{initialize_rrd_cache, rrd_sync_journal};
 
 mod metric_server;
 pub mod rrd;
@@ -65,7 +65,7 @@ async fn run_stat_generator() {
         let rrd_future = tokio::task::spawn_blocking({
             let stats = Arc::clone(&stats);
             move || {
-                rrd_update_host_stats_sync(&stats.0, &stats.1, &stats.2);
+                rrd::rrd_update_host_stats_sync(&stats.0, &stats.1, &stats.2);
                 rrd_sync_journal();
             }
         });
@@ -193,72 +193,6 @@ fn collect_disk_stats_sync() -> (DiskStat, Vec<DiskStat>) {
     (root, datastores)
 }
 
-fn rrd_update_host_stats_sync(host: &HostStats, hostdisk: &DiskStat, datastores: &[DiskStat]) {
-    if let Some(stat) = &host.proc {
-        rrd_update_gauge("host/cpu", stat.cpu);
-        rrd_update_gauge("host/iowait", stat.iowait_percent);
-    }
-
-    if let Some(meminfo) = &host.meminfo {
-        rrd_update_gauge("host/memtotal", meminfo.memtotal as f64);
-        rrd_update_gauge("host/memused", meminfo.memused as f64);
-        rrd_update_gauge("host/swaptotal", meminfo.swaptotal as f64);
-        rrd_update_gauge("host/swapused", meminfo.swapused as f64);
-    }
-
-    if let Some(netdev) = &host.net {
-        use pbs_config::network::is_physical_nic;
-        let mut netin = 0;
-        let mut netout = 0;
-        for item in netdev {
-            if !is_physical_nic(&item.device) {
-                continue;
-            }
-            netin += item.receive;
-            netout += item.send;
-        }
-        rrd_update_derive("host/netin", netin as f64);
-        rrd_update_derive("host/netout", netout as f64);
-    }
-
-    if let Some(loadavg) = &host.load {
-        rrd_update_gauge("host/loadavg", loadavg.0);
-    }
-
-    rrd_update_disk_stat(hostdisk, "host");
-
-    for stat in datastores {
-        let rrd_prefix = format!("datastore/{}", stat.name);
-        rrd_update_disk_stat(stat, &rrd_prefix);
-    }
-}
-
-fn rrd_update_disk_stat(disk: &DiskStat, rrd_prefix: &str) {
-    if let Some(status) = &disk.usage {
-        let rrd_key = format!("{}/total", rrd_prefix);
-        rrd_update_gauge(&rrd_key, status.total as f64);
-        let rrd_key = format!("{}/used", rrd_prefix);
-        rrd_update_gauge(&rrd_key, status.used as f64);
-        let rrd_key = format!("{}/available", rrd_prefix);
-        rrd_update_gauge(&rrd_key, status.available as f64);
-    }
-
-    if let Some(stat) = &disk.dev {
-        let rrd_key = format!("{}/read_ios", rrd_prefix);
-        rrd_update_derive(&rrd_key, stat.read_ios as f64);
-        let rrd_key = format!("{}/read_bytes", rrd_prefix);
-        rrd_update_derive(&rrd_key, (stat.read_sectors * 512) as f64);
-
-        let rrd_key = format!("{}/write_ios", rrd_prefix);
-        rrd_update_derive(&rrd_key, stat.write_ios as f64);
-        let rrd_key = format!("{}/write_bytes", rrd_prefix);
-        rrd_update_derive(&rrd_key, (stat.write_sectors * 512) as f64);
-
-        let rrd_key = format!("{}/io_ticks", rrd_prefix);
-        rrd_update_derive(&rrd_key, (stat.io_ticks as f64) / 1000.0);
-    }
-}
-
 fn gather_disk_stats(disk_manager: Arc<DiskManage>, path: &Path, name: &str) -> DiskStat {
     let usage = match proxmox_sys::fs::fs_info(path) {
         Ok(status) => Some(status),
diff --git a/src/server/metric_collection/rrd.rs b/src/server/metric_collection/rrd.rs
index 32e0b5c5..910c57f2 100644
--- a/src/server/metric_collection/rrd.rs
+++ b/src/server/metric_collection/rrd.rs
@@ -16,6 +16,8 @@ use proxmox_sys::fs::CreateOptions;
 use pbs_buildcfg::PROXMOX_BACKUP_STATE_DIR_M;
 use proxmox_rrd_api_types::{RrdMode, RrdTimeframe};
 
+use super::{DiskStat, HostStats};
+
 const RRD_CACHE_BASEDIR: &str = concat!(PROXMOX_BACKUP_STATE_DIR_M!(), "/rrdb");
 
 static RRD_CACHE: OnceCell<Cache> = OnceCell::new();
@@ -145,3 +147,73 @@ pub fn rrd_update_derive(name: &str, value: f64) {
         }
     }
 }
+
+pub(super) fn rrd_update_host_stats_sync(
+    host: &HostStats,
+    hostdisk: &DiskStat,
+    datastores: &[DiskStat],
+) {
+    if let Some(stat) = &host.proc {
+        rrd_update_gauge("host/cpu", stat.cpu);
+        rrd_update_gauge("host/iowait", stat.iowait_percent);
+    }
+
+    if let Some(meminfo) = &host.meminfo {
+        rrd_update_gauge("host/memtotal", meminfo.memtotal as f64);
+        rrd_update_gauge("host/memused", meminfo.memused as f64);
+        rrd_update_gauge("host/swaptotal", meminfo.swaptotal as f64);
+        rrd_update_gauge("host/swapused", meminfo.swapused as f64);
+    }
+
+    if let Some(netdev) = &host.net {
+        use pbs_config::network::is_physical_nic;
+        let mut netin = 0;
+        let mut netout = 0;
+        for item in netdev {
+            if !is_physical_nic(&item.device) {
+                continue;
+            }
+            netin += item.receive;
+            netout += item.send;
+        }
+        rrd_update_derive("host/netin", netin as f64);
+        rrd_update_derive("host/netout", netout as f64);
+    }
+
+    if let Some(loadavg) = &host.load {
+        rrd_update_gauge("host/loadavg", loadavg.0);
+    }
+
+    rrd_update_disk_stat(hostdisk, "host");
+
+    for stat in datastores {
+        let rrd_prefix = format!("datastore/{}", stat.name);
+        rrd_update_disk_stat(stat, &rrd_prefix);
+    }
+}
+
+fn rrd_update_disk_stat(disk: &DiskStat, rrd_prefix: &str) {
+    if let Some(status) = &disk.usage {
+        let rrd_key = format!("{}/total", rrd_prefix);
+        rrd_update_gauge(&rrd_key, status.total as f64);
+        let rrd_key = format!("{}/used", rrd_prefix);
+        rrd_update_gauge(&rrd_key, status.used as f64);
+        let rrd_key = format!("{}/available", rrd_prefix);
+        rrd_update_gauge(&rrd_key, status.available as f64);
+    }
+
+    if let Some(stat) = &disk.dev {
+        let rrd_key = format!("{}/read_ios", rrd_prefix);
+        rrd_update_derive(&rrd_key, stat.read_ios as f64);
+        let rrd_key = format!("{}/read_bytes", rrd_prefix);
+        rrd_update_derive(&rrd_key, (stat.read_sectors * 512) as f64);
+
+        let rrd_key = format!("{}/write_ios", rrd_prefix);
+        rrd_update_derive(&rrd_key, stat.write_ios as f64);
+        let rrd_key = format!("{}/write_bytes", rrd_prefix);
+        rrd_update_derive(&rrd_key, (stat.write_sectors * 512) as f64);
+
+        let rrd_key = format!("{}/io_ticks", rrd_prefix);
+        rrd_update_derive(&rrd_key, (stat.io_ticks as f64) / 1000.0);
+    }
+}
-- 
2.39.5





More information about the pbs-devel mailing list