From 873cfd64ed2332b02ca6f8fd6d10d23e44af0516 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Thu, 9 Sep 2021 09:22:48 +0200 Subject: [PATCH] updated zabbixlaunch --- Cargo.lock | 37 ++----------------------------------- Cargo.toml | 1 - src/config/mod.rs | 6 +++--- src/main.rs | 15 ++++++++------- src/padcontrol/mod.rs | 16 +++++++++------- src/zabbix/mod.rs | 34 ++++++++++++++++++++-------------- 6 files changed, 42 insertions(+), 67 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3d40d4c..465f43d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,7 +11,7 @@ dependencies = [ "alsa-sys", "bitflags", "libc", - "nix 0.15.0", + "nix", ] [[package]] @@ -175,16 +175,6 @@ dependencies = [ "core-foundation-sys 0.2.3", ] -[[package]] -name = "ctrlc" -version = "3.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "377c9b002a72a0b2c1a18c62e2f3864bdfea4a015e3683a96e24aa45dd6c02d1" -dependencies = [ - "nix 0.22.1", - "winapi", -] - [[package]] name = "embedded-graphics" version = "0.7.1" @@ -510,15 +500,6 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" -[[package]] -name = "memoffset" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9" -dependencies = [ - "autocfg", -] - [[package]] name = "micromath" version = "1.1.1" @@ -537,7 +518,7 @@ dependencies = [ "js-sys", "libc", "memalloc", - "nix 0.15.0", + "nix", "wasm-bindgen", "web-sys", "winapi", @@ -602,19 +583,6 @@ dependencies = [ "void", ] -[[package]] -name = "nix" -version = "0.22.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7555d6c7164cc913be1ce7f95cbecdabda61eb2ccd89008524af306fb7f5031" -dependencies = [ - "bitflags", - "cc", - "cfg-if 1.0.0", - "libc", - "memoffset", -] - [[package]] name = "ntapi" version = "0.3.6" @@ -1246,7 +1214,6 @@ name = "zabbixlaunch" version = "0.1.0" dependencies = [ "clap", - "ctrlc", "embedded-graphics", "launchy", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index 4dfe8c8..9131096 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,6 @@ edition = "2018" [dependencies] clap = { version = "2.33.3" } -ctrlc = "3.2.0" embedded-graphics = { version = "0.7", optional = true } launchy = { path = "launchy" } reqwest = { version = "0.11.4", features = ["blocking", "json"] } diff --git a/src/config/mod.rs b/src/config/mod.rs index 2cd258e..4961bff 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -15,7 +15,7 @@ pub struct Config { impl Config { pub fn new() -> Self { - Self { + Self { server: String::from("https://zabbix.acme.com/api_jsonrpc.php"), username: String::from("bob"), password: String::from("password"), @@ -40,10 +40,10 @@ impl Config { let mut file = match fileopen { Ok(f) => f, Err(e) => { - panic!("{e}", e=e); + panic!("{}", e); }, }; - let mut contents = String::from(""); + let mut contents = String::new(); file.read_to_string(&mut contents).unwrap(); let parse: Result = serde_json::from_str(contents.as_str()); let cfg = match parse { diff --git a/src/main.rs b/src/main.rs index 35eaa39..d7deb8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,23 +2,24 @@ pub mod config; mod padcontrol; mod zabbix; -use std::borrow::Borrow; - - use config::Config; fn main() { + // parse arguments let matches = config::argparse(); - let configfile= matches.value_of("config").unwrap_or("config.json").to_owned(); - // init and clear the launchpad - let (mut canvas, mut poller) = padcontrol::initpad(); + // load configuration + let configfile = matches + .value_of("config") + .unwrap_or("config.json"); let mut cfg = Config::new(); cfg.load(&configfile); + // init/connect to launchpad and clear it + let (mut canvas, mut poller) = padcontrol::initpad(); padcontrol::input(&mut canvas, &mut poller); - // fetch zabbix informations + // fetch zabbix data zabbix::get_zabbix_authtoken(&mut cfg); zabbix::get_zabbix_problems(&mut cfg); cfg.save(&configfile); diff --git a/src/padcontrol/mod.rs b/src/padcontrol/mod.rs index d7d5272..35e6ed0 100644 --- a/src/padcontrol/mod.rs +++ b/src/padcontrol/mod.rs @@ -3,13 +3,14 @@ use launchy::{self, Canvas, CanvasLayout, CanvasLayoutPoller, MsgPollingWrapper, pub fn initpad() -> (CanvasLayout<'static>, CanvasLayoutPoller) { let (mut canvas, poller) = launchy::CanvasLayout::new_polling(); - let res = canvas - .add_by_guess_rotated::(0, 0, launchy::Rotation::None); - match res { + match canvas.add_by_guess_rotated::(0, 0, launchy::Rotation::None) { Ok(o) => o, - _ => panic!("Error connecting to midi device !"), - } - println!("Connected to device"); + _ => { + eprintln!("Failed to connect to device, check USB connection"); + std::process::exit(1); + }, + }; + println!("Connected to device using USB"); clear(&mut canvas); (canvas, poller) } @@ -24,6 +25,7 @@ pub fn input(canvas: &mut CanvasLayout, poller: &mut CanvasLayoutPoller) { } } +/* fn _effect(canvas: &mut CanvasLayout, poller: &mut CanvasLayoutPoller) { for color in (0u64..).map(|f| Color::red_green_color(f as f32 / 60.0 / 2.5)) { for msg in poller.iter_for_millis(17).filter(|msg| msg.is_press()) { @@ -44,7 +46,7 @@ fn _effect(canvas: &mut CanvasLayout, poller: &mut CanvasLayoutPoller) { canvas[pad] = canvas[pad].mix(surrounding_color, 0.4); }); } -} +}*/ fn clear(canvas: &mut CanvasLayout) { for a in 0..8 { diff --git a/src/zabbix/mod.rs b/src/zabbix/mod.rs index d3214c4..bb3df10 100644 --- a/src/zabbix/mod.rs +++ b/src/zabbix/mod.rs @@ -1,6 +1,8 @@ use crate::config::Config; use serde_json::{json, Value}; +const zabbix_api_version: &'static str = "2.0"; + pub fn get_zabbix_authtoken(cfg: &mut Config) { let body = query_auth_token(&cfg.username, &cfg.password); let response = reqwest::blocking::Client::new() @@ -28,24 +30,28 @@ pub fn get_zabbix_problems(cfg: &mut Config) { // } } -pub fn query_auth_token(username: &String, - password: &String) -> Value { +pub fn query_auth_token(zabbix_username: &String, + zabbix_password: &String) -> Value { + let api_function = "user.login"; + let api_id = 1; json!({ - "jsonrpc": "2.0", - "method": "user.login", + "jsonrpc": zabbix_api_version, + "method": api_function, "params": { - "user": username, - "password": password + "user": zabbix_username, + "password": zabbix_password }, - "id": 1 + "id": api_id }) } -pub fn query_problems(token: &String, - limit: i64) -> Value { +pub fn query_problems(zabbix_token: &String, + zabbix_limit: i64) -> Value { + let zabbix_api_function = "problem.get"; + let zabbix_api_id = 1; json!({ - "jsonrpc": "2.0", - "method": "problem.get", + "jsonrpc": zabbix_api_version, + "method": zabbix_api_function, "params": { "output": "extend", "selectAcknowledges": "extend", @@ -54,9 +60,9 @@ pub fn query_problems(token: &String, "recent": "true", "sortfield": ["eventid"], "sortorder": "DESC", - "limit": limit + "limit": zabbix_limit }, - "auth": token, - "id": 1 + "auth": zabbix_token, + "id": zabbix_api_id }) } \ No newline at end of file