public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup v4 5/6] proxmox-backup-proxy: send metrics to configured metrics server
Date: Mon, 17 Jan 2022 11:48:24 +0100	[thread overview]
Message-ID: <20220117104825.2409598-10-d.csapak@proxmox.com> (raw)
In-Reply-To: <20220117104825.2409598-1-d.csapak@proxmox.com>

and keep the data as similar as possible to pve (tags/fields)

datastores get their own 'object' type and reside in the "blockstat"
measurement

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/bin/proxmox-backup-proxy.rs | 135 +++++++++++++++++++++++++++++++-
 1 file changed, 132 insertions(+), 3 deletions(-)

diff --git a/src/bin/proxmox-backup-proxy.rs b/src/bin/proxmox-backup-proxy.rs
index fd2120ee..a6deeb66 100644
--- a/src/bin/proxmox-backup-proxy.rs
+++ b/src/bin/proxmox-backup-proxy.rs
@@ -23,11 +23,13 @@ use proxmox_sys::linux::{
 };
 use proxmox_sys::fs::{CreateOptions, FileSystemInformation};
 use proxmox_lang::try_block;
+use proxmox_metrics::MetricsData;
 use proxmox_router::{RpcEnvironment, RpcEnvironmentType, UserInformation};
 use proxmox_http::client::{RateLimitedStream, ShareableRateLimit};
 use proxmox_sys::{task_log, task_warn};
 use proxmox_sys::logrotate::LogRotate;
 
+use pbs_config::metrics::get_metric_server_connections;
 use pbs_datastore::DataStore;
 
 use proxmox_rest_server::{
@@ -948,16 +950,123 @@ async fn run_stat_generator() {
             }
         };
 
-        let rrd_future = tokio::task::spawn_blocking(move || {
-            rrd_update_host_stats_sync(&stats.0, &stats.1, &stats.2);
-            rrd_sync_journal();
+        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_sync_journal();
+            }
         });
 
+        let metrics_future = send_data_to_metric_servers(stats);
+
+        let (rrd_res, metrics_res) = join!(rrd_future, metrics_future);
+        if let Err(err) = rrd_res {
+            log::error!("rrd update panicked: {}", err);
+        }
+        if let Err(err) = metrics_res {
+            log::error!("error during metrics sending: {}", err);
+        }
 
         tokio::time::sleep_until(tokio::time::Instant::from_std(delay_target)).await;
 
      }
+}
+
+async fn send_data_to_metric_servers(
+    stats: Arc<(HostStats, DiskStat, Vec<DiskStat>)>,
+) -> Result<(), Error> {
+    let (config, _digest) = pbs_config::metrics::config()?;
+    let (channels, names) = get_metric_server_connections(config)?;
+
+    if channels.is_empty() {
+        return Ok(());
+    }
+
+    let ctime = proxmox_time::epoch_i64();
+    let nodename = proxmox_sys::nodename();
+
+    let mut values = Vec::new();
 
+    let mut cpuvalue = match &stats.0.proc {
+        Some(stat) => serde_json::to_value(stat)?,
+        None => json!({}),
+    };
+
+    if let Some(loadavg) = &stats.0.load {
+        cpuvalue["avg1"] = Value::from(loadavg.0);
+        cpuvalue["avg5"] = Value::from(loadavg.1);
+        cpuvalue["avg15"] = Value::from(loadavg.2);
+    }
+
+    values.push(Arc::new(MetricsData::new(
+        "cpustat",
+        &[("object", "host"), ("host", nodename)],
+        ctime,
+        cpuvalue,
+    )?));
+
+    if let Some(stat) = &stats.0.meminfo {
+        values.push(Arc::new(MetricsData::new(
+            "memory",
+            &[("object", "host"), ("host", nodename)],
+            ctime,
+            stat,
+        )?));
+    }
+
+    if let Some(netdev) = &stats.0.net {
+        for item in netdev {
+            values.push(Arc::new(MetricsData::new(
+                "nics",
+                &[
+                    ("object", "host"),
+                    ("host", nodename),
+                    ("instance", &item.device),
+                ],
+                ctime,
+                item,
+            )?));
+        }
+    }
+
+    values.push(Arc::new(MetricsData::new(
+        "blockstat",
+        &[("object", "host"), ("host", nodename)],
+        ctime,
+        stats.1.to_value(),
+    )?));
+
+    for datastore in stats.2.iter() {
+        values.push(Arc::new(MetricsData::new(
+            "blockstat",
+            &[
+                ("object", "datastore"),
+                ("nodename", nodename),
+                ("datastore", &datastore.name),
+            ],
+            ctime,
+            datastore.to_value(),
+        )?));
+    }
+
+    let results = proxmox_metrics::send_data_to_channels(&values, &channels).await;
+    for (res, name) in results.into_iter().zip(names.iter()) {
+        if let Err(err) = res {
+            log::error!("error sending into channel of {}: {}", name, err);
+        }
+    }
+
+    futures::future::join_all(channels.into_iter().zip(names.into_iter()).map(
+        |(channel, name)| async move {
+            if let Err(err) = channel.join().await {
+                log::error!("error sending to metric server {}: {}", name, err);
+            }
+        },
+    ))
+    .await;
+
+    Ok(())
 }
 
 struct HostStats {
@@ -973,6 +1082,26 @@ struct DiskStat {
     dev: Option<BlockDevStat>,
 }
 
+impl DiskStat {
+    fn to_value(&self) -> Value {
+        let mut value = json!({});
+        if let Some(usage) = &self.usage {
+            value["total"] = Value::from(usage.total);
+            value["used"] = Value::from(usage.used);
+            value["avail"] = Value::from(usage.available);
+        }
+
+        if let Some(dev) = &self.dev {
+            value["read_ios"] = Value::from(dev.read_ios);
+            value["read_bytes"] = Value::from(dev.read_sectors * 512);
+            value["write_ios"] = Value::from(dev.write_ios);
+            value["write_bytes"] = Value::from(dev.write_sectors * 512);
+            value["io_ticks"] = Value::from(dev.io_ticks / 1000);
+        }
+        value
+    }
+}
+
 fn collect_host_stats_sync() -> HostStats {
     use proxmox_sys::linux::procfs::{
         read_meminfo, read_proc_stat, read_proc_net_dev, read_loadavg};
-- 
2.30.2





  parent reply	other threads:[~2022-01-17 10:49 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-17 10:48 [pbs-devel] [PATCH proxmox/proxmox-backup v4] add metrics server capability Dominik Csapak
2022-01-17 10:48 ` [pbs-devel] [PATCH proxmox v4 1/4] proxmox-sys: make some structs serializable Dominik Csapak
2022-02-01 11:39   ` [pbs-devel] applied: " Thomas Lamprecht
2022-01-17 10:48 ` [pbs-devel] [PATCH proxmox v4 2/4] proxmox-sys: add FileSystemInformation struct and helper Dominik Csapak
2022-02-01 11:40   ` [pbs-devel] applied: " Thomas Lamprecht
2022-01-17 10:48 ` [pbs-devel] [PATCH proxmox v4 3/4] proxmox-async: add connect_to_udp helper Dominik Csapak
2022-02-01 12:02   ` Thomas Lamprecht
2022-02-01 12:13     ` Dominik Csapak
2022-02-01 12:39       ` Thomas Lamprecht
2022-02-01 13:17         ` Wolfgang Bumiller
2022-01-17 10:48 ` [pbs-devel] [PATCH proxmox v4 4/4] proxmox-metrics: implement metrics server client code Dominik Csapak
2022-01-17 10:48 ` [pbs-devel] [PATCH proxmox-backup v4 1/6] use 'fs_info' from proxmox-sys Dominik Csapak
2022-01-17 10:48 ` [pbs-devel] [PATCH proxmox-backup v4 2/6] pbs-api-types: add metrics api types Dominik Csapak
2022-02-01  9:55   ` Matthias Heiserer
2022-02-01 10:11     ` Dominik Csapak
2022-01-17 10:48 ` [pbs-devel] [PATCH proxmox-backup v4 3/6] pbs-config: add metrics config class Dominik Csapak
2022-01-17 10:48 ` [pbs-devel] [PATCH proxmox-backup v4 4/6] backup-proxy: decouple stats gathering from rrd update Dominik Csapak
2022-01-17 10:48 ` Dominik Csapak [this message]
2022-01-17 10:48 ` [pbs-devel] [PATCH proxmox-backup v4 6/6] api: add metricserver endpoints Dominik Csapak
2022-02-01 11:01 ` [pbs-devel] [PATCH proxmox/proxmox-backup v4] add metrics server capability Matthias Heiserer
2022-02-01 11:39   ` Thomas Lamprecht
2022-02-01 13:22     ` Matthias Heiserer
2022-02-01 13:26       ` Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220117104825.2409598-10-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal