updated zabbixlaunch

This commit is contained in:
Paul 2021-09-11 23:23:59 +02:00
parent 56eedaf040
commit 8b22768886
7 changed files with 163 additions and 75 deletions

1
Cargo.lock generated
View File

@ -457,6 +457,7 @@ dependencies = [
[[package]] [[package]]
name = "launchy" name = "launchy"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/kangalioo/launchy?branch=master#f066a4f8a2616dcb7b2b9b2b06f30136a80fef35"
dependencies = [ dependencies = [
"midir", "midir",
] ]

View File

@ -10,6 +10,7 @@ pub struct Config {
pub username: String, pub username: String,
pub password: String, pub password: String,
pub authtoken: Option<String>, pub authtoken: Option<String>,
pub refresh: Option<u64>,
pub limit: Option<i64>, pub limit: Option<i64>,
} }
@ -20,7 +21,8 @@ impl Config {
username: String::from("bob"), username: String::from("bob"),
password: String::from("password"), password: String::from("password"),
authtoken: Some(String::from("token")), authtoken: Some(String::from("token")),
limit: Some(20i64) refresh: Some(5u64),
limit: Some(20i64),
} }
} }

View File

@ -3,8 +3,13 @@ mod padcontrol;
mod zabbix; mod zabbix;
use config::Config; use config::Config;
//use std::rc::Rc;
//use std::vec::Vec;
use zabbix::problems::ZabbixLayout;
fn main() { fn main() {
let mut datamatrix = ZabbixLayout{layout: vec!()};
// parse arguments // parse arguments
let matches = config::argparse(); let matches = config::argparse();
@ -14,13 +19,11 @@ fn main() {
.unwrap_or("config.json"); .unwrap_or("config.json");
let mut cfg = Config::new(); let mut cfg = Config::new();
cfg.load(&configfile); cfg.load(&configfile);
zabbix::api::get_zabbix_authtoken(&mut cfg);
cfg.save(&configfile);
// init/connect to launchpad and clear it // init/connect to launchpad and clear it
let (mut canvas, mut poller) = padcontrol::initpad(); let (mut canvas, mut poller) = padcontrol::initpad();
padcontrol::input(&mut canvas, &mut poller); //padcontrol::input(&mut canvas, &mut poller);
padcontrol::draw(&mut canvas, &mut poller, &mut datamatrix, &mut cfg);
// fetch zabbix data
zabbix::get_zabbix_authtoken(&mut cfg);
zabbix::get_zabbix_problems(&mut cfg);
cfg.save(&configfile);
} }

View File

@ -1,5 +1,9 @@
use launchy::Color; use launchy::Color;
use launchy::{self, Canvas, CanvasLayout, CanvasLayoutPoller, MsgPollingWrapper, Pad}; use launchy::{self, Canvas, CanvasLayout, CanvasLayoutPoller, MsgPollingWrapper, Pad};
use crate::zabbix;
use crate::zabbix::problems::{ZabbixLayout};
use crate::config::Config;
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();
@ -25,6 +29,26 @@ pub fn input(canvas: &mut CanvasLayout, poller: &mut CanvasLayoutPoller) {
} }
} }
pub fn draw(canvas: &mut CanvasLayout, poller: &mut CanvasLayoutPoller, datamatrix: &mut ZabbixLayout, cfg: &mut Config) {
println!("Refresh rate is {} seconds", cfg.refresh.unwrap());
loop {
let zabbix_data = zabbix::api::get_zabbix_problems(cfg);
datamatrix.compute(&zabbix_data);
for (i, j) in datamatrix.layout.iter().enumerate() {
let p = Pad{x: i as i32, y: 1i32};
let c = match j.status {
4 => Color::RED,
3 => Color::YELLOW,
2 => Color::WHITE,
_ => Color::GREEN,
};
canvas[p] = c;
}
let _a = canvas.flush();
std::thread::sleep(std::time::Duration::from_secs(cfg.refresh.unwrap_or(5)));
}
}
/* /*
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)) {

69
src/zabbix/api.rs Normal file
View File

@ -0,0 +1,69 @@
use crate::config::Config;
use serde_json::{json, Value};
pub 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()
.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) -> Value {
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();
response.json().unwrap()
}
pub fn query_auth_token(zabbix_username: &String,
zabbix_password: &String) -> Value {
let api_function = "user.login";
let api_id = 1;
json!({
"jsonrpc": ZABBIX_API_VERSION,
"method": api_function,
"params": {
"user": zabbix_username,
"password": zabbix_password
},
"id": api_id
})
}
pub fn query_problems(zabbix_token: &String,
zabbix_limit: i64) -> Value {
let zabbix_api_function = "problem.get";
let zabbix_api_id = 1;
json!({
"jsonrpc": ZABBIX_API_VERSION,
"method": zabbix_api_function,
"params": {
"output": "extend",
"selectAcknowledges": "extend",
"selectTags": "extend",
"selectSuppressionData": "extend",
"recent": "true",
"sortfield": ["eventid"],
"sortorder": "DESC",
"limit": zabbix_limit
},
"auth": zabbix_token,
"id": zabbix_api_id
})
}

View File

@ -1,68 +1,2 @@
use crate::config::Config; pub mod api;
use serde_json::{json, Value}; pub mod problems;
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()
.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(zabbix_username: &String,
zabbix_password: &String) -> Value {
let api_function = "user.login";
let api_id = 1;
json!({
"jsonrpc": zabbix_api_version,
"method": api_function,
"params": {
"user": zabbix_username,
"password": zabbix_password
},
"id": api_id
})
}
pub fn query_problems(zabbix_token: &String,
zabbix_limit: i64) -> Value {
let zabbix_api_function = "problem.get";
let zabbix_api_id = 1;
json!({
"jsonrpc": zabbix_api_version,
"method": zabbix_api_function,
"params": {
"output": "extend",
"selectAcknowledges": "extend",
"selectTags": "extend",
"selectSuppressionData": "extend",
"recent": "true",
"sortfield": ["eventid"],
"sortorder": "DESC",
"limit": zabbix_limit
},
"auth": zabbix_token,
"id": zabbix_api_id
})
}

55
src/zabbix/problems.rs Normal file
View File

@ -0,0 +1,55 @@
use serde_json::Value;
use std::fmt::{Display,Formatter,Result};
#[derive(Debug)]
pub enum ZabbixStatus {
High = 4,
Average = 3,
Warning = 2,
Info = 1,
}
pub struct ZabbixLayout {
pub layout: Vec<ZabbixProblems>,
}
pub struct ZabbixProblems {
hostname: String,
pub status: i64,
}
impl Display for ZabbixProblems {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f,
"hostname: {}, status: {:?}",
self.hostname,
self.status)
}
}
impl ZabbixLayout {
pub fn compute(&mut self, input: &Value) {
self.layout = vec!();
for j in input.as_object().unwrap()["result"].as_array().unwrap()[0..8].to_vec() {
let severity = j.as_object()
.unwrap()["severity"]
.as_str()
.unwrap()
.parse::<i64>()
.unwrap();
let acknowledged = j.as_object()
.unwrap()["acknowledged"]
.as_str()
.unwrap()
.parse::<i64>()
.unwrap();
if acknowledged == 0 {
self.layout.push(
ZabbixProblems{
hostname: "test".to_string(),
status: severity
});
};
}
}
}