diff --git a/rustfmt.toml b/rustfmt.toml deleted file mode 100644 index 7530651..0000000 --- a/rustfmt.toml +++ /dev/null @@ -1 +0,0 @@ -max_width = 120 diff --git a/src/config.rs b/src/config.rs index fb80dd3..cd179f2 100644 --- a/src/config.rs +++ b/src/config.rs @@ -28,14 +28,17 @@ pub struct Configuration { impl Configuration { pub async fn load() -> Result { - let mut s = Config::builder(); - if std::fs::metadata("config").is_ok(){ - s = s.add_source(config::File::with_name("config")); - } else { println!("config file not found. continuing with env vars... ") }; - - s = s.add_source(config::Environment::with_prefix("VICI_EXPORTER").separator("_")); -// s.build().unwrap(); - let conf: Configuration = s.build().unwrap().try_deserialize().unwrap(); - Ok(conf) + let settings = Config::builder() + .set_default("vici.socket", "/var/run/charon.vici")? + .set_default("vici.interval", 10)? + .set_default("server.address", "0.0.0.0")? + .set_default("server.port", 8000)? + .add_source(config::File::with_name("config")) + .add_source(config::Environment::with_prefix("VICI_EXPORTER").separator("_")) + .build(); + match settings { + Ok(body) => Ok(body.try_deserialize().unwrap()), + Err(err) => Err(err.into()), + } } } diff --git a/src/main.rs b/src/main.rs index 80a12cb..a3a7234 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,4 @@ -use metrics::{ - describe_gauge, - gauge, - describe_counter, - counter, - IntoLabels, - Unit}; +use metrics::{counter, describe_counter, describe_gauge, gauge, IntoLabels, Unit}; use metrics_exporter_prometheus::PrometheusBuilder; use tokio::time::{interval, Duration, MissedTickBehavior}; @@ -44,11 +38,26 @@ async fn main() -> anyhow::Result<()> { let mut child_labels = sa_child_values.into_labels(); child_labels.push((&("sa_name", sa_name.clone())).into()); child_labels.push((&("sa_child_name", sa_child_name)).into()); - counter!("sa_child_bytes_in", sa_child_values.bytes_in, child_labels.clone()); - counter!("sa_child_bytes_out", sa_child_values.bytes_out, child_labels.clone()); - counter!("sa_child_packets_in", sa_child_values.packets_in, child_labels.clone()); - counter!("sa_child_packets_out", sa_child_values.packets_out, child_labels.clone()); - + counter!( + "sa_child_bytes_in", + sa_child_values.bytes_in, + child_labels.clone() + ); + counter!( + "sa_child_bytes_out", + sa_child_values.bytes_out, + child_labels.clone() + ); + counter!( + "sa_child_packets_in", + sa_child_values.packets_in, + child_labels.clone() + ); + counter!( + "sa_child_packets_out", + sa_child_values.packets_out, + child_labels.clone() + ); } } interval.tick().await; diff --git a/src/vici.rs b/src/vici.rs index 7f22db4..a1ca33a 100644 --- a/src/vici.rs +++ b/src/vici.rs @@ -1,12 +1,12 @@ #![allow(dead_code)] -use serde::Deserialize; +use serde::{de::value::BoolDeserializer, Deserialize}; use std::collections::HashMap; use futures_util::stream::StreamExt; use anyhow::Result; -use metrics::{IntoLabels,Label}; +use metrics::{IntoLabels, Label}; #[derive(Debug, Deserialize)] pub struct VICIState { @@ -25,22 +25,45 @@ impl VICIState { Ok(VICIState { version: client.request("version", ()).await?, statistics: client.request("statistics", ()).await?, - policies: collected_stream::(client, "list-policies", "list-policy").await?, - connections: collected_stream::(client, "list-connections", "list-conn") - .await?, - security_associations: collected_stream::( - client, "list-sas", "list-sa", + policies: collected_stream::( + client, + "list-policies", + "list-policy", ) .await?, - certificates: collected_stream::(client, "list-certs", "list-cert").await?, - authorities: collected_stream::(client, "list-authorities", "list-authority") + connections: collected_stream::( + client, + "list-connections", + "list-conn", + ) + .await?, + security_associations: + collected_stream::( + client, "list-sas", "list-sa", + ) .await?, + certificates: collected_stream::( + client, + "list-certs", + "list-cert", + ) + .await?, + authorities: collected_stream::( + client, + "list-authorities", + "list-authority", + ) + .await?, pools: collected_stream::(client, "list-pools", "list-pool").await?, }) } } -async fn collected_stream(client: &mut rsvici::Client, command: &str, event: &str) -> Result +async fn collected_stream( + client: &mut rsvici::Client, + command: &str, + event: &str, +) -> Result where N: for<'de> serde::Deserialize<'de>, C: std::iter::Extend + Default, @@ -243,7 +266,6 @@ pub struct SecurityAssociation { pub child_security_associations: HashMap, } - impl IntoLabels for &SecurityAssociationChild { fn into_labels(self) -> Vec