uptime monitor shows first status if its a failure

This commit is contained in:
Christoph J. Scherr 2023-08-08 14:12:02 +02:00
parent 223a5ad508
commit f4c02b3042
1 changed files with 16 additions and 14 deletions

View File

@ -19,9 +19,7 @@
use std::{fmt, time::Duration}; use std::{fmt, time::Duration};
//// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// //// IMPORTS ///////////////////////////////////////////////////////////////////////////////////////
// we want the log macros in any case use crate::logger::*;
#[allow(unused_imports)]
use tracing;
use reqwest; use reqwest;
@ -142,10 +140,10 @@ impl UptimeStatus {
return; return;
} }
let ratio: f32 = (self.reachable as f32) / (self.urls.len() as f32) * 100f32; let ratio: f32 = (self.reachable as f32) / (self.urls.len() as f32) * 100f32;
tracing::trace!("calculated success_ratio: {}", ratio); trace!("calculated success_ratio: {}", ratio);
self.success_ratio = ratio.floor() as u8; self.success_ratio = ratio.floor() as u8;
self.success = self.success_ratio >= self.success_ratio_target; self.success = self.success_ratio >= self.success_ratio_target;
tracing::trace!("calculated success as: {}", self.success) trace!("calculated success as: {}", self.success)
} }
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
@ -218,7 +216,7 @@ pub fn continuous_uptime_monitor(
timeout: u64, timeout: u64,
) { ) {
if urls.len() == 0 { if urls.len() == 0 {
tracing::error!("No URLs provided. There is nothing to monitor."); error!("No URLs provided. There is nothing to monitor.");
return; return;
} }
@ -226,11 +224,15 @@ pub fn continuous_uptime_monitor(
let mut last_downtime: Option<SystemTime> = None; let mut last_downtime: Option<SystemTime> = None;
let mut last_uptime: Option<SystemTime> = None; let mut last_uptime: Option<SystemTime> = None;
let mut status = UptimeStatus::new(success_ratio_target, urls, timeout); let mut status = UptimeStatus::new(success_ratio_target, urls, timeout);
let mut last_was_up: bool = false; // we assume that the last status was up, so the binary shows the first status if its a
// failure.
let mut last_was_up: bool = true;
let mut last_ratio: u8 = status.success_ratio; let mut last_ratio: u8 = status.success_ratio;
loop { loop {
trace!(?status, ?last_was_up, "loop iteration for continuous uptime monitor");
if !status.success { if !status.success {
if last_was_up { if last_was_up {
trace!("displaying status");
display_uptime_status("fail", last_uptime, last_downtime, &status) display_uptime_status("fail", last_uptime, last_downtime, &status)
} }
last_downtime = Some(SystemTime::now()); last_downtime = Some(SystemTime::now());
@ -293,19 +295,19 @@ 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.
tracing::info!("uptime check: {}", msg); info!("uptime check: {}", msg);
tracing::info!("last uptime: {}", match_format_time(last_uptime)); info!("last uptime: {}", match_format_time(last_uptime));
tracing::info!("last downtime: {}", match_format_time(last_downtime)); info!("last downtime: {}", match_format_time(last_downtime));
tracing::info!( info!(
"since downtime: {}", "since downtime: {}",
match_format_duration_since(last_downtime) match_format_duration_since(last_downtime)
); );
tracing::info!( info!(
"since uptime: {}", "since uptime: {}",
match_format_duration_since(last_uptime) match_format_duration_since(last_uptime)
); );
tracing::debug!("\n{}", status); debug!("\n{}", status);
tracing::info!("{}", divider!()); info!("{}", divider!());
} }
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////