diff --git a/Cargo.lock b/Cargo.lock index 465f43d..6ec6340 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -457,6 +457,7 @@ dependencies = [ [[package]] name = "launchy" version = "0.1.0" +source = "git+https://github.com/kangalioo/launchy?branch=master#f066a4f8a2616dcb7b2b9b2b06f30136a80fef35" dependencies = [ "midir", ] diff --git a/src/config/mod.rs b/src/config/mod.rs index 4961bff..6101ab8 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -10,6 +10,7 @@ pub struct Config { pub username: String, pub password: String, pub authtoken: Option, + pub refresh: Option, pub limit: Option, } @@ -20,7 +21,8 @@ impl Config { username: String::from("bob"), password: String::from("password"), authtoken: Some(String::from("token")), - limit: Some(20i64) + refresh: Some(5u64), + limit: Some(20i64), } } diff --git a/src/main.rs b/src/main.rs index d7deb8f..709c89a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,13 @@ mod padcontrol; mod zabbix; use config::Config; +//use std::rc::Rc; +//use std::vec::Vec; +use zabbix::problems::ZabbixLayout; fn main() { + let mut datamatrix = ZabbixLayout{layout: vec!()}; + // parse arguments let matches = config::argparse(); @@ -14,13 +19,11 @@ fn main() { .unwrap_or("config.json"); let mut cfg = Config::new(); cfg.load(&configfile); + zabbix::api::get_zabbix_authtoken(&mut cfg); + cfg.save(&configfile); // init/connect to launchpad and clear it let (mut canvas, mut poller) = padcontrol::initpad(); - padcontrol::input(&mut canvas, &mut poller); - - // fetch zabbix data - zabbix::get_zabbix_authtoken(&mut cfg); - zabbix::get_zabbix_problems(&mut cfg); - cfg.save(&configfile); + //padcontrol::input(&mut canvas, &mut poller); + padcontrol::draw(&mut canvas, &mut poller, &mut datamatrix, &mut cfg); } \ No newline at end of file diff --git a/src/padcontrol/mod.rs b/src/padcontrol/mod.rs index 35e6ed0..fb79ee3 100644 --- a/src/padcontrol/mod.rs +++ b/src/padcontrol/mod.rs @@ -1,5 +1,9 @@ use launchy::Color; 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) { 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) { for color in (0u64..).map(|f| Color::red_green_color(f as f32 / 60.0 / 2.5)) { diff --git a/src/zabbix/api.rs b/src/zabbix/api.rs new file mode 100644 index 0000000..fc3a4fe --- /dev/null +++ b/src/zabbix/api.rs @@ -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 + }) +} \ No newline at end of file diff --git a/src/zabbix/mod.rs b/src/zabbix/mod.rs index bb3df10..77edc2b 100644 --- a/src/zabbix/mod.rs +++ b/src/zabbix/mod.rs @@ -1,68 +1,2 @@ -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() - .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 - }) -} \ No newline at end of file +pub mod api; +pub mod problems; diff --git a/src/zabbix/problems.rs b/src/zabbix/problems.rs new file mode 100644 index 0000000..a649070 --- /dev/null +++ b/src/zabbix/problems.rs @@ -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, +} + +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::() + .unwrap(); + let acknowledged = j.as_object() + .unwrap()["acknowledged"] + .as_str() + .unwrap() + .parse::() + .unwrap(); + if acknowledged == 0 { + self.layout.push( + ZabbixProblems{ + hostname: "test".to_string(), + status: severity + }); + }; + } + } +} \ No newline at end of file