linux-monitor 0.2.7

A lightweight Linux monitoring tool, need to be used with api-server.
extern crate core;

#[cfg(target_os = "linux")]
mod http;
#[cfg(target_os = "linux")]
mod sys_libc_util;
#[cfg(target_os = "linux")]
mod sys_proc_util;
#[cfg(target_os = "linux")]
mod url_util;
mod models;

#[cfg(target_os = "linux")]
use monitor_common::get_timestamp;
#[cfg(target_os = "linux")]
use std::net::Ipv4Addr;
#[cfg(target_os = "linux")]
use std::thread::sleep;
#[cfg(target_os = "linux")]
use std::time::Duration;
#[cfg(target_os = "linux")]
use tokio::join;

#[cfg(target_os = "linux")]
use clap::Parser;
#[cfg(target_os = "linux")]
use rcron::{Job, JobScheduler};

/// A lightweight Linux monitoring tool, need to be used with api-server.
#[cfg(target_os = "linux")]
#[derive(Parser, Debug)]
#[clap(version, author = "Seeker <womeng209@qq.com>")]
struct Options {
    /// Api server IP address
    #[clap(short, long, value_parser)]
    server: Ipv4Addr,

    /// Api server port
    #[clap(short, long, value_parser, default_value_t = 8080)]
    port: u32,

    /// The group name of the current node, used for grouping all monitored nodes
    #[clap(short, long, value_parser, default_value_t = String::from("dev"))]
    group: String,
}

/// monitor main function
fn main() {
    if !cfg!(target_os = "linux") {
        eprintln!("Only supports Linux operating system");
        std::process::exit(-1);
    }

    #[cfg(target_os = "linux")]
    {
        let options = Options::parse();
        let server: Ipv4Addr = options.server;
        let server = server.to_string();
        let port: u32 = options.port;
        let url = url_util::generate_server_url(server.as_str(), port);
        let b = http::check_get(url.as_str());
        if b {
            let monitor_url = url_util::splicing_monitor_url(url.as_str());
            let sys_url = url_util::splicing_sys_url(url.as_str());
            let group_name: String = options.group;

            let rt = tokio::runtime::Runtime::new().unwrap();
            let mut sched = JobScheduler::new();

            sched.add(Job::new("0 0/1 * * * *".parse().unwrap(), move || {
                rt.block_on(async {
                    let timestamp = get_timestamp().await;
                    let push_monitor_f = sys_proc_util::push_monitor_data(
                        timestamp,
                        monitor_url.as_str(),
                        group_name.as_str(),
                    );
                    let push_sys_f = sys_libc_util::push_sys_data(
                        timestamp,
                        sys_url.as_str(),
                        group_name.as_str(),
                    );
                    join!(push_monitor_f, push_sys_f);
                });
            }));

            loop {
                sched.tick();

                sleep(Duration::from_millis(500));
            }
        }
    }
}