From aeb42422d84c71a4f19c31e88227b3ffdeb40e38 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Mon, 13 Sep 2021 18:30:58 +0200 Subject: [PATCH] updated zabbixlaunch --- src/config/mod.rs | 5 +++++ src/padcontrol/mod.rs | 33 ++++++++++++++++++++++----------- src/zabbix/api.rs | 35 +++++++++++++++++++++++++++++------ src/zabbix/problems.rs | 11 ++--------- 4 files changed, 58 insertions(+), 26 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 0ac8aef..a0d1162 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -10,6 +10,7 @@ pub struct Config { pub username: String, pub password: String, pub authtoken: Option, + pub sloweffect: Option, pub refresh: Option, pub limit: Option, } @@ -21,6 +22,7 @@ impl Config { username: String::from("bob"), password: String::from("password"), authtoken: Some(String::from("token")), + sloweffect: Some(true), refresh: Some(5u64), limit: Some(20i64), } @@ -30,6 +32,9 @@ impl Config { if self.authtoken.is_none() { self.authtoken = Some(String::from("")); } + if self.sloweffect.is_none() { + self.sloweffect = Some(true); + } if self.refresh.is_none() { self.refresh = Some(5u64); } diff --git a/src/padcontrol/mod.rs b/src/padcontrol/mod.rs index a4eea34..67fe2be 100644 --- a/src/padcontrol/mod.rs +++ b/src/padcontrol/mod.rs @@ -1,11 +1,15 @@ +use std::time::Duration; +use std::thread::sleep; use crate::config::Config; use crate::zabbix::api::get_zabbix_problems; use crate::zabbix::problems::ZabbixLayout; use launchy::Color; use launchy::{self, Canvas, CanvasLayout, CanvasLayoutPoller, MsgPollingWrapper, Pad}; -use std::io::Write; -pub const MATRIX_SIZE: i32 = 8; + +pub const MATRIX_WIDTH: i32 = 8; +pub const MATRIX_SIZE: i32 = MATRIX_WIDTH*MATRIX_WIDTH; + pub fn initpad() -> (CanvasLayout<'static>, CanvasLayoutPoller) { let (mut canvas, poller) = launchy::CanvasLayout::new_polling(); @@ -35,29 +39,36 @@ pub fn draw(canvas: &mut CanvasLayout, datamatrix: &mut ZabbixLayout, cfg: &mut println!("Refresh rate is {} seconds", cfg.refresh.unwrap()); loop { let zabbix_data = get_zabbix_problems(cfg); - print!("."); - std::io::stdout().flush().unwrap(); datamatrix.compute(&zabbix_data); + clear(canvas); let mut x = 0i32; let mut y = 1i32; + let mut xx = 0i32; for j in datamatrix.layout.iter() { let p = Pad { x: x, y: y }; let c = match j.status { - 4 => Color::RED, - 3 => Color::from_hue(0.05), - 2 => Color::from_hue(1.24), + 5 => Color::RED, + 4 => Color::from_hue(0.05), + 3 => Color::from_hue(0.1), + 2 => Color::from_hue(1.22), _ => Color::GREEN, }; canvas[p] = c; x += 1; - if x % MATRIX_SIZE == 0 { + xx += 1; + if x % MATRIX_WIDTH == 0 { y += 1; x = 0; }; + if xx >= MATRIX_SIZE { + break; + } + if cfg.sloweffect.unwrap() { + sleep(Duration::from_millis(15)); + } + canvas.flush().unwrap(); } - canvas.flush().unwrap(); - std::io::stdout().flush().unwrap(); - std::thread::sleep(std::time::Duration::from_secs(cfg.refresh.unwrap_or(5))); + sleep(Duration::from_secs(cfg.refresh.unwrap_or(5))); } } diff --git a/src/zabbix/api.rs b/src/zabbix/api.rs index 0921d48..ef0bcd7 100644 --- a/src/zabbix/api.rs +++ b/src/zabbix/api.rs @@ -17,9 +17,8 @@ pub fn get_zabbix_authtoken(cfg: &mut Config) { } pub fn get_zabbix_problems(cfg: &mut Config) -> Value { - let body = query_problems( + let body = query_triggers( &cfg.authtoken.as_ref().unwrap_or(&String::from("")), - cfg.limit.unwrap(), ); let resp = reqwest::blocking::Client::new() .post(&cfg.server) @@ -43,20 +42,44 @@ pub fn query_auth_token(zabbix_username: &String, zabbix_password: &String) -> V }) } -pub fn query_problems(zabbix_token: &String, zabbix_limit: i64) -> Value { +fn _query_problems(zabbix_token: &String, zabbix_limit: i64) -> Value { let zabbix_api_function = "problem.get"; json!({ "jsonrpc": ZABBIX_API_VERSION, "method": zabbix_api_function, "params": { + "acknowledged": false, + "limit": zabbix_limit, "output": "extend", + "recent": true, "selectAcknowledges": "extend", - "selectTags": "extend", "selectSuppressionData": "extend", - "recent": "true", + "selectTags": "extend", "sortfield": ["eventid"], "sortorder": "DESC", - "limit": zabbix_limit + "suppressed": false, + }, + "auth": zabbix_token, + "id": ZABBIX_API_ID + }) +} + + +pub fn query_triggers(zabbix_token: &String) -> Value { + let zabbix_api_function = "trigger.get"; + json!({ + "jsonrpc": ZABBIX_API_VERSION, + "method": zabbix_api_function, + "params": { + "output": "extend", + "withLastEventUnacknowledged": 1, + "sortfield": "lastchange", + "sortorder": "DESC", + "only_true": 1, + "monitored": 1, + "active":1, + "selectHosts": "extend", + "min_severity": 1, }, "auth": zabbix_token, "id": ZABBIX_API_ID diff --git a/src/zabbix/problems.rs b/src/zabbix/problems.rs index 840ef54..a5b64c1 100644 --- a/src/zabbix/problems.rs +++ b/src/zabbix/problems.rs @@ -28,15 +28,8 @@ impl ZabbixLayout { self.layout = Vec::new(); for j in input.as_object().unwrap()["result"].as_array().unwrap() { let res = j.as_object().unwrap(); - let severity = res["severity"].as_str().unwrap().parse::().unwrap(); - let ack = res["acknowledged"] - .as_str() - .unwrap() - .parse::() - .unwrap(); - if ack == 0 { - self.layout.push(ZabbixProblems { status: severity }); - }; + let severity = res["priority"].as_str().unwrap().parse::().unwrap(); + self.layout.push(ZabbixProblems { status: severity }); } } }