proxmox-rrd: use log crate instead of eprintln, avoid duplicate logs

This commit is contained in:
Dietmar Maurer 2021-10-06 18:19:22 +02:00
parent 18e8bc17e4
commit 3def6bfc64
3 changed files with 29 additions and 17 deletions

View File

@ -8,6 +8,7 @@ description = "Simple RRD database implementation."
[dependencies]
anyhow = "1.0"
bitflags = "1.2.1"
log = "0.4"
proxmox = { version = "0.13.5", features = ["api-macro"] }

View File

@ -72,7 +72,7 @@ impl RRDCache {
Ok(rrd) => rrd,
Err(err) => {
if err.kind() != std::io::ErrorKind::NotFound {
eprintln!("overwriting RRD file {:?}, because of load error: {}", path, err);
log::warn!("overwriting RRD file {:?}, because of load error: {}", path, err);
}
RRD::new(dst)
},

View File

@ -118,7 +118,7 @@ impl RRA {
(last_value*(self.last_count as f64))/(new_count as f64)
+ value/(new_count as f64)
} else {
eprintln!("rrdb update failed - unknown CF");
log::error!("rrdb update failed - unknown CF");
return;
};
self.data[index] = new_value;
@ -127,15 +127,18 @@ impl RRA {
self.last_update = time;
}
fn update(&mut self, time: f64, mut value: f64) {
fn update(&mut self, time: f64, mut value: f64, log_time_in_past: &mut bool) {
if time <= self.last_update {
eprintln!("rrdb update failed - time in past ({} < {})", time, self.last_update);
if *log_time_in_past {
log::warn!("rrdb update failed - time in past ({} < {})", time, self.last_update);
*log_time_in_past = false; // avoid logging this multiple times inside a RRD
}
return;
}
if value.is_nan() {
eprintln!("rrdb update failed - new value is NAN");
log::warn!("rrdb update failed - new value is NAN");
return;
}
@ -147,12 +150,12 @@ impl RRA {
let diff = if self.counter_value.is_nan() {
0.0
} else if is_counter && value < 0.0 {
eprintln!("rrdb update failed - got negative value for counter");
log::warn!("rrdb update failed - got negative value for counter");
return;
} else if is_counter && value < self.counter_value {
// Note: We do not try automatic overflow corrections
self.counter_value = value;
eprintln!("rrdb update failed - conter overflow/reset detected");
log::warn!("rrdb update failed - conter overflow/reset detected");
return;
} else {
value - self.counter_value
@ -339,19 +342,27 @@ impl RRD {
///
/// Note: This does not call [Self::save].
pub fn update(&mut self, time: f64, value: f64) {
self.hour_avg.update(time, value);
self.hour_max.update(time, value);
self.day_avg.update(time, value);
self.day_max.update(time, value);
if value.is_nan() {
log::warn!("rrdb update failed - new value is NAN");
return;
}
self.week_avg.update(time, value);
self.week_max.update(time, value);
let mut log_time_in_past = true;
self.month_avg.update(time, value);
self.month_max.update(time, value);
self.hour_avg.update(time, value, &mut log_time_in_past);
self.hour_max.update(time, value, &mut log_time_in_past);
self.year_avg.update(time, value);
self.year_max.update(time, value);
self.day_avg.update(time, value, &mut log_time_in_past);
self.day_max.update(time, value, &mut log_time_in_past);
self.week_avg.update(time, value, &mut log_time_in_past);
self.week_max.update(time, value, &mut log_time_in_past);
self.month_avg.update(time, value, &mut log_time_in_past);
self.month_max.update(time, value, &mut log_time_in_past);
self.year_avg.update(time, value, &mut log_time_in_past);
self.year_max.update(time, value, &mut log_time_in_past);
}
}