updated zabbixlaunch

This commit is contained in:
Paul 2021-09-09 00:50:28 +02:00
parent 8685112ad2
commit a55d072d78
7 changed files with 267 additions and 122 deletions

63
Cargo.lock generated
View File

@ -24,6 +24,26 @@ dependencies = [
"pkg-config",
]
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
"winapi",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.0.1"
@ -84,6 +104,21 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "2.33.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "core-foundation"
version = "0.2.3"
@ -886,6 +921,12 @@ dependencies = [
"winapi",
]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "syn"
version = "1.0.76"
@ -911,6 +952,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]]
name = "time"
version = "0.1.44"
@ -1024,6 +1074,12 @@ dependencies = [
"tinyvec",
]
[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
version = "0.2.2"
@ -1048,6 +1104,12 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "void"
version = "1.0.2"
@ -1183,6 +1245,7 @@ dependencies = [
name = "zabbixlaunch"
version = "0.1.0"
dependencies = [
"clap",
"ctrlc",
"embedded-graphics",
"launchy",

View File

@ -6,9 +6,10 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
launchy = { path = "launchy" }
embedded-graphics = { version = "0.7", optional = true }
clap = { version = "2.33.3" }
ctrlc = "3.2.0"
serde_json = "1.0.67"
serde = { version = "1.0.130", features = ["derive"] }
embedded-graphics = { version = "0.7", optional = true }
launchy = { path = "launchy" }
reqwest = { version = "0.11.4", features = ["blocking", "json"] }
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.67"

View File

@ -1,32 +0,0 @@
use serde_json::{json, Value};
pub fn query_auth_token(username: &String, password: &String) -> Value {
json!({
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": username,
"password": password
},
"id": 1
})
}
pub fn query_problems(token: &String, limit: Option<i8>) -> Value {
json!({
"jsonrpc": "2.0",
"method": "problem.get",
"params": {
"output": "extend",
"selectAcknowledges": "extend",
"selectTags": "extend",
"selectSuppressionData": "extend",
"recent": "true",
"sortfield": ["eventid"],
"sortorder": "DESC",
"limit": limit.unwrap_or(10)
},
"auth": token,
"id": 1
})
}

View File

@ -1,27 +1,75 @@
use std::{fs::File, io::Read};
use std::string::String;
use serde::{Deserialize, Serialize};
use serde_json::Error;
use std::string::String;
use std::{fs::File, io::Read};
use clap::{App, Arg, ArgMatches};
#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
pub server: String,
pub username: String,
pub password: String,
pub authtoken: String,
pub limit: i8,
pub authtoken: Option<String>,
pub limit: Option<i64>,
}
impl Config {
pub fn save(&self, configfile: &'static str) {
pub fn new() -> Self {
Self {
server: String::from("https://zabbix.acme.com/api_jsonrpc.php"),
username: String::from("bob"),
password: String::from("password"),
authtoken: Some(String::from("token")),
limit: Some(20i64)
}
}
pub fn load<'a>(&mut self, configfile: &'a str) {
let fileopen: Result<File,std::io::Error>;
let filemeta = std::fs::metadata(configfile);
let fileexists = match filemeta {
Ok(_) => true,
Err(_err) => false,
};
if !fileexists {
let _a = File::create(configfile);
}
fileopen = File::open(configfile);
let mut file = match fileopen {
Ok(f) => f,
Err(e) => {
panic!("{e}", e=e);
},
};
let mut contents = String::from("");
file.read_to_string(&mut contents).unwrap();
let parse: Result<Config, Error> = serde_json::from_str(contents.as_str());
let cfg = match parse {
Ok(cfg) => cfg,
Err(_e) => Config::new(),
};
*self = cfg;
self.save(configfile);
}
pub fn save<'a>(&self, configfile: &'a str) {
let file = File::create(configfile).unwrap();
serde_json::to_writer_pretty(file, &self).unwrap();
}
}
pub fn read(configfile: &'static str) -> Config {
let mut file = File::open(configfile).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
let config: Config = serde_json::from_str(contents.as_str()).unwrap();
config
pub fn argparse<'a>() -> ArgMatches<'a> {
App::new("Zabbix Launch")
.version("1.0")
.author("PaulBSD <paul@paulbsd.com>")
.about("Lights up Launchpad mini using Zabbix data")
.arg(Arg::with_name("config")
.short("c")
.long("config")
.value_name("FILE")
.help("Sets a custom config file")
.takes_value(true))
.get_matches()
}

View File

@ -1,82 +1,25 @@
mod config;
mod api;
pub mod config;
mod padcontrol;
mod zabbix;
use std::borrow::Borrow;
use launchy::{self, Canvas, CanvasLayout, CanvasLayoutPoller, MsgPollingWrapper, Pad};
use launchy::{Color};
use config::Config;
use serde_json::Value;
fn main() {
let matches = config::argparse();
let configfile= matches.value_of("config").unwrap_or("config.json").to_owned();
pub fn main() {
let (mut canvas, mut poller) = initpad();
let mut cfg = config::read("config.json");
get_zabbix_authtoken(&mut cfg);
get_zabbix_problems(&mut cfg);
clear(&mut canvas);
testinput(&mut canvas, &mut poller);
}
pub fn get_zabbix_authtoken(cfg: &mut Config) {
let body = api::query_auth_token(&cfg.username, &cfg.password);
let response = reqwest::blocking::Client::new()
.post(&cfg.server)
.json(&body)
.send()
.unwrap();
let values: Value = response
.json()
.unwrap();
cfg.authtoken = values["result"].as_str().unwrap().to_string();
//cfg.save("config.json");
}
fn get_zabbix_problems(cfg: &mut Config) {
let body = api::query_problems(&cfg.authtoken, Some(cfg.limit));
let response = reqwest::blocking::Client::new()
.post(&cfg.server)
.json(&body)
.send()
.unwrap();
let values: Value = response
.json()
.unwrap();
for i in values["result"].as_array().unwrap() {
println!("{}", i);
};
}
fn initpad() -> (CanvasLayout<'static>, CanvasLayoutPoller) {
let (mut canvas, poller) = launchy::CanvasLayout::new_polling();
canvas.add_by_guess_rotated::<launchy::mini::Canvas>(10, 18, launchy::Rotation::UpsideDown).unwrap();
(canvas, poller)
}
fn testinput(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()) {
canvas[msg.pad()] = color;
println!("{:?}", msg.pad())
}
let _res =canvas.flush();
canvas.iter().for_each(|pad| {
let surrounding_color = pad.neighbors_5().iter()
.map(|&p| canvas.get(p).unwrap_or(Color::GREEN))
.sum::<Color>() / 5.0 / 1.05;
canvas[pad] = canvas[pad].mix(surrounding_color, 0.4);
});
}
}
fn clear(canvas: &mut CanvasLayout) {
for a in 3..11 {
for b in 10..18 {
canvas[Pad { x: a, y: b }] = Color{r: 0f32, g: 0f32, b:1f32};
}
}
let _res =canvas.flush();
// init and clear the launchpad
let (mut canvas, mut poller) = padcontrol::initpad();
let mut cfg = Config::new();
cfg.load(&configfile);
padcontrol::input(&mut canvas, &mut poller);
// fetch zabbix informations
zabbix::get_zabbix_authtoken(&mut cfg);
zabbix::get_zabbix_problems(&mut cfg);
cfg.save(&configfile);
}

60
src/padcontrol/mod.rs Normal file
View File

@ -0,0 +1,60 @@
use launchy::Color;
use launchy::{self, Canvas, CanvasLayout, CanvasLayoutPoller, MsgPollingWrapper, Pad};
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 {
Ok(o) => o,
_ => panic!("Error connecting to midi device !"),
}
println!("Connected to device");
clear(&mut canvas);
(canvas, poller)
}
pub fn input(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()) {
canvas[msg.pad()] = color;
println!("{:?}", msg.pad())
}
let _res = canvas.flush();
}
}
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()) {
canvas[msg.pad()] = color;
println!("{:?}", msg.pad())
}
let _res = canvas.flush();
canvas.iter().for_each(|pad| {
let surrounding_color = pad
.neighbors_5()
.iter()
.map(|&p| canvas.get(p).unwrap_or(Color::GREEN))
.sum::<Color>()
/ 5.0
/ 1.05;
canvas[pad] = canvas[pad].mix(surrounding_color, 0.4);
});
}
}
fn clear(canvas: &mut CanvasLayout) {
for a in 0..8 {
for b in 0..8 {
canvas[Pad { x: a, y: b }] = Color {
r: 0f32,
g: 0f32,
b: 1f32,
};
}
}
let _res = canvas.flush();
}

62
src/zabbix/mod.rs Normal file
View File

@ -0,0 +1,62 @@
use crate::config::Config;
use serde_json::{json, Value};
pub fn get_zabbix_authtoken(cfg: &mut Config) {
let body = query_auth_token(&cfg.username, &cfg.password);
let response = reqwest::blocking::Client::new()
.post(&cfg.server)
.json(&body)
.send()
.unwrap();
let values: Value = response.json().unwrap();
cfg.authtoken = Some(values["result"].as_str().unwrap().to_string());
}
pub fn get_zabbix_problems(cfg: &mut Config) {
let body = query_problems(&cfg.authtoken.as_ref().unwrap_or(&String::from("")),
cfg.limit.unwrap());
let response = reqwest::blocking::Client::new()
.post(&cfg.server)
.json(&body)
.send()
.unwrap();
let _values: Value = response.json().unwrap();
// for i in values["result"].as_array().unwrap() {
// println!("{}", i);
// }
}
pub fn query_auth_token(username: &String,
password: &String) -> Value {
json!({
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": username,
"password": password
},
"id": 1
})
}
pub fn query_problems(token: &String,
limit: i64) -> Value {
json!({
"jsonrpc": "2.0",
"method": "problem.get",
"params": {
"output": "extend",
"selectAcknowledges": "extend",
"selectTags": "extend",
"selectSuppressionData": "extend",
"recent": "true",
"sortfield": ["eventid"],
"sortorder": "DESC",
"limit": limit
},
"auth": token,
"id": 1
})
}