generated from PlexSheep/baserepo
doc fixes
This commit is contained in:
parent
b8ab5eaac3
commit
9fb82b5caa
|
@ -12,7 +12,6 @@
|
||||||
#![warn(clippy::pedantic)]
|
#![warn(clippy::pedantic)]
|
||||||
|
|
||||||
//// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
|
//// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
|
||||||
use libpt::logger;
|
|
||||||
use libpt::networking::monitoring::uptime;
|
use libpt::networking::monitoring::uptime;
|
||||||
|
|
||||||
// we want the log macros in any case
|
// we want the log macros in any case
|
||||||
|
@ -43,9 +42,6 @@ use args::*;
|
||||||
//// PRIVATE FUNCTIONS /////////////////////////////////////////////////////////////////////////////
|
//// PRIVATE FUNCTIONS /////////////////////////////////////////////////////////////////////////////
|
||||||
/// ## Main function of the `pt` binary
|
/// ## Main function of the `pt` binary
|
||||||
fn main() {
|
fn main() {
|
||||||
#[cfg(debug_assertions)]
|
|
||||||
std::env::set_var(logger::LOGGER_ENV_KEY, "trace");
|
|
||||||
|
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
if cli.log_meta {
|
if cli.log_meta {
|
||||||
// set up our logger to use the given verbosity
|
// set up our logger to use the given verbosity
|
||||||
|
|
|
@ -11,7 +11,8 @@
|
||||||
|
|
||||||
//// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
|
//// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
|
||||||
// reimport our macros to this module, so the user does not get confused when importing the macros
|
// reimport our macros to this module, so the user does not get confused when importing the macros
|
||||||
pub use crate::get_stdout_for;
|
pub use crate::divider;
|
||||||
|
pub use crate::println_divider;
|
||||||
|
|
||||||
//// TYPES /////////////////////////////////////////////////////////////////////////////////////////
|
//// TYPES /////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,8 @@
|
||||||
pub mod monitoring;
|
pub mod monitoring;
|
||||||
|
|
||||||
//// CONSTANTS /////////////////////////////////////////////////////////////////////////////////////
|
//// CONSTANTS /////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
/// how long to wait for a remove server to respond in ms
|
||||||
|
pub const REQUEST_TIMEOUT: u64 = 2000;
|
||||||
|
|
||||||
//// STATICS ///////////////////////////////////////////////////////////////////////////////////////
|
//// STATICS ///////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
|
|
@ -47,16 +47,16 @@ pub const DEFAULT_CHECK_URLS: &'static [&'static str] =
|
||||||
/// [`UptimeStatus`] describes the result of an uptime check.
|
/// [`UptimeStatus`] describes the result of an uptime check.
|
||||||
pub struct UptimeStatus {
|
pub struct UptimeStatus {
|
||||||
/// true if the [`UptimeStatus`] is considered successful
|
/// true if the [`UptimeStatus`] is considered successful
|
||||||
success: bool,
|
pub success: bool,
|
||||||
/// the percentage of reachable urls out of the total urls
|
/// the percentage of reachable urls out of the total urls
|
||||||
success_ratio: u8,
|
pub success_ratio: u8,
|
||||||
/// the percentage of reachable urls out of the total urls that need to be reachable in order
|
/// the percentage of reachable urls out of the total urls that need to be reachable in order
|
||||||
/// for this [`UptimeStatus`] to be considered a success.
|
/// for this [`UptimeStatus`] to be considered a success.
|
||||||
success_ratio_target: u8,
|
pub success_ratio_target: u8,
|
||||||
/// the number of reachable [`urls`]
|
/// the number of reachable [`urls`](UptimeStatus::urls)
|
||||||
reachable: usize,
|
pub reachable: usize,
|
||||||
/// which urls to check in [`check()`]
|
/// which urls to check in [`check()`](UptimeStatus::check)
|
||||||
urls: Vec<Url>,
|
pub urls: Vec<Url>,
|
||||||
}
|
}
|
||||||
|
|
||||||
//// IMPLEMENTATION ////////////////////////////////////////////////////////////////////////////////
|
//// IMPLEMENTATION ////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -87,12 +87,15 @@ impl UptimeStatus {
|
||||||
|
|
||||||
/// ## check for success with the given urls
|
/// ## check for success with the given urls
|
||||||
///
|
///
|
||||||
/// Makes the actual https requests and updates the success fields.
|
/// Makes the actual https requests and updates fields accordingly.
|
||||||
|
///
|
||||||
|
/// Note: Blocking execution for all requests, timeout is set to
|
||||||
|
/// [REQUEST_TIMEOUT](crate::networking::REQUEST_TIMEOUT).
|
||||||
pub fn check(&mut self) {
|
pub fn check(&mut self) {
|
||||||
self.reachable = 0;
|
self.reachable = 0;
|
||||||
self.urls.iter().for_each(|url| {
|
self.urls.iter().for_each(|url| {
|
||||||
let client = reqwest::blocking::Client::builder()
|
let client = reqwest::blocking::Client::builder()
|
||||||
.timeout(Duration::from_millis(2000))
|
.timeout(Duration::from_millis(crate::networking::REQUEST_TIMEOUT))
|
||||||
.build()
|
.build()
|
||||||
.expect("could not build a client for https requests");
|
.expect("could not build a client for https requests");
|
||||||
let response = client.get(url.clone()).send();
|
let response = client.get(url.clone()).send();
|
||||||
|
@ -106,7 +109,7 @@ impl UptimeStatus {
|
||||||
/// ## calculate the success based on the `reachable` and `total`
|
/// ## calculate the success based on the `reachable` and `total`
|
||||||
///
|
///
|
||||||
/// Calculates the ratio of [`reachable`](UptimeStatus::reachable) /
|
/// Calculates the ratio of [`reachable`](UptimeStatus::reachable) /
|
||||||
/// [`total`](UptimeStatus::total).
|
/// (length of [urls](UptimeStatus::urls)).
|
||||||
///
|
///
|
||||||
/// Calculates a [`success_ratio`](UptimeStatus::success_ratio) (as [u8]) from that,
|
/// Calculates a [`success_ratio`](UptimeStatus::success_ratio) (as [u8]) from that,
|
||||||
/// by multiplying with 100, then flooring.
|
/// by multiplying with 100, then flooring.
|
||||||
|
@ -118,7 +121,7 @@ impl UptimeStatus {
|
||||||
/// In the special case that no URLs to check for have been provided, the check will be
|
/// In the special case that no URLs to check for have been provided, the check will be
|
||||||
/// considered a success, but the [`success_ratio`](UptimeStatus::success_ratio) will be `0`.
|
/// considered a success, but the [`success_ratio`](UptimeStatus::success_ratio) will be `0`.
|
||||||
///
|
///
|
||||||
/// Note: does not check for networking, use [`check()`] for that.
|
/// Note: does not check for networking, use [`check()`](UptimeStatus::check) for that.
|
||||||
pub fn calc_success(&mut self) {
|
pub fn calc_success(&mut self) {
|
||||||
// if no urls need to be checked, success without checking
|
// if no urls need to be checked, success without checking
|
||||||
if self.urls.len() == 0 {
|
if self.urls.len() == 0 {
|
||||||
|
@ -159,12 +162,12 @@ impl fmt::Display for UptimeStatus {
|
||||||
}
|
}
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"{{
|
"
|
||||||
success: {},
|
success: {},
|
||||||
success_ratio: {}%,
|
success_ratio: {}%,
|
||||||
success_ratio_target: {}%,
|
success_ratio_target: {}%,
|
||||||
reachable: {},
|
reachable: {},
|
||||||
urls: {:?}\n}}",
|
urls: {:?}\n",
|
||||||
self.success, self.success_ratio, self.success_ratio_target, self.reachable, url_strs
|
self.success, self.success_ratio, self.success_ratio_target, self.reachable, url_strs
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -173,7 +176,11 @@ impl fmt::Display for UptimeStatus {
|
||||||
//// PUBLIC FUNCTIONS //////////////////////////////////////////////////////////////////////////////
|
//// PUBLIC FUNCTIONS //////////////////////////////////////////////////////////////////////////////
|
||||||
/// ## Uptime monitor
|
/// ## Uptime monitor
|
||||||
///
|
///
|
||||||
/// This function continuously monitors the uptime of your host/network
|
/// This function continuously monitors the uptime of your host/network.
|
||||||
|
///
|
||||||
|
/// On change of status, an update will be logged at [INFO Level](log::Level::Info), containing
|
||||||
|
/// information on your current status, including timestamps of the last up/down time and durations
|
||||||
|
/// since.
|
||||||
pub fn continuous_uptime_monitor(success_ratio_target: u8, urls: Vec<String>, interval: u64) {
|
pub fn continuous_uptime_monitor(success_ratio_target: u8, urls: Vec<String>, interval: u64) {
|
||||||
if urls.len() == 0 {
|
if urls.len() == 0 {
|
||||||
error!("No URLs provided. There is nothing to monitor.");
|
error!("No URLs provided. There is nothing to monitor.");
|
||||||
|
@ -218,6 +225,7 @@ pub fn continuous_uptime_monitor(success_ratio_target: u8, urls: Vec<String>, in
|
||||||
}
|
}
|
||||||
|
|
||||||
//// PRIVATE FUNCTIONS /////////////////////////////////////////////////////////////////////////////
|
//// PRIVATE FUNCTIONS /////////////////////////////////////////////////////////////////////////////
|
||||||
|
/// Displays the current status for the [continuous uptime monitor](continuous_uptime_monitor)
|
||||||
fn display_uptime_status(
|
fn display_uptime_status(
|
||||||
msg: &str,
|
msg: &str,
|
||||||
last_uptime: Option<SystemTime>,
|
last_uptime: Option<SystemTime>,
|
||||||
|
@ -227,14 +235,8 @@ fn display_uptime_status(
|
||||||
// I know it's weird that this has two spaces too much, but somehow just the tabs is missing
|
// I know it's weird that this has two spaces too much, but somehow just the tabs is missing
|
||||||
// two spaces.
|
// two spaces.
|
||||||
info!("uptime check: {}", msg);
|
info!("uptime check: {}", msg);
|
||||||
info!(
|
info!("last uptime: {}", match_format_time(last_uptime));
|
||||||
"last uptime: {}",
|
info!("last downtime: {}", match_format_time(last_downtime));
|
||||||
match_format_time(last_uptime)
|
|
||||||
);
|
|
||||||
info!(
|
|
||||||
"last downtime: {}",
|
|
||||||
match_format_time(last_downtime)
|
|
||||||
);
|
|
||||||
info!(
|
info!(
|
||||||
"since downtime: {}",
|
"since downtime: {}",
|
||||||
match_format_duration_since(last_downtime)
|
match_format_duration_since(last_downtime)
|
||||||
|
@ -243,7 +245,7 @@ fn display_uptime_status(
|
||||||
"since uptime: {}",
|
"since uptime: {}",
|
||||||
match_format_duration_since(last_uptime)
|
match_format_duration_since(last_uptime)
|
||||||
);
|
);
|
||||||
debug!("\n{}", status);
|
debug!("{}", status);
|
||||||
info!("{}", divider!());
|
info!("{}", divider!());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in New Issue