pbs-config: add metrics config class

a section config like in pve

also adds a helper to get Metrics structs for all configured servers

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Dominik Csapak 2022-06-10 13:17:52 +02:00 committed by Wolfgang Bumiller
parent db971bf298
commit 9f30a31e53
4 changed files with 108 additions and 0 deletions

View File

@ -96,6 +96,7 @@ pxar = { version = "0.10.1", features = [ "tokio-io" ] }
proxmox-http = { version = "0.6.1", features = [ "client", "http-helpers", "websocket" ] }
proxmox-io = "1"
proxmox-lang = "1.1"
proxmox-metrics = "0.2"
proxmox-router = { version = "1.2.2", features = [ "cli" ] }
proxmox-schema = { version = "1.3.1", features = [ "api-macro" ] }
proxmox-section-config = "1"

View File

@ -25,6 +25,7 @@ proxmox-time = "1"
proxmox-serde = "0.1"
proxmox-shared-memory = "0.2"
proxmox-sys = "0.3"
proxmox-metrics = "0.2"
pbs-api-types = { path = "../pbs-api-types" }
pbs-buildcfg = { path = "../pbs-buildcfg" }

View File

@ -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 prune;
pub mod remote;

105
pbs-config/src/metrics.rs Normal file
View File

@ -0,0 +1,105 @@
use std::collections::HashMap;
use anyhow::Error;
use lazy_static::lazy_static;
use proxmox_metrics::{influxdb_http, influxdb_udp, Metrics};
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);
const UDP_SCHEMA: &ObjectSchema = InfluxDbUdp::API_SCHEMA.unwrap_object_schema();
let udp_plugin = SectionConfigPlugin::new(
"influxdb-udp".to_string(),
Some("name".to_string()),
UDP_SCHEMA,
);
config.register_plugin(udp_plugin);
const HTTP_SCHEMA: &ObjectSchema = InfluxDbHttp::API_SCHEMA.unwrap_object_schema();
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<BackupLockGuard, Error> {
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_default();
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<String, String>) -> Vec<String> {
match config() {
Ok((data, _digest)) => data.sections.keys().cloned().collect(),
Err(_) => return vec![],
}
}
/// Get the metric server connections from a config
pub fn get_metric_server_connections(
metric_config: SectionConfigData,
) -> Result<Vec<(Metrics, String)>, Error> {
let mut res = Vec::new();
for config in
metric_config.convert_to_typed_array::<pbs_api_types::InfluxDbUdp>("influxdb-udp")?
{
if !config.enable {
continue;
}
let future = influxdb_udp(&config.host, config.mtu);
res.push((future, config.name));
}
for config in
metric_config.convert_to_typed_array::<pbs_api_types::InfluxDbHttp>("influxdb-http")?
{
if !config.enable {
continue;
}
let future = influxdb_http(
&config.url,
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),
)?;
res.push((future, config.name));
}
Ok(res)
}