define conf structures

This commit is contained in:
Ondrej Novak 2026-06-14 22:52:38 +02:00
parent cfa70c946b
commit 2b079012f2
Signed by: handy
SSH Key Fingerprint: SHA256:jlNm8ijkaMl4X7+nn3zBFTJufdQgT4unKJoZVH6kYTM
3 changed files with 103 additions and 32 deletions

View File

@ -0,0 +1,19 @@
use regex::Regex;
pub struct Config {
pub graphs: Vec<GraphConfig>,
}
pub struct GraphConfig {
pub name: String,
pub width_px: u16,
pub height_px: u16,
pub samples: u16,
pub series: Vec<SeriesConfig>,
}
pub struct SeriesConfig {
pub name: String,
pub color: (u8, u8, u8),
pub metrics: Vec<Regex>,
}

View File

@ -1,34 +1,77 @@
mod config;
mod observables; mod observables;
use config::{Config, GraphConfig, SeriesConfig};
use draw::{StackedConfig, StackedSeries, StackedSeriesConfig, stacked}; use draw::{StackedConfig, StackedSeries, StackedSeriesConfig, stacked};
use observables::{Observable, ObservableConfig}; use observables::{Observable, ObservableCollection};
use protocol::Protocol; use protocol::{Protocol, ProtocolError};
use regex::Regex;
use std::{thread, time}; use std::{thread, time};
fn create_debug_config() -> Config {
let load_regexes = vec![Regex::new(".*\\/cpu-[0-9]+\\/cpu-(nice|steal|system|user)").unwrap()];
let iowait_regexes = vec![Regex::new(".*\\/cpu-[0-9]+\\/cpu-wait").unwrap()];
let irq_regexes = vec![Regex::new(".*\\/cpu-[0-9]+\\/cpu-(interrupt|softirq)").unwrap()];
let idle_regexes = vec![Regex::new(".*\\/cpu-[0-9]+\\/cpu-idle").unwrap()];
let series = vec![
SeriesConfig {
name: "load".to_string(),
color: (2, 154, 219),
metrics: load_regexes,
},
SeriesConfig {
name: "io wait".to_string(),
color: (68, 200, 229),
metrics: iowait_regexes,
},
SeriesConfig {
name: "irq".to_string(),
color: (143, 232, 252),
metrics: irq_regexes,
},
SeriesConfig {
name: "idle".to_string(),
color: (0, 0, 0),
metrics: idle_regexes,
},
];
let graphs = vec![GraphConfig {
name: "cpu load".to_string(),
width_px: 480,
height_px: 98,
samples: 60,
series: series,
}];
Config { graphs: graphs }
}
fn main() { fn main() {
let cfg = StackedConfig {
width_px_per_sample: 4,
height_px: 100,
series: vec![
StackedSeriesConfig { color: (255, 0, 0) },
StackedSeriesConfig { color: (0, 255, 0) },
StackedSeriesConfig { color: (0, 0, 255) },
],
max_samples: 10,
};
if let Ok(mut proto) = Protocol::new("/var/run/collectd-unixsock") { if let Ok(mut proto) = Protocol::new("/var/run/collectd-unixsock") {
// loop { let config = create_debug_config();
let cpu_load_cfg = ObservableConfig { let cfg = StackedConfig {
name: "cpu".to_string(), height_px: config.graphs[0].height_px as u32,
norm: 1., max_samples: config.graphs[0].samples as u32,
include: vec![Regex::new()], width_px_per_sample: (config.graphs[0].width_px / config.graphs[0].samples) as u32,
series: config.graphs[0]
.series
.iter()
.map(|s| StackedSeriesConfig { color: s.color })
.collect(),
}; };
let cpu = Observable::discover(&mut proto).unwrap();
let observables = config.graphs[0]
.series
.iter()
.map(|s| Observable::discover(&mut proto, &s.name, &s.metrics))
.collect::<Result<Vec<Observable>, ProtocolError>>()
.unwrap();
let collection = ObservableCollection::new(observables);
let mut series = StackedSeries::new(cfg); let mut series = StackedSeries::new(cfg);
for _ in 1..10 { for _ in 1..10 {
let sample = cpu.sample(&mut proto).unwrap(); let sample = collection.sample(&mut proto).unwrap();
series.push(sample); series.push(sample);
let img = stacked(&series); let img = stacked(&series);

View File

@ -1,32 +1,25 @@
use protocol::{Protocol, ProtocolError}; use protocol::{Protocol, ProtocolError};
use regex::Regex; use regex::Regex;
pub struct ObservableConfig {
pub name: String,
pub norm: f32,
pub include: Vec<Regex>,
}
pub struct Observable { pub struct Observable {
pub name: String, pub name: String,
norm: f32,
metrics: Vec<String>, metrics: Vec<String>,
} }
impl Observable { impl Observable {
pub fn discover( pub fn discover(
proto: &mut Protocol, proto: &mut Protocol,
cfg: ObservableConfig, name: &str,
includes: &Vec<Regex>,
) -> Result<Observable, ProtocolError> { ) -> Result<Observable, ProtocolError> {
let all_values = proto.list()?; let all_values = proto.list()?;
let filtered_values = all_values let filtered_values = all_values
.into_iter() .into_iter()
.filter(|v| cfg.include.iter().any(|i| i.is_match(v))) .filter(|v| includes.iter().any(|i| i.is_match(v)))
.collect(); .collect();
Ok(Observable { Ok(Observable {
name: cfg.name, name: name.to_string(),
norm: cfg.norm,
metrics: filtered_values, metrics: filtered_values,
}) })
} }
@ -38,6 +31,22 @@ impl Observable {
.map(|f| proto.get(f.into(), "value".to_string())) .map(|f| proto.get(f.into(), "value".to_string()))
.collect::<Result<Vec<f32>, ProtocolError>>()?; .collect::<Result<Vec<f32>, ProtocolError>>()?;
Ok(results.into_iter().sum::<f32>() / self.norm) Ok(results.into_iter().sum::<f32>())
}
}
pub struct ObservableCollection {
observables: Vec<Observable>,
}
impl ObservableCollection {
pub fn new(observables: Vec<Observable>) -> ObservableCollection {
ObservableCollection {
observables: observables,
}
}
pub fn sample(&self, proto: &mut Protocol) -> Result<Vec<f32>, ProtocolError> {
self.observables.iter().map(|o| o.sample(proto)).collect()
} }
} }