rnetmon/src/monitors/ping.rs

58 lines
1.6 KiB
Rust

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<PingConfigTarget>,
#[serde(default)]
period: Option<u64>,
}
#[derive(Debug, Deserialize)]
struct PingConfigTarget {
host: String,
#[serde(default)]
ping_args: Vec<String>,
#[serde(default)]
allowed_fails: Option<u64>,
#[serde(default)]
allowed_loss: Option<u64>,
}
impl Monitor for Ping {
fn new(config: serde_yaml::Value) -> Result<Self, Box<dyn std::error::Error>> {
let config = serde_yaml::from_value(config).expect("Invalid config for ping");
Ok(Ping { config: config })
}
fn run(&mut self, sender: &mpsc::Sender<Message>) {
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))
}
}