updated zabbixlaunch

This commit is contained in:
Paul Lecuq 2021-09-09 09:22:48 +02:00
parent a55d072d78
commit 873cfd64ed
6 changed files with 42 additions and 67 deletions

37
Cargo.lock generated
View File

@ -11,7 +11,7 @@ dependencies = [
"alsa-sys", "alsa-sys",
"bitflags", "bitflags",
"libc", "libc",
"nix 0.15.0", "nix",
] ]
[[package]] [[package]]
@ -175,16 +175,6 @@ dependencies = [
"core-foundation-sys 0.2.3", "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]] [[package]]
name = "embedded-graphics" name = "embedded-graphics"
version = "0.7.1" version = "0.7.1"
@ -510,15 +500,6 @@ version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
[[package]]
name = "memoffset"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "micromath" name = "micromath"
version = "1.1.1" version = "1.1.1"
@ -537,7 +518,7 @@ dependencies = [
"js-sys", "js-sys",
"libc", "libc",
"memalloc", "memalloc",
"nix 0.15.0", "nix",
"wasm-bindgen", "wasm-bindgen",
"web-sys", "web-sys",
"winapi", "winapi",
@ -602,19 +583,6 @@ dependencies = [
"void", "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]] [[package]]
name = "ntapi" name = "ntapi"
version = "0.3.6" version = "0.3.6"
@ -1246,7 +1214,6 @@ name = "zabbixlaunch"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"ctrlc",
"embedded-graphics", "embedded-graphics",
"launchy", "launchy",
"reqwest", "reqwest",

View File

@ -7,7 +7,6 @@ edition = "2018"
[dependencies] [dependencies]
clap = { version = "2.33.3" } clap = { version = "2.33.3" }
ctrlc = "3.2.0"
embedded-graphics = { version = "0.7", optional = true } embedded-graphics = { version = "0.7", optional = true }
launchy = { path = "launchy" } launchy = { path = "launchy" }
reqwest = { version = "0.11.4", features = ["blocking", "json"] } reqwest = { version = "0.11.4", features = ["blocking", "json"] }

View File

@ -15,7 +15,7 @@ pub struct Config {
impl Config { impl Config {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
server: String::from("https://zabbix.acme.com/api_jsonrpc.php"), server: String::from("https://zabbix.acme.com/api_jsonrpc.php"),
username: String::from("bob"), username: String::from("bob"),
password: String::from("password"), password: String::from("password"),
@ -40,10 +40,10 @@ impl Config {
let mut file = match fileopen { let mut file = match fileopen {
Ok(f) => f, Ok(f) => f,
Err(e) => { 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(); file.read_to_string(&mut contents).unwrap();
let parse: Result<Config, Error> = serde_json::from_str(contents.as_str()); let parse: Result<Config, Error> = serde_json::from_str(contents.as_str());
let cfg = match parse { let cfg = match parse {

View File

@ -2,23 +2,24 @@ pub mod config;
mod padcontrol; mod padcontrol;
mod zabbix; mod zabbix;
use std::borrow::Borrow;
use config::Config; use config::Config;
fn main() { fn main() {
// parse arguments
let matches = config::argparse(); let matches = config::argparse();
let configfile= matches.value_of("config").unwrap_or("config.json").to_owned();
// init and clear the launchpad // load configuration
let (mut canvas, mut poller) = padcontrol::initpad(); let configfile = matches
.value_of("config")
.unwrap_or("config.json");
let mut cfg = Config::new(); let mut cfg = Config::new();
cfg.load(&configfile); cfg.load(&configfile);
// init/connect to launchpad and clear it
let (mut canvas, mut poller) = padcontrol::initpad();
padcontrol::input(&mut canvas, &mut poller); padcontrol::input(&mut canvas, &mut poller);
// fetch zabbix informations // fetch zabbix data
zabbix::get_zabbix_authtoken(&mut cfg); zabbix::get_zabbix_authtoken(&mut cfg);
zabbix::get_zabbix_problems(&mut cfg); zabbix::get_zabbix_problems(&mut cfg);
cfg.save(&configfile); cfg.save(&configfile);

View File

@ -3,13 +3,14 @@ use launchy::{self, Canvas, CanvasLayout, CanvasLayoutPoller, MsgPollingWrapper,
pub fn initpad() -> (CanvasLayout<'static>, CanvasLayoutPoller) { pub fn initpad() -> (CanvasLayout<'static>, CanvasLayoutPoller) {
let (mut canvas, poller) = launchy::CanvasLayout::new_polling(); let (mut canvas, poller) = launchy::CanvasLayout::new_polling();
let res = canvas match canvas.add_by_guess_rotated::<launchy::mini::Canvas>(0, 0, launchy::Rotation::None) {
.add_by_guess_rotated::<launchy::mini::Canvas>(0, 0, launchy::Rotation::None);
match res {
Ok(o) => o, Ok(o) => o,
_ => panic!("Error connecting to midi device !"), _ => {
} eprintln!("Failed to connect to device, check USB connection");
println!("Connected to device"); std::process::exit(1);
},
};
println!("Connected to device using USB");
clear(&mut canvas); clear(&mut canvas);
(canvas, poller) (canvas, poller)
} }
@ -24,6 +25,7 @@ pub fn input(canvas: &mut CanvasLayout, poller: &mut CanvasLayoutPoller) {
} }
} }
/*
fn _effect(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 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()) { 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); canvas[pad] = canvas[pad].mix(surrounding_color, 0.4);
}); });
} }
} }*/
fn clear(canvas: &mut CanvasLayout) { fn clear(canvas: &mut CanvasLayout) {
for a in 0..8 { for a in 0..8 {

View File

@ -1,6 +1,8 @@
use crate::config::Config; use crate::config::Config;
use serde_json::{json, Value}; use serde_json::{json, Value};
const zabbix_api_version: &'static str = "2.0";
pub fn get_zabbix_authtoken(cfg: &mut Config) { pub fn get_zabbix_authtoken(cfg: &mut Config) {
let body = query_auth_token(&cfg.username, &cfg.password); let body = query_auth_token(&cfg.username, &cfg.password);
let response = reqwest::blocking::Client::new() 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, pub fn query_auth_token(zabbix_username: &String,
password: &String) -> Value { zabbix_password: &String) -> Value {
let api_function = "user.login";
let api_id = 1;
json!({ json!({
"jsonrpc": "2.0", "jsonrpc": zabbix_api_version,
"method": "user.login", "method": api_function,
"params": { "params": {
"user": username, "user": zabbix_username,
"password": password "password": zabbix_password
}, },
"id": 1 "id": api_id
}) })
} }
pub fn query_problems(token: &String, pub fn query_problems(zabbix_token: &String,
limit: i64) -> Value { zabbix_limit: i64) -> Value {
let zabbix_api_function = "problem.get";
let zabbix_api_id = 1;
json!({ json!({
"jsonrpc": "2.0", "jsonrpc": zabbix_api_version,
"method": "problem.get", "method": zabbix_api_function,
"params": { "params": {
"output": "extend", "output": "extend",
"selectAcknowledges": "extend", "selectAcknowledges": "extend",
@ -54,9 +60,9 @@ pub fn query_problems(token: &String,
"recent": "true", "recent": "true",
"sortfield": ["eventid"], "sortfield": ["eventid"],
"sortorder": "DESC", "sortorder": "DESC",
"limit": limit "limit": zabbix_limit
}, },
"auth": token, "auth": zabbix_token,
"id": 1 "id": zabbix_api_id
}) })
} }