From 4c4203d98c51719a04296341d70153da79341b5e Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 9 Jul 2023 13:45:01 +0200 Subject: [PATCH] monitoring with repeat and defaults defaults can also be toggled off --- src/bin/main/args.rs | 18 ++++++++++----- src/bin/main/mod.rs | 35 +++++++++++++++++++++-------- src/networking/monitoring/uptime.rs | 9 ++++++-- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/bin/main/args.rs b/src/bin/main/args.rs index 75d8743..a062a1e 100644 --- a/src/bin/main/args.rs +++ b/src/bin/main/args.rs @@ -15,6 +15,8 @@ #![warn(clippy::pedantic)] //// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// +use libpt; + use clap::{Args, Parser, Subcommand}; use clap_num::number_range; @@ -85,14 +87,20 @@ pub enum Commands { pub enum NetCommands { /// monitor your network Monitor { - #[clap(short, long)] - repeat: bool, + /// repeat every N milliseconds, 0 means no repeat + #[clap(short, long, default_value_t = 0, name = "N")] + repeat: u64, + /// At what percentage should the try be considered successful #[clap(short, long, default_value_t = 100, value_parser=max100)] - percentage_for_success: u8, + success_ratio: u8, - #[arg(default_values_t = ["https://cloudflare.com".to_string()])] - additional_domains: Vec, + /// extra URLs to check with + extra_urls: Vec, + + /// Don't check for default URLs + #[clap(short, long)] + no_default: bool }, /// discover hosts in your network diff --git a/src/bin/main/mod.rs b/src/bin/main/mod.rs index 6d3f84c..1e67d12 100644 --- a/src/bin/main/mod.rs +++ b/src/bin/main/mod.rs @@ -12,8 +12,8 @@ #![warn(clippy::pedantic)] //// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// -use libpt::networking::monitoring::uptime; use libpt::logger; +use libpt::networking::monitoring::uptime; // we want the log macros in any case #[allow(unused_imports)] @@ -66,16 +66,33 @@ fn net(cli: &Cli, command: NetCommands) { match command { NetCommands::Monitor { repeat, - percentage_for_success, - additional_domains, + success_ratio, + extra_urls, + no_default } => { - let status: uptime::UptimeStatus = uptime::check_status( - additional_domains, - percentage_for_success, - ); + let urls: Vec; + if no_default { + urls = extra_urls; + } + else { + let mut combined: Vec = Vec::new(); + for i in uptime::DEFAULT_CHECK_URLS { + combined.push(i.to_string()); + } + combined.extend(extra_urls); + urls = combined; + } let _verbose = cli.verbose.log_level().is_some(); - println!("{}", uptime::display_uptime_status(status)); - + if repeat > 0 { + loop { + let status: uptime::UptimeStatus = uptime::check_status(&urls, success_ratio); + println!("{}", uptime::display_uptime_status(&status)); + std::thread::sleep(std::time::Duration::from_millis(repeat)) + } + } else { + let status: uptime::UptimeStatus = uptime::check_status(&urls, success_ratio); + println!("{}", uptime::display_uptime_status(&status)); + } } NetCommands::Discover {} => {} } diff --git a/src/networking/monitoring/uptime.rs b/src/networking/monitoring/uptime.rs index e1fd56d..857ef4c 100644 --- a/src/networking/monitoring/uptime.rs +++ b/src/networking/monitoring/uptime.rs @@ -27,6 +27,11 @@ use reqwest; pub type UptimeStatus = (bool, usize, usize); //// CONSTANTS ///////////////////////////////////////////////////////////////////////////////////// +/// urls used for checking by default +pub const DEFAULT_CHECK_URLS: &'static [&'static str] = &[ + "https://www.cscherr.de", + "https://www.cloudflare.com" +]; //// STATICS /////////////////////////////////////////////////////////////////////////////////////// @@ -53,7 +58,7 @@ pub type UptimeStatus = (bool, usize, usize); /// /// #### `status` /// Will be `true` if the check is considered a success. -pub fn check_status(urls_strs: Vec, percentage_for_success: u8) -> UptimeStatus { +pub fn check_status(urls_strs: &Vec, percentage_for_success: u8) -> UptimeStatus { if percentage_for_success > 100 { panic!("percentage_for_success is over 100: {percentage_for_success}") } @@ -99,7 +104,7 @@ pub fn check_status(urls_strs: Vec, percentage_for_success: u8) -> Uptim /// ## display UptimeStatus /// /// returns a fancy string that shows the UptimeStatus, so you can print it to the user. -pub fn display_uptime_status(status: UptimeStatus) -> String { +pub fn display_uptime_status(status: &UptimeStatus) -> String { format!( r"{{ success: {},