From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 3B12684BC4 for ; Wed, 15 Dec 2021 10:20:35 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 32DDEFB91 for ; Wed, 15 Dec 2021 10:20:05 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id A8226FABA for ; Wed, 15 Dec 2021 10:20:00 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 09C82451FD for ; Wed, 15 Dec 2021 10:19:55 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Wed, 15 Dec 2021 10:19:49 +0100 Message-Id: <20211215091952.1490583-7-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211215091952.1490583-1-d.csapak@proxmox.com> References: <20211215091952.1490583-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.174 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox-backup v2 3/6] pbs-config: add metrics config class X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 15 Dec 2021 09:20:35 -0000 a section config like in pve Signed-off-by: Dominik Csapak --- Cargo.toml | 1 + pbs-config/Cargo.toml | 1 + pbs-config/src/lib.rs | 1 + pbs-config/src/metrics.rs | 122 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 pbs-config/src/metrics.rs diff --git a/Cargo.toml b/Cargo.toml index d7ad2085..1643b628 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -96,6 +96,7 @@ pxar = { version = "0.10.1", features = [ "tokio-io" ] } proxmox-http = { version = "0.6", features = [ "client", "http-helpers", "websocket" ] } proxmox-io = "1" proxmox-lang = "1" +proxmox-metrics = "0.1" proxmox-router = { version = "1.1", features = [ "cli" ] } proxmox-schema = { version = "1", features = [ "api-macro" ] } proxmox-section-config = "1" diff --git a/pbs-config/Cargo.toml b/pbs-config/Cargo.toml index cd14d823..4c920712 100644 --- a/pbs-config/Cargo.toml +++ b/pbs-config/Cargo.toml @@ -25,6 +25,7 @@ proxmox-time = "1" proxmox-serde = "0.1" proxmox-shared-memory = "0.2" proxmox-sys = "0.2" +proxmox-metrics = "0.1" pbs-api-types = { path = "../pbs-api-types" } pbs-buildcfg = { path = "../pbs-buildcfg" } diff --git a/pbs-config/src/lib.rs b/pbs-config/src/lib.rs index 118030bc..29880ab9 100644 --- a/pbs-config/src/lib.rs +++ b/pbs-config/src/lib.rs @@ -6,6 +6,7 @@ pub mod domains; pub mod drive; pub mod key_config; pub mod media_pool; +pub mod metrics; pub mod network; pub mod remote; pub mod sync; diff --git a/pbs-config/src/metrics.rs b/pbs-config/src/metrics.rs new file mode 100644 index 00000000..97f0cd17 --- /dev/null +++ b/pbs-config/src/metrics.rs @@ -0,0 +1,122 @@ +use std::collections::HashMap; + +use anyhow::Error; +use lazy_static::lazy_static; + +use proxmox_metrics::{influxdb, MetricsChannel, MetricsServerFuture}; +use proxmox_schema::*; +use proxmox_section_config::{SectionConfig, SectionConfigData, SectionConfigPlugin}; + +use pbs_api_types::{InfluxDbHttp, InfluxDbUdp, METRIC_SERVER_ID_SCHEMA}; + +use crate::{open_backup_lockfile, BackupLockGuard}; + +lazy_static! { + pub static ref CONFIG: SectionConfig = init(); +} + +fn init() -> SectionConfig { + let mut config = SectionConfig::new(&METRIC_SERVER_ID_SCHEMA); + + let udp_schema = match InfluxDbUdp::API_SCHEMA { + Schema::Object(ref object_schema) => object_schema, + _ => unreachable!(), + }; + + let udp_plugin = SectionConfigPlugin::new( + "influxdb-udp".to_string(), + Some("name".to_string()), + udp_schema, + ); + config.register_plugin(udp_plugin); + + let http_schema = match InfluxDbHttp::API_SCHEMA { + Schema::Object(ref object_schema) => object_schema, + _ => unreachable!(), + }; + + let http_plugin = SectionConfigPlugin::new( + "influxdb-http".to_string(), + Some("name".to_string()), + http_schema, + ); + + config.register_plugin(http_plugin); + + config +} + +pub const METRIC_SERVER_CFG_FILENAME: &str = "/etc/proxmox-backup/metricserver.cfg"; +pub const METRIC_SERVER_CFG_LOCKFILE: &str = "/etc/proxmox-backup/.metricserver.lck"; + +/// Get exclusive lock +pub fn lock_config() -> Result { + open_backup_lockfile(METRIC_SERVER_CFG_LOCKFILE, None, true) +} + +pub fn config() -> Result<(SectionConfigData, [u8; 32]), Error> { + let content = proxmox_sys::fs::file_read_optional_string(METRIC_SERVER_CFG_FILENAME)? + .unwrap_or_else(|| "".to_string()); + + let digest = openssl::sha::sha256(content.as_bytes()); + let data = CONFIG.parse(METRIC_SERVER_CFG_FILENAME, &content)?; + Ok((data, digest)) +} + +pub fn save_config(config: &SectionConfigData) -> Result<(), Error> { + let raw = CONFIG.write(METRIC_SERVER_CFG_FILENAME, &config)?; + crate::replace_backup_config(METRIC_SERVER_CFG_FILENAME, raw.as_bytes()) +} + +// shell completion helper +pub fn complete_remote_name(_arg: &str, _param: &HashMap) -> Vec { + match config() { + Ok((data, _digest)) => data.sections.iter().map(|(id, _)| id.to_string()).collect(), + Err(_) => return vec![], + } +} + +/// Get the metric server connections from a config +pub fn get_metric_server_connections( + metric_config: SectionConfigData, +) -> Result<(Vec, Vec, Vec), Error> { + let mut futures = Vec::new(); + let mut channels = Vec::new(); + let mut names = Vec::new(); + + for config in + metric_config.convert_to_typed_array::("influxdb-udp")? + { + let (future, sender) = influxdb::InfluxDbUdp::new(&config.host, config.port, config.mtu); + names.push(config.name); + futures.push(future); + channels.push(sender); + } + + for config in + metric_config.convert_to_typed_array::("influxdb-http")? + { + let port = config.port.unwrap_or_else(|| { + if config.https.unwrap_or(true) { + 443 + } else { + 80 + } + }); + + let (future, sender) = influxdb::InfluxDbHttp::new( + config.https.unwrap_or(true), + &config.host, + port, + config.organization.as_deref().unwrap_or("proxmox"), + config.bucket.as_deref().unwrap_or("proxmox"), + config.token.as_deref(), + config.verify_tls.unwrap_or(true), + config.max_body_size.unwrap_or(25_000_000), + )?; + names.push(config.name); + futures.push(future); + channels.push(sender); + } + Ok((futures, channels, names)) +} -- 2.30.2