update dependencies, replace oping with tokio_ping
parent
b60f2f8290
commit
c6d5505f2a
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
|||||||
listener = "[::]:9898"
|
listener = "[::]:9898"
|
||||||
|
|
||||||
[hosts]
|
[hosts]
|
||||||
"1.1.1.1" = 5
|
"1.1.1.1" = 500
|
||||||
"1.0.0.1" = 5
|
"1.0.0.1" = 500
|
||||||
|
|
||||||
|
@ -1,27 +1,25 @@
|
|||||||
use log::{error, warn, info, debug, trace};
|
use log::error;
|
||||||
use futures::future::lazy;
|
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
mod metrics;
|
mod metrics;
|
||||||
mod ping;
|
mod ping;
|
||||||
use crate::config::{Config, read_config, setup_clap, setup_fern};
|
use crate::config::{read_config, setup_clap, setup_fern};
|
||||||
use crate::metrics::start_serving_metrics;
|
use crate::metrics::start_serving_metrics;
|
||||||
use crate::ping::start_pinging_hosts;
|
use crate::ping::start_pinging_hosts;
|
||||||
|
|
||||||
fn main() {
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), ()> {
|
||||||
let clap = setup_clap();
|
let clap = setup_clap();
|
||||||
setup_fern(clap.occurrences_of("v"));
|
setup_fern(clap.occurrences_of("v"));
|
||||||
let config = match read_config(clap.value_of("config").unwrap()) {
|
let config = match read_config(clap.value_of("config").unwrap()) {
|
||||||
Ok(config) => config,
|
Ok(config) => config,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
error!("Couldn't read config file!");
|
error!("Couldn't read config file!");
|
||||||
return;
|
return Err(());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
tokio::run(lazy(move || {
|
tokio::spawn(start_pinging_hosts(config.clone()));
|
||||||
start_serving_metrics(&config);
|
start_serving_metrics(config.clone()).await;
|
||||||
start_pinging_hosts(&config);
|
Ok(())
|
||||||
Ok(())
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
@ -1,55 +1,47 @@
|
|||||||
use crate::config::{Config, Error};
|
use crate::config::Config;
|
||||||
use std::time::{Duration, Instant};
|
use futures_util::stream::StreamExt;
|
||||||
use tokio::timer::{Interval};
|
|
||||||
use futures::{future::{lazy, Future}, stream::Stream};
|
|
||||||
use oping::{Ping};
|
|
||||||
use log::{trace, debug, info, warn, error};
|
|
||||||
use prometheus::*;
|
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
use log::{error, info};
|
||||||
|
use prometheus::*;
|
||||||
|
use std::time::Duration;
|
||||||
|
use tokio::time::delay_for;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref PING_HISTOGRAM : HistogramVec = register_histogram_vec!(
|
static ref PING_HISTOGRAM: HistogramVec = register_histogram_vec!(
|
||||||
"ping_rtt_milliseconds",
|
"ping_rtt_milliseconds",
|
||||||
"The ping round trip time in milliseconds",
|
"The ping round trip time in milliseconds",
|
||||||
&["target"],
|
&["target"],
|
||||||
vec![0.5, 1.0, 5.0, 10.0, 15.0, 20.0, 25.0, 50.0, 75.0, 100.0, 150.0, 200.0, 250.0,
|
vec![
|
||||||
300.0, 350.0, 400.0, 450.0, 500.0, 550.0, 600.0, 650.0, 700.0, 750.0, 800.0, 900.0,
|
0.5, 1.0, 5.0, 10.0, 15.0, 20.0, 25.0, 50.0, 75.0, 100.0, 150.0, 200.0, 250.0, 300.0,
|
||||||
1000.0, 1250.0, 1500.0, 1750.0, 2000.0]
|
350.0, 400.0, 450.0, 500.0, 550.0, 600.0, 650.0, 700.0, 750.0, 800.0, 900.0, 1000.0,
|
||||||
).unwrap();
|
1250.0, 1500.0, 1750.0, 2000.0
|
||||||
|
]
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn start_pinging_hosts(config: &Config) {
|
pub(crate) async fn start_pinging_hosts(
|
||||||
|
config: Config,
|
||||||
|
) -> std::result::Result<(), tokio_ping::Error> {
|
||||||
|
let pinger = tokio_ping::Pinger::new().await?;
|
||||||
for (host, interval) in config.hosts.clone() {
|
for (host, interval) in config.hosts.clone() {
|
||||||
info!("Spawn ping task for {}", host);
|
info!("Spawn ping task for {}", host);
|
||||||
tokio::spawn(
|
let pingchain = pinger.chain(host).timeout(Duration::from_secs(3));
|
||||||
Interval::new(Instant::now(), Duration::from_millis(interval))
|
let host = host.to_string();
|
||||||
.for_each(move |_| {
|
tokio::spawn(pingchain.stream().for_each(move |ping_result| {
|
||||||
let mut ping = Ping::new();
|
match ping_result {
|
||||||
ping.set_timeout(2.5);
|
Ok(time) => match time {
|
||||||
ping.add_host(&host);
|
Some(time) => {
|
||||||
for response in match ping.send() {
|
PING_HISTOGRAM
|
||||||
Ok(iterator) => iterator,
|
.with_label_values(&[&host])
|
||||||
Err(e) => {
|
.observe(time.as_millis() as f64);
|
||||||
error!("Something went wrong sending the ping: {:?}", e);
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
}{
|
|
||||||
if response.dropped > 0 {
|
|
||||||
debug!("No response from host: {}", response.hostname);
|
|
||||||
PING_HISTOGRAM
|
|
||||||
.with_label_values(&[&host])
|
|
||||||
.observe(2500.0)
|
|
||||||
} else {
|
|
||||||
debug!("Response from host {} (address {}): latency {} ms",
|
|
||||||
response.hostname, response.address, response.latency_ms);
|
|
||||||
trace!(" all details: {:?}", response);
|
|
||||||
PING_HISTOGRAM
|
|
||||||
.with_label_values(&[&host])
|
|
||||||
.observe(response.latency_ms);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
None => PING_HISTOGRAM.with_label_values(&[&host]).observe(3000.0),
|
||||||
}).map_err(|_| ())
|
},
|
||||||
);
|
Err(error) => error!("Couldn't ping {}: {}", &host, error),
|
||||||
|
}
|
||||||
|
delay_for(Duration::from_millis(interval))
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
}
|
Ok(())
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue