rrd: pack multiple rrd values into th estat list

This commit is contained in:
Dietmar Maurer 2020-05-23 14:03:44 +02:00
parent ba1c249eec
commit a4a3f7ca5e
5 changed files with 139 additions and 27 deletions

View File

@ -20,15 +20,20 @@ use crate::api2::types::*;
}, },
}, },
)] )]
/// Read CPU stats /// Read node stats
fn get_cpu_stats( fn get_node_stats(
timeframe: RRDTimeFrameResolution, timeframe: RRDTimeFrameResolution,
cf: RRDMode, cf: RRDMode,
_param: Value, _param: Value,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
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() pub const ROUTER: Router = Router::new()
.get(&API_METHOD_GET_CPU_STATS); .get(&API_METHOD_GET_NODE_STATS);

View File

@ -601,7 +601,7 @@ async fn run_stat_generator() {
} }
async fn generate_host_stats() { 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; use proxmox_backup::rrd;
match read_proc_stat() { match read_proc_stat() {
@ -614,4 +614,17 @@ async fn generate_host_stats() {
eprintln!("read_proc_stat failed - {}", err); 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);
}
}
} }

View File

@ -49,7 +49,7 @@ pub fn update_value(rel_path: &str, value: f64) -> Result<(), Error> {
let mut map = RRD_CACHE.write().unwrap(); let mut map = RRD_CACHE.write().unwrap();
let now = now()?; let now = now()?;
if let Some(rrd) = map.get_mut(rel_path) { if let Some(rrd) = map.get_mut(rel_path) {
rrd.update(now, value); rrd.update(now, value);
rrd.save(&path)?; rrd.save(&path)?;
@ -62,7 +62,7 @@ pub fn update_value(rel_path: &str, value: f64) -> Result<(), Error> {
rrd.save(&path)?; rrd.save(&path)?;
map.insert(rel_path.into(), rrd); map.insert(rel_path.into(), rrd);
} }
Ok(()) Ok(())
} }
@ -75,10 +75,37 @@ pub fn extract_data(
let now = now()?; let now = now()?;
let map = RRD_CACHE.read().unwrap(); let map = RRD_CACHE.read().unwrap();
if let Some(rrd) = map.get(rel_path) { if let Some(rrd) = map.get(rel_path) {
Ok(rrd.extract_data(now, timeframe, mode)) Ok(rrd.extract_data(now, timeframe, mode))
} else { } else {
Ok(RRD::new().extract_data(now, timeframe, mode)) Ok(RRD::new().extract_data(now, timeframe, mode))
} }
} }
pub fn extract_data_list(
base: &str,
items: &[&str],
timeframe: RRDTimeFrameResolution,
mode: RRDMode,
) -> Result<Value, Error> {
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))
}

View File

@ -6,8 +6,7 @@ use serde_json::{json, Value};
use crate::api2::types::{RRDMode, RRDTimeFrameResolution}; use crate::api2::types::{RRDMode, RRDTimeFrameResolution};
const RRD_DATA_ENTRIES: usize = 70; pub const RRD_DATA_ENTRIES: usize = 70;
#[repr(C)] #[repr(C)]
#[derive(Default, Copy, Clone)] #[derive(Default, Copy, Clone)]
@ -65,7 +64,7 @@ impl RRD {
RRDTimeFrameResolution::Month => &self.month, RRDTimeFrameResolution::Month => &self.month,
RRDTimeFrameResolution::Year => &self.year, RRDTimeFrameResolution::Year => &self.year,
}; };
let mut t = start; let mut t = start;
let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize; let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize;
for _ in 0..RRD_DATA_ENTRIES { for _ in 0..RRD_DATA_ENTRIES {
@ -94,7 +93,7 @@ impl RRD {
if raw.len() != expected_len { if raw.len() != expected_len {
bail!("RRD::from_raw failed - wrong data size ({} != {})", raw.len(), expected_len); bail!("RRD::from_raw failed - wrong data size ({} != {})", raw.len(), expected_len);
} }
let mut rrd: RRD = unsafe { std::mem::zeroed() }; let mut rrd: RRD = unsafe { std::mem::zeroed() };
unsafe { unsafe {
let rrd_slice = std::slice::from_raw_parts_mut(&mut rrd as *mut _ as *mut u8, expected_len); 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)?; let raw = proxmox::tools::fs::file_get_contents(filename)?;
Self::from_raw(&raw) Self::from_raw(&raw)
} }
pub fn save(&self, filename: &Path) -> Result<(), Error> { pub fn save(&self, filename: &Path) -> Result<(), Error> {
use proxmox::tools::{fs::replace_file, fs::CreateOptions}; use proxmox::tools::{fs::replace_file, fs::CreateOptions};
@ -126,15 +125,15 @@ impl RRD {
.group(backup_user.gid); .group(backup_user.gid);
replace_file(filename, rrd_slice, options)?; replace_file(filename, rrd_slice, options)?;
Ok(()) Ok(())
} }
fn compute_new_value( fn compute_new_value(
data: &[RRDEntry; RRD_DATA_ENTRIES], data: &[RRDEntry; RRD_DATA_ENTRIES],
index: usize, index: usize,
value: f64, value: f64,
) -> RRDEntry { ) -> RRDEntry {
let RRDEntry { max, average, count } = data[index]; let RRDEntry { max, average, count } = data[index];
let new_count = count + 1; // fixme: check overflow? let new_count = count + 1; // fixme: check overflow?
if count == 0 { if count == 0 {
@ -145,7 +144,7 @@ impl RRD {
RRDEntry { max: new_max, average: new_average, count: new_count } RRDEntry { max: new_max, average: new_average, count: new_count }
} }
} }
pub fn update(&mut self, epoch: u64, value: f64) { pub fn update(&mut self, epoch: u64, value: f64) {
// fixme: check time progress (epoch last) // fixme: check time progress (epoch last)
let last = self.last_update; let last = self.last_update;
@ -162,7 +161,7 @@ impl RRD {
let index = ((epoch/reso) % (RRD_DATA_ENTRIES as u64)) as usize; let index = ((epoch/reso) % (RRD_DATA_ENTRIES as u64)) as usize;
self.hour[index] = Self::compute_new_value(&self.hour, index, value); 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 min_time = epoch - (RRD_DATA_ENTRIES as u64)*reso;
let mut t = last; let mut t = last;
let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize; 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; let index = ((epoch/reso) % (RRD_DATA_ENTRIES as u64)) as usize;
self.day[index] = Self::compute_new_value(&self.day, index, value); 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 min_time = epoch - (RRD_DATA_ENTRIES as u64)*reso;
let mut t = last; let mut t = last;
let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize; 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; let index = ((epoch/reso) % (RRD_DATA_ENTRIES as u64)) as usize;
self.week[index] = Self::compute_new_value(&self.week, index, value); 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 min_time = epoch - (RRD_DATA_ENTRIES as u64)*reso;
let mut t = last; let mut t = last;
let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize; 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; let index = ((epoch/reso) % (RRD_DATA_ENTRIES as u64)) as usize;
self.month[index] = Self::compute_new_value(&self.month, index, value); 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 min_time = epoch - (RRD_DATA_ENTRIES as u64)*reso;
let mut t = last; let mut t = last;
let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize; let mut index = ((t/reso) % (RRD_DATA_ENTRIES as u64)) as usize;
@ -209,3 +208,54 @@ impl RRD {
self.last_update = epoch; 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()
}

View File

@ -12,16 +12,26 @@ Ext.define('pve-rrd-node', {
extend: 'Ext.data.Model', extend: 'Ext.data.Model',
fields: [ fields: [
{ {
name: 'value', name: 'cpu',
// percentage // percentage
convert: function(value) { convert: function(value) {
return value*100; return value*100;
} }
}, },
{
name: 'iowait',
// percentage
convert: function(value) {
return value*100;
}
},
"memtotal",
"memused",
{ type: 'date', dateFormat: 'timestamp', name: 'time' } { type: 'date', dateFormat: 'timestamp', name: 'time' }
] ]
}); });
Ext.define('PBS.DataStoreStatus', { Ext.define('PBS.DataStoreStatus', {
extend: 'Ext.panel.Panel', extend: 'Ext.panel.Panel',
alias: 'widget.pbsDataStoreStatus', alias: 'widget.pbsDataStoreStatus',
@ -53,10 +63,17 @@ Ext.define('PBS.DataStoreStatus', {
{ {
xtype: 'proxmoxRRDChart', xtype: 'proxmoxRRDChart',
title: gettext('CPU usage'), title: gettext('CPU usage'),
fields: ['value'], fields: ['cpu','iowait'],
fieldTitles: [gettext('CPU usage')], fieldTitles: [gettext('CPU usage'), gettext('IO delay')],
store: rrdstore store: rrdstore
} },
{
xtype: 'proxmoxRRDChart',
title: gettext('Memory usage'),
fields: ['memtotal','memused'],
fieldTitles: [gettext('Total'), gettext('RAM usage')],
store: rrdstore
},
] ]
}; };