define conf structures
This commit is contained in:
parent
cfa70c946b
commit
2b079012f2
|
|
@ -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>,
|
||||
}
|
||||
|
|
@ -1,34 +1,77 @@
|
|||
mod config;
|
||||
mod observables;
|
||||
|
||||
use config::{Config, GraphConfig, SeriesConfig};
|
||||
use draw::{StackedConfig, StackedSeries, StackedSeriesConfig, stacked};
|
||||
use observables::{Observable, ObservableConfig};
|
||||
use protocol::Protocol;
|
||||
use observables::{Observable, ObservableCollection};
|
||||
use protocol::{Protocol, ProtocolError};
|
||||
use regex::Regex;
|
||||
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() {
|
||||
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") {
|
||||
// loop {
|
||||
let config = create_debug_config();
|
||||
|
||||
let cpu_load_cfg = ObservableConfig {
|
||||
name: "cpu".to_string(),
|
||||
norm: 1.,
|
||||
include: vec![Regex::new()],
|
||||
let cfg = StackedConfig {
|
||||
height_px: config.graphs[0].height_px as u32,
|
||||
max_samples: config.graphs[0].samples as u32,
|
||||
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);
|
||||
for _ in 1..10 {
|
||||
let sample = cpu.sample(&mut proto).unwrap();
|
||||
let sample = collection.sample(&mut proto).unwrap();
|
||||
series.push(sample);
|
||||
|
||||
let img = stacked(&series);
|
||||
|
|
|
|||
|
|
@ -1,32 +1,25 @@
|
|||
use protocol::{Protocol, ProtocolError};
|
||||
use regex::Regex;
|
||||
|
||||
pub struct ObservableConfig {
|
||||
pub name: String,
|
||||
pub norm: f32,
|
||||
pub include: Vec<Regex>,
|
||||
}
|
||||
|
||||
pub struct Observable {
|
||||
pub name: String,
|
||||
norm: f32,
|
||||
metrics: Vec<String>,
|
||||
}
|
||||
|
||||
impl Observable {
|
||||
pub fn discover(
|
||||
proto: &mut Protocol,
|
||||
cfg: ObservableConfig,
|
||||
name: &str,
|
||||
includes: &Vec<Regex>,
|
||||
) -> Result<Observable, ProtocolError> {
|
||||
let all_values = proto.list()?;
|
||||
let filtered_values = all_values
|
||||
.into_iter()
|
||||
.filter(|v| cfg.include.iter().any(|i| i.is_match(v)))
|
||||
.filter(|v| includes.iter().any(|i| i.is_match(v)))
|
||||
.collect();
|
||||
|
||||
Ok(Observable {
|
||||
name: cfg.name,
|
||||
norm: cfg.norm,
|
||||
name: name.to_string(),
|
||||
metrics: filtered_values,
|
||||
})
|
||||
}
|
||||
|
|
@ -38,6 +31,22 @@ impl Observable {
|
|||
.map(|f| proto.get(f.into(), "value".to_string()))
|
||||
.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()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue