19
src/ping.rs
19
src/ping.rs
@ -20,7 +20,7 @@
|
||||
use crate::config::Config;
|
||||
use futures_util::stream::StreamExt;
|
||||
use lazy_static::lazy_static;
|
||||
use log::{error, info};
|
||||
use log::{error, info, trace};
|
||||
use prometheus::*;
|
||||
use std::time::Duration;
|
||||
use tokio::time::delay_for;
|
||||
@ -42,7 +42,13 @@ lazy_static! {
|
||||
pub(crate) async fn start_pinging_hosts(
|
||||
config: Config,
|
||||
) -> std::result::Result<(), tokio_ping::Error> {
|
||||
let pinger = tokio_ping::Pinger::new().await?;
|
||||
let pinger = match tokio_ping::Pinger::new().await {
|
||||
Ok(pinger) => pinger,
|
||||
Err(error) => {
|
||||
error!("Couldn't create pinger: {}", error);
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
for (host, interval) in config.hosts.clone() {
|
||||
info!("Spawn ping task for {}", host);
|
||||
let pingchain = pinger.chain(host).timeout(Duration::from_secs(3));
|
||||
@ -51,11 +57,16 @@ pub(crate) async fn start_pinging_hosts(
|
||||
match ping_result {
|
||||
Ok(time) => match time {
|
||||
Some(time) => {
|
||||
let ms = time.as_millis();
|
||||
trace!("Received pong from {} after {} ms", &host, &ms);
|
||||
PING_HISTOGRAM
|
||||
.with_label_values(&[&host])
|
||||
.observe(time.as_millis() as f64);
|
||||
.observe(ms as f64);
|
||||
}
|
||||
None => {
|
||||
trace!("Received no response from {} within timeout", &host);
|
||||
PING_HISTOGRAM.with_label_values(&[&host]).observe(3000.0);
|
||||
}
|
||||
None => PING_HISTOGRAM.with_label_values(&[&host]).observe(3000.0),
|
||||
},
|
||||
Err(error) => error!("Couldn't ping {}: {}", &host, error),
|
||||
}
|
||||
|
Reference in New Issue
Block a user