From a4a3f7ca5ef2d100c62db7ac87e26170418070af Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sat, 23 May 2020 14:03:44 +0200 Subject: [PATCH] rrd: pack multiple rrd values into th estat list --- src/api2/node/rrd.rs | 13 ++++-- src/bin/proxmox-backup-proxy.rs | 15 ++++++- src/rrd/cache.rs | 33 ++++++++++++-- src/rrd/rrd.rs | 80 ++++++++++++++++++++++++++------- www/DataStoreStatus.js | 25 +++++++++-- 5 files changed, 139 insertions(+), 27 deletions(-) diff --git a/src/api2/node/rrd.rs b/src/api2/node/rrd.rs index acfd072b..72534515 100644 --- a/src/api2/node/rrd.rs +++ b/src/api2/node/rrd.rs @@ -20,15 +20,20 @@ use crate::api2::types::*; }, }, )] -/// Read CPU stats -fn get_cpu_stats( +/// Read node stats +fn get_node_stats( timeframe: RRDTimeFrameResolution, cf: RRDMode, _param: Value, ) -> Result { - crate::rrd::extract_data("host/cpu", timeframe, cf) + crate::rrd::extract_data_list( + "host", + &["cpu", "iowait", "memtotal", "memused"], + timeframe, + cf, + ) } pub const ROUTER: Router = Router::new() - .get(&API_METHOD_GET_CPU_STATS); + .get(&API_METHOD_GET_NODE_STATS); diff --git a/src/bin/proxmox-backup-proxy.rs b/src/bin/proxmox-backup-proxy.rs index 04f7b165..42249e18 100644 --- a/src/bin/proxmox-backup-proxy.rs +++ b/src/bin/proxmox-backup-proxy.rs @@ -601,7 +601,7 @@ async fn run_stat_generator() { } async fn generate_host_stats() { - use proxmox::sys::linux::procfs::read_proc_stat; + use proxmox::sys::linux::procfs::{read_meminfo, read_proc_stat}; use proxmox_backup::rrd; match read_proc_stat() { @@ -614,4 +614,17 @@ async fn generate_host_stats() { eprintln!("read_proc_stat failed - {}", err); } } + match read_meminfo() { + Ok(meminfo) => { + if let Err(err) = rrd::update_value("host/memtotal", meminfo.memtotal as f64) { + eprintln!("rrd::update_value 'host/memtotal' failed - {}", err); + } + if let Err(err) = rrd::update_value("host/memused", meminfo.memused as f64) { + eprintln!("rrd::update_value 'host/memused' failed - {}", err); + } + } + Err(err) => { + eprintln!("read_meminfo failed - {}", err); + } + } } diff --git a/src/rrd/cache.rs b/src/rrd/cache.rs index 6baa2e33..c80b9f76 100644 --- a/src/rrd/cache.rs +++ b/src/rrd/cache.rs @@ -49,7 +49,7 @@ pub fn update_value(rel_path: &str, value: f64) -> Result<(), Error> { let mut map = RRD_CACHE.write().unwrap(); let now = now()?; - + if let Some(rrd) = map.get_mut(rel_path) { rrd.update(now, value); rrd.save(&path)?; @@ -62,7 +62,7 @@ pub fn update_value(rel_path: &str, value: f64) -> Result<(), Error> { rrd.save(&path)?; map.insert(rel_path.into(), rrd); } - + Ok(()) } @@ -75,10 +75,37 @@ pub fn extract_data( let now = now()?; let map = RRD_CACHE.read().unwrap(); - + if let Some(rrd) = map.get(rel_path) { Ok(rrd.extract_data(now, timeframe, mode)) } else { Ok(RRD::new().extract_data(now, timeframe, mode)) } } + + +pub fn extract_data_list( + base: &str, + items: &[&str], + timeframe: RRDTimeFrameResolution, + mode: RRDMode, +) -> Result { + + let now = now()?; + + let map = RRD_CACHE.read().unwrap(); + + let mut list: Vec<(&str, &RRD)> = Vec::new(); + + let empty_rrd = RRD::new(); + + for name in items.iter() { + if let Some(rrd) = map.get(&format!("{}/{}", base, name)) { + list.push((name, rrd)); + } else { + list.push((name, &empty_rrd)); + } + } + + Ok(extract_rrd_data(&list, now, timeframe, mode)) +} diff --git a/src/rrd/rrd.rs b/src/rrd/rrd.rs index 681d5be1..398d48cb 100644 --- a/src/rrd/rrd.rs +++ b/src/rrd/rrd.rs @@ -6,8 +6,7 @@ use serde_json::{json, Value}; use crate::api2::types::{RRDMode, RRDTimeFrameResolution}; -const RRD_DATA_ENTRIES: usize = 70; - +pub const RRD_DATA_ENTRIES: usize = 70; #[repr(C)] #[derive(Default, Copy, Clone)] @@ -65,7 +64,7 @@ impl RRD { RRDTimeFrameResolution::Month => &self.month, RRDTimeFrameResolution::Year => &self.year, }; - + let mut t = start; let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize; for _ in 0..RRD_DATA_ENTRIES { @@ -94,7 +93,7 @@ impl RRD { if raw.len() != expected_len { bail!("RRD::from_raw failed - wrong data size ({} != {})", raw.len(), expected_len); } - + let mut rrd: RRD = unsafe { std::mem::zeroed() }; unsafe { let rrd_slice = std::slice::from_raw_parts_mut(&mut rrd as *mut _ as *mut u8, expected_len); @@ -108,7 +107,7 @@ impl RRD { let raw = proxmox::tools::fs::file_get_contents(filename)?; Self::from_raw(&raw) } - + pub fn save(&self, filename: &Path) -> Result<(), Error> { use proxmox::tools::{fs::replace_file, fs::CreateOptions}; @@ -126,15 +125,15 @@ impl RRD { .group(backup_user.gid); replace_file(filename, rrd_slice, options)?; - + Ok(()) } - + fn compute_new_value( data: &[RRDEntry; RRD_DATA_ENTRIES], index: usize, value: f64, - ) -> RRDEntry { + ) -> RRDEntry { let RRDEntry { max, average, count } = data[index]; let new_count = count + 1; // fixme: check overflow? if count == 0 { @@ -145,7 +144,7 @@ impl RRD { RRDEntry { max: new_max, average: new_average, count: new_count } } } - + pub fn update(&mut self, epoch: u64, value: f64) { // fixme: check time progress (epoch last) let last = self.last_update; @@ -162,7 +161,7 @@ impl RRD { let index = ((epoch/reso) % (RRD_DATA_ENTRIES as u64)) as usize; self.hour[index] = Self::compute_new_value(&self.hour, index, value); - let reso = RRDTimeFrameResolution::Day as u64; + let reso = RRDTimeFrameResolution::Day as u64; let min_time = epoch - (RRD_DATA_ENTRIES as u64)*reso; let mut t = last; let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize; @@ -172,8 +171,8 @@ impl RRD { } let index = ((epoch/reso) % (RRD_DATA_ENTRIES as u64)) as usize; self.day[index] = Self::compute_new_value(&self.day, index, value); - - let reso = RRDTimeFrameResolution::Week as u64; + + let reso = RRDTimeFrameResolution::Week as u64; let min_time = epoch - (RRD_DATA_ENTRIES as u64)*reso; let mut t = last; let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize; @@ -184,7 +183,7 @@ impl RRD { let index = ((epoch/reso) % (RRD_DATA_ENTRIES as u64)) as usize; self.week[index] = Self::compute_new_value(&self.week, index, value); - let reso = RRDTimeFrameResolution::Month as u64; + let reso = RRDTimeFrameResolution::Month as u64; let min_time = epoch - (RRD_DATA_ENTRIES as u64)*reso; let mut t = last; let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize; @@ -194,8 +193,8 @@ impl RRD { } let index = ((epoch/reso) % (RRD_DATA_ENTRIES as u64)) as usize; self.month[index] = Self::compute_new_value(&self.month, index, value); - - let reso = RRDTimeFrameResolution::Year as u64; + + let reso = RRDTimeFrameResolution::Year as u64; let min_time = epoch - (RRD_DATA_ENTRIES as u64)*reso; let mut t = last; let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize; @@ -209,3 +208,54 @@ impl RRD { self.last_update = epoch; } } + +pub fn extract_rrd_data( + rrd_list: &[(&str, &RRD)], + epoch: u64, + timeframe: RRDTimeFrameResolution, + mode: RRDMode, +) -> Value { + + let reso = timeframe as u64; + + let end = reso*(epoch/reso); + let start = end - reso*(RRD_DATA_ENTRIES as u64); + + let mut list = Vec::new(); + + let mut t = start; + let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize; + for _ in 0..RRD_DATA_ENTRIES { + let mut item = json!({ "time": t }); + for (name, rrd) in rrd_list.iter() { + let rrd_end = reso*(rrd.last_update/reso); + let rrd_start = rrd_end - reso*(RRD_DATA_ENTRIES as u64); + + if t < rrd_start || t > rrd_end { + continue; + } else { + let data = match timeframe { + RRDTimeFrameResolution::Hour => &rrd.hour, + RRDTimeFrameResolution::Day => &rrd.day, + RRDTimeFrameResolution::Week => &rrd.week, + RRDTimeFrameResolution::Month => &rrd.month, + RRDTimeFrameResolution::Year => &rrd.year, + }; + let entry = data[index]; + if entry.count == 0 { + continue; + } else { + let value = match mode { + RRDMode::Max => entry.max, + RRDMode::Average => entry.average, + }; + item[name] = value.into(); + } + } + } + list.push(item); + t += reso; index = (index + 1) % RRD_DATA_ENTRIES; + } + + list.into() +} diff --git a/www/DataStoreStatus.js b/www/DataStoreStatus.js index 902b8d78..3c24a899 100644 --- a/www/DataStoreStatus.js +++ b/www/DataStoreStatus.js @@ -12,16 +12,26 @@ Ext.define('pve-rrd-node', { extend: 'Ext.data.Model', fields: [ { - name: 'value', + name: 'cpu', // percentage convert: function(value) { return value*100; } }, + { + name: 'iowait', + // percentage + convert: function(value) { + return value*100; + } + }, + "memtotal", + "memused", { type: 'date', dateFormat: 'timestamp', name: 'time' } ] }); + Ext.define('PBS.DataStoreStatus', { extend: 'Ext.panel.Panel', alias: 'widget.pbsDataStoreStatus', @@ -53,10 +63,17 @@ Ext.define('PBS.DataStoreStatus', { { xtype: 'proxmoxRRDChart', title: gettext('CPU usage'), - fields: ['value'], - fieldTitles: [gettext('CPU usage')], + fields: ['cpu','iowait'], + fieldTitles: [gettext('CPU usage'), gettext('IO delay')], store: rrdstore - } + }, + { + xtype: 'proxmoxRRDChart', + title: gettext('Memory usage'), + fields: ['memtotal','memused'], + fieldTitles: [gettext('Total'), gettext('RAM usage')], + store: rrdstore + }, ] };