monitoring with repeat and defaults

defaults can also be toggled off
This commit is contained in:
Christoph J. Scherr 2023-07-09 13:45:01 +02:00
parent 763f734649
commit 4c4203d98c
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
3 changed files with 46 additions and 16 deletions

View File

@ -15,6 +15,8 @@
#![warn(clippy::pedantic)] #![warn(clippy::pedantic)]
//// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// //// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
use libpt;
use clap::{Args, Parser, Subcommand}; use clap::{Args, Parser, Subcommand};
use clap_num::number_range; use clap_num::number_range;
@ -85,14 +87,20 @@ pub enum Commands {
pub enum NetCommands { pub enum NetCommands {
/// monitor your network /// monitor your network
Monitor { Monitor {
#[clap(short, long)] /// repeat every N milliseconds, 0 means no repeat
repeat: bool, #[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)] #[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()])] /// extra URLs to check with
additional_domains: Vec<String>, extra_urls: Vec<String>,
/// Don't check for default URLs
#[clap(short, long)]
no_default: bool
}, },
/// discover hosts in your network /// discover hosts in your network

View File

@ -12,8 +12,8 @@
#![warn(clippy::pedantic)] #![warn(clippy::pedantic)]
//// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// //// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
use libpt::networking::monitoring::uptime;
use libpt::logger; use libpt::logger;
use libpt::networking::monitoring::uptime;
// we want the log macros in any case // we want the log macros in any case
#[allow(unused_imports)] #[allow(unused_imports)]
@ -66,16 +66,33 @@ fn net(cli: &Cli, command: NetCommands) {
match command { match command {
NetCommands::Monitor { NetCommands::Monitor {
repeat, repeat,
percentage_for_success, success_ratio,
additional_domains, extra_urls,
no_default
} => { } => {
let status: uptime::UptimeStatus = uptime::check_status( let urls: Vec<String>;
additional_domains, if no_default {
percentage_for_success, urls = extra_urls;
); }
else {
let mut combined: Vec<String> = 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(); 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 {} => {} NetCommands::Discover {} => {}
} }

View File

@ -27,6 +27,11 @@ use reqwest;
pub type UptimeStatus = (bool, usize, usize); pub type UptimeStatus = (bool, usize, usize);
//// CONSTANTS ///////////////////////////////////////////////////////////////////////////////////// //// CONSTANTS /////////////////////////////////////////////////////////////////////////////////////
/// urls used for checking by default
pub const DEFAULT_CHECK_URLS: &'static [&'static str] = &[
"https://www.cscherr.de",
"https://www.cloudflare.com"
];
//// STATICS /////////////////////////////////////////////////////////////////////////////////////// //// STATICS ///////////////////////////////////////////////////////////////////////////////////////
@ -53,7 +58,7 @@ pub type UptimeStatus = (bool, usize, usize);
/// ///
/// #### `status` /// #### `status`
/// Will be `true` if the check is considered a success. /// Will be `true` if the check is considered a success.
pub fn check_status(urls_strs: Vec<String>, percentage_for_success: u8) -> UptimeStatus { pub fn check_status(urls_strs: &Vec<String>, percentage_for_success: u8) -> UptimeStatus {
if percentage_for_success > 100 { if percentage_for_success > 100 {
panic!("percentage_for_success is over 100: {percentage_for_success}") panic!("percentage_for_success is over 100: {percentage_for_success}")
} }
@ -99,7 +104,7 @@ pub fn check_status(urls_strs: Vec<String>, percentage_for_success: u8) -> Uptim
/// ## display UptimeStatus /// ## display UptimeStatus
/// ///
/// returns a fancy string that shows the UptimeStatus, so you can print it to the user. /// 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!( format!(
r"{{ r"{{
success: {}, success: {},