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"
|
||||
|
||||
[hosts]
|
||||
"1.1.1.1" = 5
|
||||
"1.0.0.1" = 5
|
||||
"1.1.1.1" = 500
|
||||
"1.0.0.1" = 500
|
||||
|
||||
|
@ -1,27 +1,25 @@
|
||||
use log::{error, warn, info, debug, trace};
|
||||
use futures::future::lazy;
|
||||
use log::error;
|
||||
|
||||
mod config;
|
||||
mod metrics;
|
||||
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::ping::start_pinging_hosts;
|
||||
|
||||
fn main() {
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), ()> {
|
||||
let clap = setup_clap();
|
||||
setup_fern(clap.occurrences_of("v"));
|
||||
let config = match read_config(clap.value_of("config").unwrap()) {
|
||||
Ok(config) => config,
|
||||
Err(_) => {
|
||||
error!("Couldn't read config file!");
|
||||
return;
|
||||
return Err(());
|
||||
}
|
||||
};
|
||||
|
||||
tokio::run(lazy(move || {
|
||||
start_serving_metrics(&config);
|
||||
start_pinging_hosts(&config);
|
||||
tokio::spawn(start_pinging_hosts(config.clone()));
|
||||
start_serving_metrics(config.clone()).await;
|
||||
Ok(())
|
||||
}));
|
||||
}
|
||||
|
@ -1,55 +1,47 @@
|
||||
use crate::config::{Config, Error};
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::timer::{Interval};
|
||||
use futures::{future::{lazy, Future}, stream::Stream};
|
||||
use oping::{Ping};
|
||||
use log::{trace, debug, info, warn, error};
|
||||
use prometheus::*;
|
||||
use crate::config::Config;
|
||||
use futures_util::stream::StreamExt;
|
||||
use lazy_static::lazy_static;
|
||||
use log::{error, info};
|
||||
use prometheus::*;
|
||||
use std::time::Duration;
|
||||
use tokio::time::delay_for;
|
||||
|
||||
lazy_static! {
|
||||
static ref PING_HISTOGRAM: HistogramVec = register_histogram_vec!(
|
||||
"ping_rtt_milliseconds",
|
||||
"The ping round trip time in milliseconds",
|
||||
&["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,
|
||||
300.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, 1250.0, 1500.0, 1750.0, 2000.0]
|
||||
).unwrap();
|
||||
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, 300.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,
|
||||
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() {
|
||||
info!("Spawn ping task for {}", host);
|
||||
tokio::spawn(
|
||||
Interval::new(Instant::now(), Duration::from_millis(interval))
|
||||
.for_each(move |_| {
|
||||
let mut ping = Ping::new();
|
||||
ping.set_timeout(2.5);
|
||||
ping.add_host(&host);
|
||||
for response in match ping.send() {
|
||||
Ok(iterator) => iterator,
|
||||
Err(e) => {
|
||||
error!("Something went wrong sending the ping: {:?}", e);
|
||||
return Ok(());
|
||||
}
|
||||
}{
|
||||
if response.dropped > 0 {
|
||||
debug!("No response from host: {}", response.hostname);
|
||||
let pingchain = pinger.chain(host).timeout(Duration::from_secs(3));
|
||||
let host = host.to_string();
|
||||
tokio::spawn(pingchain.stream().for_each(move |ping_result| {
|
||||
match ping_result {
|
||||
Ok(time) => match time {
|
||||
Some(time) => {
|
||||
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);
|
||||
.observe(time.as_millis() as f64);
|
||||
}
|
||||
None => PING_HISTOGRAM.with_label_values(&[&host]).observe(3000.0),
|
||||
},
|
||||
Err(error) => error!("Couldn't ping {}: {}", &host, error),
|
||||
}
|
||||
Ok(())
|
||||
}).map_err(|_| ())
|
||||
);
|
||||
delay_for(Duration::from_millis(interval))
|
||||
}));
|
||||
}
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in New Issue