cleaned-up code and added compiler optimizations
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Paul Lecuq 2022-03-04 10:14:53 +01:00
parent 9fefb6e8a0
commit 2f1be523f8
5 changed files with 23 additions and 38 deletions

17
Cargo.lock generated
View File

@ -104,12 +104,10 @@ dependencies = [
"atty", "atty",
"bitflags", "bitflags",
"indexmap", "indexmap",
"lazy_static",
"os_str_bytes", "os_str_bytes",
"strsim", "strsim",
"termcolor", "termcolor",
"textwrap", "textwrap",
"yaml-rust",
] ]
[[package]] [[package]]
@ -443,12 +441,6 @@ version = "0.2.103"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8f7255a17a627354f321ef0055d63b898c6fb27eff628af4d1b66b7331edf6" checksum = "dd8f7255a17a627354f321ef0055d63b898c6fb27eff628af4d1b66b7331edf6"
[[package]]
name = "linked-hash-map"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.14" version = "0.4.14"
@ -1132,15 +1124,6 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "yaml-rust"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
dependencies = [
"linked-hash-map",
]
[[package]] [[package]]
name = "zabbixlaunch" name = "zabbixlaunch"
version = "1.0.0" version = "1.0.0"

View File

@ -9,7 +9,7 @@ repository = "https://git.paulbsd.com/paulbsd/zabbixlaunch"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
clap = { version = "3", features = ["cargo", "yaml"] } clap = "3"
embedded-graphics = { version = "0.7", optional = true } embedded-graphics = { version = "0.7", optional = true }
launchy = { git = "https://github.com/kangalioo/launchy", branch = "master" } launchy = { git = "https://github.com/kangalioo/launchy", branch = "master" }
reqwest = { version = "0.11", default-features = false, features = ["blocking", "json", "rustls-tls"] } reqwest = { version = "0.11", default-features = false, features = ["blocking", "json", "rustls-tls"] }
@ -19,3 +19,5 @@ nix = "0.23"
[profile.release] [profile.release]
debug = 0 debug = 0
lto = true
codegen-units = 1

View File

@ -66,8 +66,9 @@ impl Context {
.short('c') .short('c')
.long("config") .long("config")
.value_name("FILE") .value_name("FILE")
.help("Sets a custom config file") .default_value("/etc/zabbixlaunch/config.json")
.takes_value(true), .takes_value(true)
.help("Sets a custom config file"),
) )
.arg( .arg(
Arg::new("mode") Arg::new("mode")
@ -75,8 +76,9 @@ impl Context {
.long("mode") .long("mode")
.value_name("mode") .value_name("mode")
.default_value("draw") .default_value("draw")
.help("Sets a when running") .possible_values(&["draw", "test", "input", "effect"])
.takes_value(true), .takes_value(true)
.help("Sets a when running"),
) )
.arg( .arg(
Arg::new("debug") Arg::new("debug")
@ -138,7 +140,7 @@ impl<'a> Config {
pub fn load(&'a mut self, configfile: &str) { pub fn load(&'a mut self, configfile: &str) {
let fileopen: Result<File, std::io::Error>; let fileopen: Result<File, std::io::Error>;
let filemeta = std::fs::metadata(configfile); let filemeta = std::fs::metadata(configfile);
let mut errorreading = false; let mut error_reading = false;
let fileexists = match filemeta { let fileexists = match filemeta {
Ok(_) => true, Ok(_) => true,
Err(_) => false, Err(_) => false,
@ -158,7 +160,7 @@ impl<'a> Config {
let mut contents = String::new(); let mut contents = String::new();
file.read_to_string(&mut contents).unwrap(); file.read_to_string(&mut contents).unwrap();
let parse: Result<JsonValue, JsonError> = serde_json::from_str(contents.as_str()); let parse: Result<JsonValue, JsonError> = serde_json::from_str(contents.as_str());
let newcfg = match parse { let ncfg = match parse {
Ok(ncfg) => { Ok(ncfg) => {
let tmpcfg = Config::new(); let tmpcfg = Config::new();
Config { Config {
@ -190,13 +192,13 @@ impl<'a> Config {
} }
} }
Err(err) => { Err(err) => {
errorreading = true; error_reading = true;
println!("error occured: '{}'", err); println!("error occured: '{}'", err);
Config::new() Config::new()
} }
}; };
*self = newcfg; *self = ncfg;
if !fileexists || errorreading { if !fileexists || error_reading {
self.save(&configfile); self.save(&configfile);
} }
} }

View File

@ -1,8 +1,8 @@
use std::time::Duration;
use std::thread::sleep;
use crate::config::Config; use crate::config::Config;
use serde_json::json; use serde_json::json;
use serde_json::Value as JsonValue; use serde_json::Value as JsonValue;
//use std::thread::sleep;
//use std::time::Duration;
pub const ZABBIX_API_VERSION: &'static str = "2.0"; pub const ZABBIX_API_VERSION: &'static str = "2.0";
pub const ZABBIX_API_ID: i32 = 1; pub const ZABBIX_API_ID: i32 = 1;
@ -36,15 +36,13 @@ pub fn get_zabbix_problems(cfg: &Config) -> Result<JsonValue, reqwest::Error> {
match resp { match resp {
Ok(v) => v.json(), Ok(v) => v.json(),
Err(e) => { Err(e) => Err(e),
Err(e)
}
} }
} }
/// Check if Zabbix is operational /// Check if Zabbix is operational
/// Undefinitely check that Zabbix works /// Undefinitely check that Zabbix works
fn check_zabbix_connection(cfg: &Config) -> bool { /*fn check_zabbix_connection(cfg: &Config) -> bool {
let mut res: bool = false; let mut res: bool = false;
let delay = 5; let delay = 5;
let timeout = 10; let timeout = 10;
@ -60,7 +58,7 @@ fn check_zabbix_connection(cfg: &Config) -> bool {
sleep(Duration::from_secs(delay)); sleep(Duration::from_secs(delay));
} }
res res
} }*/
/// Build the query that fetchs the token /// Build the query that fetchs the token
fn build_query_auth_token(zabbix_username: &String, zabbix_password: &String) -> JsonValue { fn build_query_auth_token(zabbix_username: &String, zabbix_password: &String) -> JsonValue {
@ -77,7 +75,7 @@ fn build_query_auth_token(zabbix_username: &String, zabbix_password: &String) ->
} }
/// Build the query that fetchs problems /// Build the query that fetchs problems
fn build_query_problems(zabbix_token: &String, zabbix_limit: i64) -> JsonValue { /*fn build_query_problems(zabbix_token: &String, zabbix_limit: i64) -> JsonValue {
let zabbix_api_function = "problem.get"; let zabbix_api_function = "problem.get";
json!({ json!({
"jsonrpc": ZABBIX_API_VERSION, "jsonrpc": ZABBIX_API_VERSION,
@ -97,7 +95,7 @@ fn build_query_problems(zabbix_token: &String, zabbix_limit: i64) -> JsonValue {
"auth": zabbix_token, "auth": zabbix_token,
"id": ZABBIX_API_ID "id": ZABBIX_API_ID
}) })
} }*/
/// Build the query that fetchs triggers /// Build the query that fetchs triggers
fn build_query_triggers(zabbix_token: &String) -> JsonValue { fn build_query_triggers(zabbix_token: &String) -> JsonValue {

View File

@ -1,14 +1,14 @@
use serde_json::Value; use serde_json::Value;
use std::fmt::{Display, Formatter, Result}; use std::fmt::{Display, Formatter, Result};
#[derive(Debug, Clone)] /*#[derive(Debug, Clone)]
pub enum ZabbixStatus { pub enum ZabbixStatus {
Disaster = 5, Disaster = 5,
High = 4, High = 4,
Average = 3, Average = 3,
Warning = 2, Warning = 2,
Info = 1, Info = 1,
} }*/
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct DataMatrix { pub struct DataMatrix {