123 lines
3.5 KiB
Rust
123 lines
3.5 KiB
Rust
use std::time::Duration;
|
|
use std::thread::sleep;
|
|
use crate::config::Config;
|
|
use serde_json::json;
|
|
use serde_json::Value as JsonValue;
|
|
|
|
pub const ZABBIX_API_VERSION: &'static str = "2.0";
|
|
pub const ZABBIX_API_ID: i32 = 1;
|
|
|
|
/// Refresh the user token
|
|
pub fn get_zabbix_authtoken(cfg: &mut Config) {
|
|
let body = build_query_auth_token(&cfg.username, &cfg.password);
|
|
let resp = reqwest::blocking::Client::new()
|
|
.post(&cfg.server)
|
|
.json(&body)
|
|
.send();
|
|
match resp {
|
|
Ok(v) => {
|
|
let values: JsonValue = v.json().unwrap();
|
|
cfg.authtoken = Some(values["result"].as_str().unwrap().to_string());
|
|
}
|
|
Err(e) => {
|
|
println!("{}", e);
|
|
cfg.authtoken = Some("".to_string());
|
|
}
|
|
};
|
|
}
|
|
|
|
/// Fetch Zabbix problems
|
|
pub fn get_zabbix_problems(cfg: &Config) -> Result<JsonValue, reqwest::Error> {
|
|
let body = build_query_triggers(&cfg.authtoken.as_ref().unwrap_or(&String::from("")));
|
|
let resp = reqwest::blocking::Client::new()
|
|
.post(&cfg.server)
|
|
.json(&body)
|
|
.send();
|
|
|
|
match resp {
|
|
Ok(v) => v.json(),
|
|
Err(e) => {
|
|
Err(e)
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Check if Zabbix is operational
|
|
/// Undefinitely check that Zabbix works
|
|
fn check_zabbix_connection(cfg: &Config) -> bool {
|
|
let mut res: bool = false;
|
|
let delay = 5;
|
|
let timeout = 10;
|
|
while !res {
|
|
let req = reqwest::blocking::Client::builder().timeout(Duration::from_secs(timeout)).build().unwrap();
|
|
let resp = req.get(&cfg.server)
|
|
.send();
|
|
match resp {
|
|
Ok(_) => res = true,
|
|
Err(_) => res = false
|
|
}
|
|
println!("Waiting for {delay}", delay=delay);
|
|
sleep(Duration::from_secs(delay));
|
|
}
|
|
res
|
|
}
|
|
|
|
/// Build the query that fetchs the token
|
|
fn build_query_auth_token(zabbix_username: &String, zabbix_password: &String) -> JsonValue {
|
|
let zabbix_api_function = "user.login";
|
|
json!({
|
|
"jsonrpc": ZABBIX_API_VERSION,
|
|
"method": zabbix_api_function,
|
|
"params": {
|
|
"user": zabbix_username,
|
|
"password": zabbix_password
|
|
},
|
|
"id": ZABBIX_API_ID
|
|
})
|
|
}
|
|
|
|
/// Build the query that fetchs problems
|
|
fn build_query_problems(zabbix_token: &String, zabbix_limit: i64) -> JsonValue {
|
|
let zabbix_api_function = "problem.get";
|
|
json!({
|
|
"jsonrpc": ZABBIX_API_VERSION,
|
|
"method": zabbix_api_function,
|
|
"params": {
|
|
"acknowledged": false,
|
|
"limit": zabbix_limit,
|
|
"output": "extend",
|
|
"recent": true,
|
|
"selectAcknowledges": "extend",
|
|
"selectSuppressionData": "extend",
|
|
"selectTags": "extend",
|
|
"sortfield": ["eventid"],
|
|
"sortorder": "DESC",
|
|
"suppressed": false,
|
|
},
|
|
"auth": zabbix_token,
|
|
"id": ZABBIX_API_ID
|
|
})
|
|
}
|
|
|
|
/// Build the query that fetchs triggers
|
|
fn build_query_triggers(zabbix_token: &String) -> JsonValue {
|
|
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,
|
|
"id": ZABBIX_API_ID
|
|
})
|
|
}
|