use crate::message::{Level, Message}; use crate::monitor::*; use serde::Deserialize; #[derive(Debug)] pub struct Ping { config: PingConfig, } #[derive(Debug, Deserialize)] struct PingConfig { targets: Vec, #[serde(default)] period: Option, } #[derive(Debug, Deserialize)] struct PingConfigTarget { host: String, #[serde(default)] ping_args: Vec, #[serde(default)] allowed_fails: Option, #[serde(default)] allowed_loss: Option, } impl Monitor for Ping { fn new(config: serde_yaml::Value) -> Result> { let config = serde_yaml::from_value(config).expect("Invalid config for ping"); Ok(Ping { config: config }) } fn run(&mut self, sender: &mpsc::Sender) { for target in &self.config.targets { use std::process::{Command, Stdio}; let res = Command::new("ping") .args(&target.ping_args) .arg(&target.host) .stdout(Stdio::null()) .status() .expect("failed to execute process"); if !res.success() { sender .send(Message { emitter: "ping".to_owned(), msg_type: "ping.failed".to_owned(), level: Level::Issue, text: format!("Cannot ping {}", &target.host), }) .unwrap(); } } std::thread::sleep(std::time::Duration::from_millis(2000)) } }