src/bin/proxmox-backup-proxy.rs: simplify code

This commit is contained in:
Dietmar Maurer 2020-05-25 16:19:55 +02:00
parent d0833a70f7
commit 309ef20d6d
1 changed files with 28 additions and 40 deletions

View File

@ -600,21 +600,31 @@ async fn run_stat_generator() {
} }
fn rrd_update_gauge(name: &str, value: f64) {
use proxmox_backup::rrd;
if let Err(err) = rrd::update_value(name, value, rrd::DST::Gauge) {
eprintln!("rrd::update_value '{}' failed - {}", name, err);
}
}
fn rrd_update_derive(name: &str, value: f64) {
use proxmox_backup::rrd;
if let Err(err) = rrd::update_value(name, value, rrd::DST::Derive) {
eprintln!("rrd::update_value '{}' failed - {}", name, err);
}
}
async fn generate_host_stats() { async fn generate_host_stats() {
use proxmox::sys::linux::procfs::{ use proxmox::sys::linux::procfs::{
read_meminfo, read_proc_stat, read_proc_net_dev, read_loadavg}; read_meminfo, read_proc_stat, read_proc_net_dev, read_loadavg};
use proxmox_backup::{ rrd, config::datastore }; use proxmox_backup::config::datastore;
proxmox_backup::tools::runtime::block_in_place(move || { proxmox_backup::tools::runtime::block_in_place(move || {
match read_proc_stat() { match read_proc_stat() {
Ok(stat) => { Ok(stat) => {
if let Err(err) = rrd::update_value("host/cpu", stat.cpu, rrd::DST::Gauge) { rrd_update_gauge("host/cpu", stat.cpu);
eprintln!("rrd::update_value 'host/cpu' failed - {}", err); rrd_update_gauge("host/iowait", stat.iowait_percent);
}
if let Err(err) = rrd::update_value("host/iowait", stat.iowait_percent, rrd::DST::Gauge) {
eprintln!("rrd::update_value 'host/iowait' failed - {}", err);
}
} }
Err(err) => { Err(err) => {
eprintln!("read_proc_stat failed - {}", err); eprintln!("read_proc_stat failed - {}", err);
@ -623,18 +633,10 @@ async fn generate_host_stats() {
match read_meminfo() { match read_meminfo() {
Ok(meminfo) => { Ok(meminfo) => {
if let Err(err) = rrd::update_value("host/memtotal", meminfo.memtotal as f64, rrd::DST::Gauge) { rrd_update_gauge("host/memtotal", meminfo.memtotal as f64);
eprintln!("rrd::update_value 'host/memtotal' failed - {}", err); rrd_update_gauge("host/memused", meminfo.memused as f64);
} rrd_update_gauge("host/swaptotal", meminfo.swaptotal as f64);
if let Err(err) = rrd::update_value("host/memused", meminfo.memused as f64, rrd::DST::Gauge) { rrd_update_gauge("host/swapused", meminfo.swapused as f64);
eprintln!("rrd::update_value 'host/memused' failed - {}", err);
}
if let Err(err) = rrd::update_value("host/swaptotal", meminfo.swaptotal as f64, rrd::DST::Gauge) {
eprintln!("rrd::update_value 'host/swaptotal' failed - {}", err);
}
if let Err(err) = rrd::update_value("host/swapused", meminfo.swapused as f64, rrd::DST::Gauge) {
eprintln!("rrd::update_value 'host/swapused' failed - {}", err);
}
} }
Err(err) => { Err(err) => {
eprintln!("read_meminfo failed - {}", err); eprintln!("read_meminfo failed - {}", err);
@ -651,12 +653,8 @@ async fn generate_host_stats() {
netin += item.receive; netin += item.receive;
netout += item.send; netout += item.send;
} }
if let Err(err) = rrd::update_value("host/netin", netin as f64, rrd::DST::Derive) { rrd_update_derive("host/netin", netin as f64);
eprintln!("rrd::update_value 'host/netin' failed - {}", err); rrd_update_derive("host/netout", netout as f64);
}
if let Err(err) = rrd::update_value("host/netout", netout as f64, rrd::DST::Derive) {
eprintln!("rrd::update_value 'host/netout' failed - {}", err);
}
} }
Err(err) => { Err(err) => {
eprintln!("read_prox_net_dev failed - {}", err); eprintln!("read_prox_net_dev failed - {}", err);
@ -665,9 +663,7 @@ async fn generate_host_stats() {
match read_loadavg() { match read_loadavg() {
Ok(loadavg) => { Ok(loadavg) => {
if let Err(err) = rrd::update_value("host/loadavg", loadavg.0 as f64, rrd::DST::Gauge) { rrd_update_gauge("host/loadavg", loadavg.0 as f64);
eprintln!("rrd::update_value 'host/roottotal' failed - {}", err);
}
} }
Err(err) => { Err(err) => {
eprintln!("read_loadavg failed - {}", err); eprintln!("read_loadavg failed - {}", err);
@ -676,12 +672,8 @@ async fn generate_host_stats() {
match disk_usage(std::path::Path::new("/")) { match disk_usage(std::path::Path::new("/")) {
Ok((total, used, _avail)) => { Ok((total, used, _avail)) => {
if let Err(err) = rrd::update_value("host/roottotal", total as f64, rrd::DST::Gauge) { rrd_update_gauge("host/roottotal", total as f64);
eprintln!("rrd::update_value 'host/roottotal' failed - {}", err); rrd_update_gauge("host/rootused", used as f64);
}
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) => { Err(err) => {
eprintln!("read root disk_usage failed - {}", err); eprintln!("read root disk_usage failed - {}", err);
@ -697,12 +689,8 @@ async fn generate_host_stats() {
match disk_usage(std::path::Path::new(&config.path)) { match disk_usage(std::path::Path::new(&config.path)) {
Ok((total, used, _avail)) => { Ok((total, used, _avail)) => {
let rrd_key = format!("datastore/{}", config.name); let rrd_key = format!("datastore/{}", config.name);
if let Err(err) = rrd::update_value(&rrd_key, total as f64, rrd::DST::Gauge) { rrd_update_gauge(&rrd_key, total as f64);
eprintln!("rrd::update_value '{}' failed - {}", rrd_key, err); rrd_update_gauge(&rrd_key, used as f64);
}
if let Err(err) = rrd::update_value(&rrd_key, used as f64, rrd::DST::Gauge) {
eprintln!("rrd::update_value '{}' failed - {}", rrd_key, err);
}
} }
Err(err) => { Err(err) => {
eprintln!("read disk_usage on {:?} failed - {}", config.path, err); eprintln!("read disk_usage on {:?} failed - {}", config.path, err);