From 05b38b3540ed56fccc928565e695725a341d28a3 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Tue, 7 Dec 2021 18:49:04 +0100 Subject: [PATCH] updated zabbixlaunch --- src/config/mod.rs | 5 ++++- src/padcontrol/mod.rs | 22 +++++++++++----------- src/zabbix/api.rs | 34 +++++++++++++++++++++++----------- src/zabbix/problems.rs | 1 + 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 3fbaa7b..80e897d 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -56,9 +56,11 @@ impl Context { ctx } + pub fn hotreload(&mut self) { let mut i = 0; let waitmilli = self.cfg.refresh.unwrap_or(5) * 1000; + while i < waitmilli { let waitinc = waitmilli / ReloadFrequency::Medium as u64; let events = match self.inotify.read_events() { @@ -66,11 +68,12 @@ impl Context { Err(_) => vec![], }; if events.len() > 0 { - self.cfg.load(self.configfile.as_str()); println!( "Reloading {cfg}", cfg = self.configfile ); + self.cfg.load(self.configfile.as_str()); + get_zabbix_authtoken(&mut self.cfg); break; } else { sleep(Duration::from_millis(waitinc)); diff --git a/src/padcontrol/mod.rs b/src/padcontrol/mod.rs index 4be2542..9d5cd58 100644 --- a/src/padcontrol/mod.rs +++ b/src/padcontrol/mod.rs @@ -7,7 +7,9 @@ use std::time::Duration; pub const MATRIX_WIDTH: i32 = 8; pub const MATRIX_SIZE: i32 = MATRIX_WIDTH * MATRIX_WIDTH; -pub const REQUEST_TIMEOUT: i32 = 10; +pub const REQUEST_TIMEOUT: i32 = 5; +pub const ZERO_COLOR: f32 = 0.; +pub const ONE_COLOR: f32 = 1.; pub fn run(ctx: &mut Context) { match ctx.mode.as_str() { @@ -63,25 +65,24 @@ fn test(ctx: &mut Context) { sec = ctx.cfg.refresh.unwrap() ); loop { - let zabbix_data_result = get_zabbix_problems(&ctx.cfg); - let zabbix_data = match zabbix_data_result { + ctx.hotreload(); + let zabbix_data = match get_zabbix_problems(&ctx.cfg) { Ok(z) => z, Err(_) => { - println!("Error requesting zabbix service, retrying in {}", REQUEST_TIMEOUT); + println!("Error requesting zabbix service, retrying in {} seconds", REQUEST_TIMEOUT); sleep(Duration::from_secs(REQUEST_TIMEOUT as u64)); continue; } }; println!("{}", zabbix_data); - ctx.hotreload(); } } pub fn draw(canvas: &mut CanvasLayout, ctx: &mut Context) { println!("Refresh rate is {} seconds", ctx.cfg.refresh.unwrap()); loop { - let zabbix_data_result = get_zabbix_problems(&ctx.cfg); - let zabbix_data = match zabbix_data_result { + ctx.hotreload(); + let zabbix_data = match get_zabbix_problems(&ctx.cfg) { Ok(zabbix_data) => zabbix_data, Err(_) => { println!("Error requesting zabbix service, retrying in {}", REQUEST_TIMEOUT); @@ -114,7 +115,6 @@ pub fn draw(canvas: &mut CanvasLayout, ctx: &mut Context) { } canvas.flush().unwrap(); } - ctx.hotreload(); } } @@ -144,9 +144,9 @@ fn clear(canvas: &mut CanvasLayout) { for a in 0..8 { for b in 0..9 { canvas[Pad { x: a, y: b }] = Color { - r: 0f32, - g: 0f32, - b: 1f32, + r: ZERO_COLOR, + g: ZERO_COLOR, + b: ONE_COLOR, }; } } diff --git a/src/zabbix/api.rs b/src/zabbix/api.rs index 23eea28..ea70c48 100644 --- a/src/zabbix/api.rs +++ b/src/zabbix/api.rs @@ -1,7 +1,8 @@ use std::time::Duration; use std::thread::sleep; use crate::config::Config; -use serde_json::{json, Value}; +use serde_json::json; +use serde_json::Value as JsonValue; pub const ZABBIX_API_VERSION: &'static str = "2.0"; pub const ZABBIX_API_ID: i32 = 1; @@ -12,22 +13,33 @@ pub fn get_zabbix_authtoken(cfg: &mut Config) { let resp = reqwest::blocking::Client::new() .post(&cfg.server) .json(&body) - .send() - .unwrap(); - - let values: Value = resp.json().unwrap(); - cfg.authtoken = Some(values["result"].as_str().unwrap().to_string()); + .send(); + match resp { + Ok(v) => { + let values: JsonValue = v.json().unwrap(); + cfg.authtoken = Some(values["result"].as_str().unwrap().to_string()); + } + Err(e) => { + println!("{}", e); + cfg.authtoken = Some("".to_string()); + } + }; } /// Fetch Zabbix problems -pub fn get_zabbix_problems(cfg: &Config) -> Result { +pub fn get_zabbix_problems(cfg: &Config) -> Result { let body = build_query_triggers(&cfg.authtoken.as_ref().unwrap_or(&String::from(""))); let resp = reqwest::blocking::Client::new() .post(&cfg.server) .json(&body) .send(); - Ok(resp.unwrap().json().unwrap()) + match resp { + Ok(v) => v.json(), + Err(e) => { + Err(e) + } + } } /// Check if Zabbix is operational @@ -51,7 +63,7 @@ fn check_zabbix_connection(cfg: &Config) -> bool { } /// Build the query that fetchs the token -fn build_query_auth_token(zabbix_username: &String, zabbix_password: &String) -> Value { +fn build_query_auth_token(zabbix_username: &String, zabbix_password: &String) -> JsonValue { let zabbix_api_function = "user.login"; json!({ "jsonrpc": ZABBIX_API_VERSION, @@ -65,7 +77,7 @@ fn build_query_auth_token(zabbix_username: &String, zabbix_password: &String) -> } /// Build the query that fetchs problems -fn build_query_problems(zabbix_token: &String, zabbix_limit: i64) -> Value { +fn build_query_problems(zabbix_token: &String, zabbix_limit: i64) -> JsonValue { let zabbix_api_function = "problem.get"; json!({ "jsonrpc": ZABBIX_API_VERSION, @@ -88,7 +100,7 @@ fn build_query_problems(zabbix_token: &String, zabbix_limit: i64) -> Value { } /// Build the query that fetchs triggers -fn build_query_triggers(zabbix_token: &String) -> Value { +fn build_query_triggers(zabbix_token: &String) -> JsonValue { let zabbix_api_function = "trigger.get"; json!({ "jsonrpc": ZABBIX_API_VERSION, diff --git a/src/zabbix/problems.rs b/src/zabbix/problems.rs index 19c079a..27d44c3 100644 --- a/src/zabbix/problems.rs +++ b/src/zabbix/problems.rs @@ -3,6 +3,7 @@ use std::fmt::{Display, Formatter, Result}; #[derive(Debug, Clone)] pub enum ZabbixStatus { + Disaster = 5, High = 4, Average = 3, Warning = 2,