linux-monitor 0.2.7

A lightweight Linux monitoring tool, need to be used with api-server.
#[cfg(target_os = "linux")]
use crate::http;
#[cfg(target_os = "linux")]
use crate::models::SysInfoRecord;
#[cfg(target_os = "linux")]
use sys_info;
#[cfg(target_os = "linux")]
use sys_info::DiskInfo;
#[cfg(target_os = "linux")]
use tokio::join;

/// System running time, unit: second
#[cfg(target_os = "linux")]
async fn get_boot_time() -> i64 {
    let boot_time = sys_info::boottime().unwrap();
    let sec: i64 = boot_time.tv_sec;
    sec
}

/// Get the number of CPUs
#[cfg(target_os = "linux")]
async fn get_cpu_num() -> u32 {
    let n = sys_info::cpu_num().unwrap();
    n
}

/// CPU frequency, unit: MHz
#[cfg(target_os = "linux")]
async fn get_cpu_speed() -> u64 {
    let speed = sys_info::cpu_speed().unwrap();
    speed
}

/// Get disk space details
#[cfg(target_os = "linux")]
async fn get_disk_info() -> DiskInfo {
    let disk_info = sys_info::disk_info().unwrap();
    disk_info
}

/// get hostname
#[cfg(target_os = "linux")]
async fn get_hostname() -> String {
    let hostname = sys_info::hostname().unwrap();
    hostname
}

/// Get the total system memory size
#[cfg(target_os = "linux")]
async fn get_mem_total() -> u64 {
    let total = sys_info::mem_info().unwrap().total;
    total
}

/// Get operating system name
#[cfg(target_os = "linux")]
async fn get_os_name() -> String {
    let os_name = sys_info::linux_os_release().unwrap().pretty_name.unwrap();
    os_name
}

/// Load average in one minute
#[cfg(target_os = "linux")]
async fn get_load_avg_one() -> f64 {
    let load_avg = sys_info::loadavg().unwrap().one;
    load_avg
}

/// Get the total number of system processes
#[cfg(target_os = "linux")]
async fn get_proc_total() -> u64 {
    let t = sys_info::proc_total().unwrap();
    t
}

/// Push system data to Server
#[cfg(target_os = "linux")]
pub async fn push_sys_data(timestamp: u64, url: &str, group_name: &str) {
    let boot_time_sec_f = get_boot_time();
    let cpu_num_f = get_cpu_num();
    let cpu_speed_f = get_cpu_speed();
    let disk_info_f = get_disk_info();
    let hostname_f = get_hostname();
    let mem_total_f = get_mem_total();
    let os_name_f = get_os_name();
    let load_avg_one_f = get_load_avg_one();
    let proc_total_f = get_proc_total();
    let (
        boot_time_sec,
        cpu_num,
        cpu_speed,
        disk_info,
        hostname,
        mem_total,
        os_name,
        load_avg_one,
        proc_total,
    ) = join!(
        boot_time_sec_f,
        cpu_num_f,
        cpu_speed_f,
        disk_info_f,
        hostname_f,
        mem_total_f,
        os_name_f,
        load_avg_one_f,
        proc_total_f
    );

    let sys_info = SysInfoRecord::new(
        boot_time_sec,
        cpu_num,
        cpu_speed,
        mem_total,
        disk_info.total,
        disk_info.free,
        hostname,
        os_name,
        load_avg_one,
        proc_total,
        timestamp,
        group_name.to_string(),
    );
    http::try_post(url, &sys_info, 3).await;
}

#[cfg(target_os = "linux")]
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_all_fn() {}
}