linux-monitor 0.2.7

A lightweight Linux monitoring tool, need to be used with api-server.
use serde::Serialize;
use serde_json::Value;
use std::thread::sleep;
use std::time::Duration;

/// Check by get request
pub fn check_get(url: &str) -> bool {
    let client = reqwest::blocking::Client::new();
    let r = client.get(url).timeout(Duration::from_secs(3)).send();
    return match r {
        Ok(res) => res.status().is_success(),
        Err(e) => {
            eprintln!("Server connection failed, {}", e.to_string());
            false
        }
    };
}

/// post general request
pub async fn post<T: Serialize>(url: &str, params: &T) -> bool {
    let client = reqwest::Client::builder()
        .cookie_store(true)
        .build()
        .unwrap();
    let res = client
        .post(url)
        .timeout(Duration::from_secs(10))
        .json(params)
        .send()
        .await;
    match res {
        Ok(r) => {
            let response = r.text().await.unwrap();
            let res_json: Value = serde_json::from_str(response.as_str()).unwrap();
            let code_r = res_json["code"].as_i64();
            return match code_r {
                Some(code) => {
                    if code == 200 {
                        return true;
                    }
                    false
                }
                None => {
                    eprintln!("Get response code error");
                    false
                }
            };
        }
        Err(e) => {
            eprintln!("Post request failed:{}", e);
            false
        }
    }
}

///
pub async fn try_post<T: Serialize>(url: &str, params: &T, t: i32) {
    for i in 0..t {
        let r = post(url, params).await;
        if r {
            break;
        }
        println!(
            "Failed to push data for the {}th time, waiting for retry...",
            i
        );
        sleep(Duration::from_secs(1));
    }
}