Compare commits

...

5 commits

Author SHA1 Message Date
Jan Christian Grünhage 576f5728a4 chore: bump version to 0.4.1 and update changelog 2021-04-27 21:42:47 +02:00
Jan Christian Grünhage d16e06b253 chore: extend Cargo.toml 2021-04-27 21:42:25 +02:00
Jan Christian Grünhage 58ff42a3c2 chore: update dependencies 2021-04-27 21:33:06 +02:00
Jan Christian Grünhage c2988995e1 chore: switch tokio-ping over to fork 2021-04-27 21:26:35 +02:00
Jan Christian Grünhage a17ee534b2 chore: code cleanup 2021-04-27 21:26:35 +02:00
7 changed files with 73 additions and 58 deletions

View file

@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [v0.4.1] - 2021-04-27
### Changed
- code cleanup
- switch over to tokio-ping fork, tokio-icmp-echo
## [v0.4.0] - 2021-04-23
### Changed
- update dependencies, including stable tokio this time.

48
Cargo.lock generated
View file

@ -332,9 +332,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.93"
version = "0.2.94"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9385f66bf6105b241aa65a61cb923ef20efc665cb9f9bb50ac2f0c4b7f378d41"
checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e"
[[package]]
name = "lock_api"
@ -447,7 +447,7 @@ dependencies = [
[[package]]
name = "peshming"
version = "0.4.0"
version = "0.4.1"
dependencies = [
"anyhow",
"async-anyhow-logger",
@ -462,7 +462,7 @@ dependencies = [
"prometheus",
"serde",
"tokio",
"tokio-ping",
"tokio-icmp-echo",
"toml",
]
@ -542,9 +542,9 @@ dependencies = [
[[package]]
name = "protobuf"
version = "2.22.1"
version = "2.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b7f4a129bb3754c25a4e04032a90173c68f85168f77118ac4cb4936e7f06f92"
checksum = "45604fc7a88158e7d514d8e22e14ac746081e7a70d7690074dd0029ee37458d6"
[[package]]
name = "quote"
@ -700,12 +700,11 @@ dependencies = [
[[package]]
name = "time"
version = "0.1.44"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
dependencies = [
"libc",
"wasi",
"winapi",
]
@ -726,20 +725,10 @@ dependencies = [
]
[[package]]
name = "tokio-macros"
version = "1.1.0"
name = "tokio-icmp-echo"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "caf7b11a536f46a809a8a9f0bb4237020f70ecbf115b842360afb127ea2fda57"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tokio-ping"
version = "0.3.0"
source = "git+https://github.com/jcgruenhage/tokio-ping?branch=main#cf9b2de4ceb838b6ac82a28a3c87eaf4a32b8a5a"
checksum = "6a22ee530dd5521192842786230ab5fdd7a330d7d865c81087a06fef2ac6ab22"
dependencies = [
"futures",
"libc",
@ -751,6 +740,17 @@ dependencies = [
"tokio",
]
[[package]]
name = "tokio-macros"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "caf7b11a536f46a809a8a9f0bb4237020f70ecbf115b842360afb127ea2fda57"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tokio-util"
version = "0.6.6"
@ -836,9 +836,9 @@ dependencies = [
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "winapi"

View file

@ -1,23 +1,26 @@
[package]
name = "peshming"
version = "0.4.0"
version = "0.4.1"
license = "AGPL-3.0-only"
authors = ["Jan Christian Grünhage <jan.christian@gruenhage.xyz>"]
repository = "https://git.jcg.re/jcgruenhage/peshming"
keywords = ["ping", "icmp", "prometheus"]
edition = "2018"
description = "Pings configured hosts in a configurable intervals and exposes metrics for prometheus."
[dependencies]
prometheus = "0.12.0"
toml = "0.5.8"
hyper = { version = "0.14.7", features = ["full"] }
lazy_static = "1.4.0"
futures = "0.3.14"
tokio = { version = "1.5.0", features = ["rt-multi-thread", "macros", "time"] }
clap = "2.33.3"
fern = "0.6.0"
log = "0.4.14"
chrono = "0.4.19"
serde = { version = "1.0.125", features = ["derive"] }
tokio-ping = { git = "https://github.com/jcgruenhage/tokio-ping", branch = "main" }
futures-util = "0.3.14"
anyhow = "1.0.40"
async-anyhow-logger = "0.1.0"
prometheus = "0.12"
toml = "0.5"
hyper = { version = "0.14", features = ["full"] }
lazy_static = "1"
futures = "0.3"
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] }
clap = "2"
fern = "0.6"
log = "0.4"
chrono = "0.4"
serde = { version = "1", features = ["derive"] }
tokio-icmp-echo = "0.4"
futures-util = "0.3"
anyhow = "1"
async-anyhow-logger = "0.1"

View file

@ -29,7 +29,7 @@ pub(crate) struct Config {
pub(crate) hosts: HashMap<std::net::IpAddr, u64>,
}
pub(crate) fn setup_clap() -> clap::ArgMatches<'static> {
fn setup_clap() -> clap::ArgMatches<'static> {
clap_app!(myapp =>
(name: crate_name!())
(version: crate_version!())
@ -42,7 +42,7 @@ pub(crate) fn setup_clap() -> clap::ArgMatches<'static> {
.get_matches()
}
pub(crate) fn setup_fern(level: u64) {
fn setup_fern(level: u64) {
let level = match level {
0 => log::LevelFilter::Error,
1 => log::LevelFilter::Warn,
@ -70,7 +70,16 @@ pub(crate) fn setup_fern(level: u64) {
}
}
pub(crate) fn read_config(path: &str) -> Result<Config> {
fn read_config(path: &str) -> Result<Config> {
let config_file_content = std::fs::read_to_string(path).context("Couldn't read config file")?;
Ok(toml::from_str(&config_file_content).context("Couldn't parse config file")?)
}
pub(crate) fn setup_app() -> Result<Config> {
let clap = setup_clap();
setup_fern(clap.occurrences_of("v"));
let config_path = clap
.value_of("config")
.context("Got no config file. clap should've catched this")?;
Ok(read_config(config_path).context("Couldn't read config file!")?)
}

View file

@ -17,25 +17,23 @@
* 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 anyhow::{Context, Result};
use anyhow::Result;
use async_anyhow_logger::catch;
mod config;
mod metrics;
mod ping;
use crate::config::{read_config, setup_clap, setup_fern};
use crate::metrics::start_serving_metrics;
use crate::ping::start_pinging_hosts;
use config::setup_app;
use metrics::start_serving_metrics;
use ping::start_pinging_hosts;
#[tokio::main]
async fn main() -> Result<()> {
let clap = setup_clap();
setup_fern(clap.occurrences_of("v"));
let config =
read_config(clap.value_of("config").unwrap()).context("Couldn't read config file!")?;
let config = setup_app()?;
let ping_fut = start_pinging_hosts(config.clone());
let serve_fut = start_serving_metrics(config.clone());
let ping_fut = catch(start_pinging_hosts(&config));
let serve_fut = catch(start_serving_metrics(&config));
futures::pin_mut!(ping_fut);
futures::pin_mut!(serve_fut);

View file

@ -95,7 +95,7 @@ async fn serve_not_found() -> Result<Response<Body>> {
.context("Couldn't build not found response")?)
}
pub(crate) async fn start_serving_metrics(config: Config) -> Result<()> {
pub(crate) async fn start_serving_metrics(config: &Config) -> Result<()> {
let serve_future = Server::bind(&config.listener).serve(make_service_fn(|_| async {
Ok::<_, hyper::Error>(service_fn(serve_req))
}));

View file

@ -25,7 +25,7 @@ use log::{info, trace};
use prometheus::*;
use std::net::IpAddr;
use std::time::Duration;
use tokio_ping::{PingFuture, Pinger};
use tokio_icmp_echo::{PingFuture, Pinger};
lazy_static! {
static ref PING_HISTOGRAM: HistogramVec = register_histogram_vec!(
@ -41,15 +41,15 @@ lazy_static! {
.unwrap();
}
pub(crate) async fn start_pinging_hosts(config: Config) -> Result<()> {
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.hosts.clone() {
info!("Spawn ping task for {}", host);
handles.push(tokio::spawn(catch(ping_host(pinger.clone(), host, interval))));
handles.push(tokio::spawn(ping_host(pinger.clone(), host, interval)));
}
let (result, _, _) = futures::future::select_all(handles).await;
result?;
result??;
Ok(())
}