76 lines
3.4 KiB
Rust
76 lines
3.4 KiB
Rust
/********************************************************************************
|
|
* Prometheus exporter for monitoring network connectivity using icmp pings *
|
|
* *
|
|
* Copyright (C) 2019-2022 Jan Christian Grünhage *
|
|
* Copyright (C) 2020-2021 Famedly GmbH *
|
|
* *
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
* it under the terms of the GNU Affero General Public License as *
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
* License, or (at your option) any later version. *
|
|
* *
|
|
* This program is distributed in the hope that it will be useful, *
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
* GNU Affero General Public License for more details. *
|
|
* *
|
|
* You should have received a copy of the GNU Affero General Public License *
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
********************************************************************************/
|
|
use crate::config::Config;
|
|
use anyhow::{Context, Result};
|
|
use async_anyhow_logger::catch;
|
|
use log::{info, trace};
|
|
use metrics::histogram;
|
|
use std::net::IpAddr;
|
|
use std::time::Duration;
|
|
use tokio_icmp_echo::{PingFuture, Pinger};
|
|
|
|
pub(crate) async fn start_pinging_hosts(config: &Config) -> Result<()> {
|
|
let pinger = Pinger::new().await.context("Couldn't create pinger")?;
|
|
let mut handles = vec![];
|
|
for (host, interval) in config.ping.hosts.clone() {
|
|
info!("Spawn ping task for {}", host);
|
|
handles.push(tokio::spawn(ping_host(
|
|
pinger.clone(),
|
|
host,
|
|
interval,
|
|
config.ping.timeout,
|
|
)));
|
|
}
|
|
let (result, _, _) = futures::future::select_all(handles).await;
|
|
result??;
|
|
Ok(())
|
|
}
|
|
|
|
async fn ping_host(pinger: Pinger, host: IpAddr, interval: u64, timeout: Duration) -> Result<()> {
|
|
let mut pingchain = pinger.chain(host).timeout(timeout);
|
|
let mut interval = tokio::time::interval(Duration::from_millis(interval));
|
|
let host_string = host.to_string();
|
|
loop {
|
|
interval.tick().await;
|
|
tokio::spawn(catch(handle_ping_result(
|
|
pingchain.send(),
|
|
host_string.clone(),
|
|
timeout,
|
|
)));
|
|
}
|
|
}
|
|
|
|
async fn handle_ping_result(result: PingFuture, host: String, timeout: Duration) -> Result<()> {
|
|
let pong = result.await.context(format!("Couldn't ping {}", &host))?;
|
|
match pong {
|
|
Some(time) => {
|
|
let ms = time.as_millis();
|
|
trace!("Received pong from {} after {} ms", &host, &ms);
|
|
histogram!("ping_rtt_milliseconds", ms as f64, "target" => host);
|
|
}
|
|
None => {
|
|
trace!("Received no response from {} within timeout", &host);
|
|
histogram!("ping_rtt_milliseconds", timeout.as_millis() as f64, "target" => host);
|
|
}
|
|
};
|
|
|
|
Ok(())
|
|
}
|