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)]
//// 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<String>,
/// extra URLs to check with
extra_urls: Vec<String>,
/// Don't check for default URLs
#[clap(short, long)]
no_default: bool
},
/// discover hosts in your network

View File

@ -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<String>;
if no_default {
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();
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 {} => {}
}

View File

@ -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<String>, percentage_for_success: u8) -> UptimeStatus {
pub fn check_status(urls_strs: &Vec<String>, 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<String>, 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: {},