updated zabbixlaunch
This commit is contained in:
parent
56eedaf040
commit
8b22768886
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -457,6 +457,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "launchy"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/kangalioo/launchy?branch=master#f066a4f8a2616dcb7b2b9b2b06f30136a80fef35"
|
||||
dependencies = [
|
||||
"midir",
|
||||
]
|
||||
|
@ -10,6 +10,7 @@ pub struct Config {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub authtoken: Option<String>,
|
||||
pub refresh: Option<u64>,
|
||||
pub limit: Option<i64>,
|
||||
}
|
||||
|
||||
@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
|
15
src/main.rs
15
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);
|
||||
}
|
@ -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)) {
|
||||
|
69
src/zabbix/api.rs
Normal file
69
src/zabbix/api.rs
Normal 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
|
||||
})
|
||||
}
|
@ -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
|
||||
})
|
||||
}
|
||||
pub mod api;
|
||||
pub mod problems;
|
||||
|
55
src/zabbix/problems.rs
Normal file
55
src/zabbix/problems.rs
Normal 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
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user