updated zabbixlaunch

This commit is contained in:
Paul Lecuq 2021-09-13 18:30:58 +02:00
parent b73fb753be
commit aeb42422d8
4 changed files with 58 additions and 26 deletions

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 sloweffect: Option<bool>,
pub refresh: Option<u64>, pub refresh: Option<u64>,
pub limit: Option<i64>, pub limit: Option<i64>,
} }
@ -21,6 +22,7 @@ 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")),
sloweffect: Some(true),
refresh: Some(5u64), refresh: Some(5u64),
limit: Some(20i64), limit: Some(20i64),
} }
@ -30,6 +32,9 @@ impl Config {
if self.authtoken.is_none() { if self.authtoken.is_none() {
self.authtoken = Some(String::from("")); self.authtoken = Some(String::from(""));
} }
if self.sloweffect.is_none() {
self.sloweffect = Some(true);
}
if self.refresh.is_none() { if self.refresh.is_none() {
self.refresh = Some(5u64); self.refresh = Some(5u64);
} }

View File

@ -1,11 +1,15 @@
use std::time::Duration;
use std::thread::sleep;
use crate::config::Config; use crate::config::Config;
use crate::zabbix::api::get_zabbix_problems; use crate::zabbix::api::get_zabbix_problems;
use crate::zabbix::problems::ZabbixLayout; use crate::zabbix::problems::ZabbixLayout;
use launchy::Color; use launchy::Color;
use launchy::{self, Canvas, CanvasLayout, CanvasLayoutPoller, MsgPollingWrapper, Pad}; use launchy::{self, Canvas, CanvasLayout, CanvasLayoutPoller, MsgPollingWrapper, Pad};
use std::io::Write;
pub const MATRIX_SIZE: i32 = 8;
pub const MATRIX_WIDTH: i32 = 8;
pub const MATRIX_SIZE: i32 = MATRIX_WIDTH*MATRIX_WIDTH;
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();
@ -35,29 +39,36 @@ pub fn draw(canvas: &mut CanvasLayout, datamatrix: &mut ZabbixLayout, cfg: &mut
println!("Refresh rate is {} seconds", cfg.refresh.unwrap()); println!("Refresh rate is {} seconds", cfg.refresh.unwrap());
loop { loop {
let zabbix_data = get_zabbix_problems(cfg); let zabbix_data = get_zabbix_problems(cfg);
print!(".");
std::io::stdout().flush().unwrap();
datamatrix.compute(&zabbix_data); datamatrix.compute(&zabbix_data);
clear(canvas);
let mut x = 0i32; let mut x = 0i32;
let mut y = 1i32; let mut y = 1i32;
let mut xx = 0i32;
for j in datamatrix.layout.iter() { for j in datamatrix.layout.iter() {
let p = Pad { x: x, y: y }; let p = Pad { x: x, y: y };
let c = match j.status { let c = match j.status {
4 => Color::RED, 5 => Color::RED,
3 => Color::from_hue(0.05), 4 => Color::from_hue(0.05),
2 => Color::from_hue(1.24), 3 => Color::from_hue(0.1),
2 => Color::from_hue(1.22),
_ => Color::GREEN, _ => Color::GREEN,
}; };
canvas[p] = c; canvas[p] = c;
x += 1; x += 1;
if x % MATRIX_SIZE == 0 { xx += 1;
if x % MATRIX_WIDTH == 0 {
y += 1; y += 1;
x = 0; x = 0;
}; };
if xx >= MATRIX_SIZE {
break;
}
if cfg.sloweffect.unwrap() {
sleep(Duration::from_millis(15));
}
canvas.flush().unwrap();
} }
canvas.flush().unwrap(); sleep(Duration::from_secs(cfg.refresh.unwrap_or(5)));
std::io::stdout().flush().unwrap();
std::thread::sleep(std::time::Duration::from_secs(cfg.refresh.unwrap_or(5)));
} }
} }

View File

@ -17,9 +17,8 @@ pub fn get_zabbix_authtoken(cfg: &mut Config) {
} }
pub fn get_zabbix_problems(cfg: &mut Config) -> Value { pub fn get_zabbix_problems(cfg: &mut Config) -> Value {
let body = query_problems( let body = query_triggers(
&cfg.authtoken.as_ref().unwrap_or(&String::from("")), &cfg.authtoken.as_ref().unwrap_or(&String::from("")),
cfg.limit.unwrap(),
); );
let resp = reqwest::blocking::Client::new() let resp = reqwest::blocking::Client::new()
.post(&cfg.server) .post(&cfg.server)
@ -43,20 +42,44 @@ pub fn query_auth_token(zabbix_username: &String, zabbix_password: &String) -> V
}) })
} }
pub fn query_problems(zabbix_token: &String, zabbix_limit: i64) -> Value { fn _query_problems(zabbix_token: &String, zabbix_limit: i64) -> Value {
let zabbix_api_function = "problem.get"; let zabbix_api_function = "problem.get";
json!({ json!({
"jsonrpc": ZABBIX_API_VERSION, "jsonrpc": ZABBIX_API_VERSION,
"method": zabbix_api_function, "method": zabbix_api_function,
"params": { "params": {
"acknowledged": false,
"limit": zabbix_limit,
"output": "extend", "output": "extend",
"recent": true,
"selectAcknowledges": "extend", "selectAcknowledges": "extend",
"selectTags": "extend",
"selectSuppressionData": "extend", "selectSuppressionData": "extend",
"recent": "true", "selectTags": "extend",
"sortfield": ["eventid"], "sortfield": ["eventid"],
"sortorder": "DESC", "sortorder": "DESC",
"limit": zabbix_limit "suppressed": false,
},
"auth": zabbix_token,
"id": ZABBIX_API_ID
})
}
pub fn query_triggers(zabbix_token: &String) -> Value {
let zabbix_api_function = "trigger.get";
json!({
"jsonrpc": ZABBIX_API_VERSION,
"method": zabbix_api_function,
"params": {
"output": "extend",
"withLastEventUnacknowledged": 1,
"sortfield": "lastchange",
"sortorder": "DESC",
"only_true": 1,
"monitored": 1,
"active":1,
"selectHosts": "extend",
"min_severity": 1,
}, },
"auth": zabbix_token, "auth": zabbix_token,
"id": ZABBIX_API_ID "id": ZABBIX_API_ID

View File

@ -28,15 +28,8 @@ impl ZabbixLayout {
self.layout = Vec::new(); self.layout = Vec::new();
for j in input.as_object().unwrap()["result"].as_array().unwrap() { for j in input.as_object().unwrap()["result"].as_array().unwrap() {
let res = j.as_object().unwrap(); let res = j.as_object().unwrap();
let severity = res["severity"].as_str().unwrap().parse::<i64>().unwrap(); let severity = res["priority"].as_str().unwrap().parse::<i64>().unwrap();
let ack = res["acknowledged"] self.layout.push(ZabbixProblems { status: severity });
.as_str()
.unwrap()
.parse::<i64>()
.unwrap();
if ack == 0 {
self.layout.push(ZabbixProblems { status: severity });
};
} }
} }
} }