WIP: updates on hot reload of config using inotify
This commit is contained in:
parent
b7a90a81ba
commit
8dcdc017e0
6
Cargo.lock
generated
6
Cargo.lock
generated
@ -781,9 +781,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "reqwest"
|
||||
version = "0.11.4"
|
||||
version = "0.11.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "246e9f61b9bb77df069a947682be06e31ac43ea37862e244a69f177694ea6d22"
|
||||
checksum = "51c732d463dd300362ffb44b7b125f299c23d2990411a4253824630ebc7467fb"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"bytes",
|
||||
@ -1130,8 +1130,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ce9b1b516211d33767048e5d47fa2a381ed8b76fc48d2ce4aa39877f9f183e0"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"wasm-bindgen-macro",
|
||||
]
|
||||
|
||||
|
@ -9,7 +9,7 @@ edition = "2018"
|
||||
clap = { version = "2.33.3" }
|
||||
embedded-graphics = { version = "0.7.1", optional = true }
|
||||
launchy = { git = "https://github.com/kangalioo/launchy", branch = "master" }
|
||||
reqwest = { version = "0.11.4", features = ["blocking", "json"] }
|
||||
reqwest = { version = "0.11.5", features = ["blocking", "json"] }
|
||||
serde = { version = "1.0.130", features = ["derive"] }
|
||||
serde_json = "1.0.68"
|
||||
nix = "0.23.0"
|
@ -1,14 +1,16 @@
|
||||
use clap::{App, Arg, ArgMatches};
|
||||
use nix::sys::inotify::{AddWatchFlags, InitFlags, Inotify, InotifyEvent};
|
||||
use nix::sys::inotify::{AddWatchFlags, InitFlags, Inotify};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Error;
|
||||
use std::string::String;
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
use std::{fs::File, io::Read};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Context {
|
||||
pub cfg: Config,
|
||||
pub configfile: String,
|
||||
pub events: Option<Vec<InotifyEvent>>,
|
||||
pub inotify: Inotify,
|
||||
}
|
||||
|
||||
@ -22,19 +24,31 @@ impl Context {
|
||||
let mut ctx = Context {
|
||||
cfg: Config::new(),
|
||||
configfile: configfile,
|
||||
events: None,
|
||||
inotify: Inotify::init(InitFlags::empty()).unwrap(),
|
||||
inotify: Inotify::init(InitFlags::IN_NONBLOCK).unwrap(),
|
||||
};
|
||||
|
||||
println!("Loading {} file ...", ctx.configfile);
|
||||
ctx.cfg.load(&ctx.configfile);
|
||||
|
||||
println!("Adding inotify watch on {} file ...", ctx.configfile);
|
||||
ctx.inotify
|
||||
.add_watch(ctx.configfile.as_str(), AddWatchFlags::IN_MODIFY)
|
||||
.unwrap();
|
||||
|
||||
ctx
|
||||
}
|
||||
pub fn hotreload(&mut self) {
|
||||
let events = match self.inotify.read_events() {
|
||||
Ok(ev) => ev,
|
||||
Err(_) => vec![],
|
||||
};
|
||||
if events.len() > 0 {
|
||||
self.cfg.load(self.configfile.as_str());
|
||||
println!("{:?} {} {:?}", self.cfg, events.len(), events);
|
||||
} else {
|
||||
sleep(Duration::from_secs(self.cfg.refresh.unwrap_or(5)));
|
||||
}
|
||||
}
|
||||
pub fn argparse() -> ArgMatches<'static> {
|
||||
App::new("Zabbix Launch")
|
||||
.version("1.0")
|
||||
@ -76,19 +90,25 @@ impl<'a> Config {
|
||||
}
|
||||
}
|
||||
|
||||
fn feed_missing(&mut self) {
|
||||
fn feed_missing(&mut self) -> bool {
|
||||
let mut res = false;
|
||||
if self.authtoken.is_none() {
|
||||
self.authtoken = Some(String::from(""));
|
||||
res = true;
|
||||
}
|
||||
if self.sloweffect.is_none() {
|
||||
self.sloweffect = Some(true);
|
||||
res = true;
|
||||
}
|
||||
if self.refresh.is_none() {
|
||||
self.refresh = Some(5u64);
|
||||
res = true;
|
||||
}
|
||||
if self.limit.is_none() {
|
||||
self.limit = Some(20u64);
|
||||
res = true;
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
pub fn load(&'a mut self, configfile: &str) {
|
||||
@ -118,8 +138,9 @@ impl<'a> Config {
|
||||
Err(_e) => Config::new(),
|
||||
};
|
||||
*self = cfg;
|
||||
self.feed_missing();
|
||||
if self.feed_missing() {
|
||||
self.save(&configfile);
|
||||
};
|
||||
}
|
||||
|
||||
pub fn save(&self, configfile: &str) {
|
||||
|
@ -6,12 +6,12 @@ use config::Context;
|
||||
use zabbix::problems::ZabbixLayout;
|
||||
|
||||
fn main() {
|
||||
let mut datamatrix = ZabbixLayout { layout: Vec::new() };
|
||||
let mut datamatrix = ZabbixLayout::new();
|
||||
|
||||
// load configuration
|
||||
let mut context = Context::new();
|
||||
zabbix::api::get_zabbix_authtoken(&mut context.cfg);
|
||||
let (mut canvas, mut _poller) = padcontrol::initpad();
|
||||
padcontrol::draw(&mut canvas, &mut datamatrix, &mut context);
|
||||
//padcontrol::test(&mut context);
|
||||
//let (mut canvas, mut _poller) = padcontrol::initpad();
|
||||
//padcontrol::draw(&mut canvas, &mut datamatrix, &mut context);
|
||||
padcontrol::test(&mut context);
|
||||
}
|
||||
|
@ -33,30 +33,21 @@ pub fn _input(canvas: &mut CanvasLayout, poller: &mut CanvasLayoutPoller) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn test<'a>(ctx: &mut Context) {
|
||||
pub fn test(ctx: &mut Context) {
|
||||
println!("Refresh rate is {} seconds", ctx.cfg.refresh.unwrap());
|
||||
loop {
|
||||
println!("test");
|
||||
let events = match ctx.inotify.read_events() {
|
||||
Ok(ok) => ok,
|
||||
Err(_) => continue,
|
||||
};
|
||||
println!("nombre d'events: {}", events.len());
|
||||
for i in events {
|
||||
println!("test event");
|
||||
println!("{:?}", i);
|
||||
}
|
||||
let zabbix_data_result = get_zabbix_problems(&ctx.cfg);
|
||||
let zabbix_data = match zabbix_data_result {
|
||||
Ok(z) => z,
|
||||
Err(_) => {
|
||||
println!("Error requesting zabbix service");
|
||||
sleep(Duration::from_secs(10));
|
||||
let duration = 10;
|
||||
println!("Error requesting zabbix service, retrying in {}", duration);
|
||||
sleep(Duration::from_secs(duration));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
println!("{:?}", zabbix_data);
|
||||
sleep(Duration::from_secs(ctx.cfg.refresh.unwrap_or(5)));
|
||||
println!("{}", zabbix_data);
|
||||
ctx.hotreload();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -9,10 +9,18 @@ pub enum ZabbixStatus {
|
||||
Info = 1,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ZabbixLayout {
|
||||
pub layout: Vec<ZabbixProblems>,
|
||||
}
|
||||
|
||||
impl ZabbixLayout {
|
||||
pub fn new() -> Self {
|
||||
Self { layout: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ZabbixProblems {
|
||||
pub status: i64,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user