rexaping/src/ping.rs

121 lines
5.0 KiB
Rust
Raw Normal View History

2020-04-06 13:28:02 +02:00
/********************************************************************************
* Prometheus exporter for monitoring network connectivity using icmp pings *
* *
* Copyright (C) 2019-2020 Jan Christian Grünhage *
* Copyright (C) 2020 Famedly GmbH *
2022-01-03 14:36:38 +00:00
* Copyright (C) 2021-2022 Faelix Limited *
2020-04-06 13:28:02 +02:00
* *
* 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;
2019-02-06 19:29:27 +01:00
use lazy_static::lazy_static;
2022-01-03 14:36:38 +00:00
use log::{trace};
use prometheus::*;
2020-04-06 20:29:46 +02:00
use std::net::IpAddr;
use std::time::Duration;
2021-04-27 20:55:09 +02:00
use tokio_icmp_echo::{PingFuture, Pinger};
2022-01-03 14:36:38 +00:00
use std::collections::HashMap;
2019-02-06 19:29:27 +01:00
lazy_static! {
static ref PING_HISTOGRAM: HistogramVec = register_histogram_vec!(
2019-02-06 19:29:27 +01:00
"ping_rtt_milliseconds",
"The ping round trip time in milliseconds",
2022-01-03 14:36:38 +00:00
&["target", "device", "interface", "expected", "team", "priority"],
vec![
2022-01-03 14:36:38 +00:00
0.125, 0.25, 0.5, 1.0, 1.5, 2.0, 2.5, 5.0, 7.5, 10.0, 15.0, 20.0, 25.0, 30.0,
35.0, 40.0, 45.0, 50.0, 55.0, 60.0, 65.0, 70.0, 75.0, 80.0, 90.0, 100.0,
125.0, 150.0, 175.0, 200.0, 250.0, 300.0, 400.0, 500.0, 750.0, 1000.0, 2000.0, 4000.0
]
)
.unwrap();
2019-02-06 19:29:27 +01:00
}
2022-01-03 14:36:38 +00:00
lazy_static! {
static ref PING_COUNTER: IntCounterVec =
register_int_counter_vec!("ping_replies", "Number of ICMP ping responses received", &["target"]).unwrap();
}
2021-04-23 14:32:26 +02:00
pub(crate) async fn start_pinging_hosts(config: &Config) -> Result<()> {
let pinger = Pinger::new().await.context("Couldn't create pinger")?;
let mut handles = vec![];
2022-01-03 14:36:38 +00:00
let mut interval;
for (target, hostdata) in config.hosts.clone() {
let hd_interval = hostdata.get("interval");
match hd_interval {
Some(ival) => {
interval = ival.parse()?;
},
_ => interval = 1000,
}
handles.push(tokio::spawn(ping_host(pinger.clone(), target, interval, hostdata)));
2019-02-06 19:29:27 +01:00
}
let (result, _, _) = futures::future::select_all(handles).await;
2021-04-23 14:32:26 +02:00
result??;
Ok(())
}
2020-04-06 20:29:46 +02:00
2022-01-03 14:36:38 +00:00
async fn ping_host(pinger: Pinger, host: IpAddr, interval: u64, hostdata: HashMap<String, String>) -> Result<()> {
let mut pingchain = pinger.chain(host).timeout(Duration::from_secs(3));
2020-04-06 20:29:46 +02:00
let mut interval = tokio::time::interval(Duration::from_millis(interval));
let host_string = host.to_string();
2020-04-06 20:29:46 +02:00
loop {
interval.tick().await;
tokio::spawn(catch(handle_ping_result(
pingchain.send(),
host_string.clone(),
2022-01-03 14:36:38 +00:00
hostdata.clone(),
)));
2020-04-06 20:29:46 +02:00
}
}
2022-01-03 14:36:38 +00:00
async fn handle_ping_result(result: PingFuture, host: String, hostdata: HashMap<String, String>) -> Result<()> {
let pong = result.await.context(format!("Couldn't ping {}", &host))?;
2022-01-03 14:36:38 +00:00
let empty = "".to_string();
let device = hostdata.get("device").unwrap_or(&empty);
let interface = hostdata.get("interface").unwrap_or(&empty);
let team = hostdata.get("team").unwrap_or(&empty);
let up = "up".to_string();
let expected = hostdata.get("expected").unwrap_or(&up);
let notice = "notice".to_string();
let severity = hostdata.get("severity").unwrap_or(&notice);
2020-04-06 20:29:46 +02:00
match pong {
Some(time) => {
let ms = time.as_millis();
trace!("Received pong from {} after {} ms", &host, &ms);
PING_HISTOGRAM
2022-01-03 14:36:38 +00:00
.with_label_values(&[&host, device, interface, expected, team, severity])
2020-04-06 20:29:46 +02:00
.observe(ms as f64);
2022-01-03 14:36:38 +00:00
//PING_COUNTER
// .with_label_values(&[&host])
// .collect();
2020-04-06 20:29:46 +02:00
}
None => {
trace!("Received no response from {} within timeout", &host);
2022-01-03 14:36:38 +00:00
PING_HISTOGRAM
.with_label_values(&[&host, device, interface, expected, team, severity])
.observe(4000.0);
2020-04-06 20:29:46 +02:00
}
};
Ok(())
2020-04-06 20:29:46 +02:00
}