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",
"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",

View File

@ -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"] }

View File

@ -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<Config, Error> = serde_json::from_str(contents.as_str());
let cfg = match parse {

View File

@ -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);

View File

@ -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::<launchy::mini::Canvas>(0, 0, launchy::Rotation::None);
match res {
match canvas.add_by_guess_rotated::<launchy::mini::Canvas>(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 {

View File

@ -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
})
}