Use -c for config. Fix #3.

This commit is contained in:
2018-10-03 08:06:50 +02:00
parent 51e5ef2815
commit e5b0caad36
3 changed files with 17 additions and 18 deletions

View File

@@ -5,27 +5,27 @@ command = "ping -c 1 -t 3 {{ host }}" # -t should be -w on linux environments.
command = "curl --head --connect-timeout {{ host }}" command = "curl --head --connect-timeout {{ host }}"
[[hosts]] [[checks]]
name = "Cloudflare/APNIC DNS (primary)" name = "Cloudflare/APNIC DNS (primary)"
command = "ping" command = "ping"
host = "1.1.1.1" host = "1.1.1.1"
[[hosts]] [[checks]]
name = "Cloudflare/APNIC DNS (secondary)" name = "Cloudflare/APNIC DNS (secondary)"
command = "ping" command = "ping"
host = "1.0.0.1" host = "1.0.0.1"
[[hosts]] [[checks]]
name = "Google DNS (primary)" name = "Google DNS (primary)"
command = "ping" command = "ping"
host = "8.8.8.8" host = "8.8.8.8"
[[hosts]] [[checks]]
name = "Google DNS (secondary)" name = "Google DNS (secondary)"
command = "ping" command = "ping"
host = "8.8.4.4" host = "8.8.4.4"
[[hosts]] [[checks]]
name = "Google.com" name = "Google.com"
command = "curl" command = "curl"
host = "http://www.google.com/" host = "http://www.google.com/"

View File

@@ -24,14 +24,14 @@ use subprocess::{Exec, NullFile};
#[derive(StructOpt)] #[derive(StructOpt)]
#[structopt(name = "ips-uptime")] #[structopt(name = "ips-uptime")]
struct Opt { struct Opt {
#[structopt(parse(from_os_str))] #[structopt(short = "c", long = "config", parse(from_os_str))]
config: PathBuf, config: PathBuf,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct Config { struct Config {
command: HashMap<String, Command>, command: HashMap<String, Command>,
hosts: Vec<Host>, checks: Vec<Check>,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@@ -40,7 +40,7 @@ struct Command {
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct Host { struct Check {
name: String, name: String,
command: String, command: String,
#[serde(flatten)] #[serde(flatten)]
@@ -49,7 +49,7 @@ struct Host {
cmd: String, cmd: String,
} }
impl Host { impl Check {
fn status(&self) -> bool { fn status(&self) -> bool {
let result = Exec::shell(&self.cmd) let result = Exec::shell(&self.cmd)
.stdout(NullFile) .stdout(NullFile)
@@ -76,15 +76,15 @@ fn main() {
handlebars.set_strict_mode(true); handlebars.set_strict_mode(true);
for host in &mut config.hosts { for check in &mut config.checks {
let command = config let command = config
.command .command
.get(&host.command) .get(&check.command)
.map(|command| &command.command) .map(|command| &command.command)
.unwrap_or(&host.command); .unwrap_or(&check.command);
host.cmd = handlebars check.cmd = handlebars
.render_template(command, &host.args) .render_template(command, &check.args)
.expect("failed to register template"); .expect("failed to register template");
} }
@@ -96,10 +96,9 @@ fn main() {
let now = Local::now(); let now = Local::now();
let result = config let result = config
.hosts .checks
.iter() .iter()
// .inspect(|host| println!("{}", host.name)) .find(|check| check.status());
.find(|host| host.status());
if result.is_some() { if result.is_some() {
if let Some(down) = status { if let Some(down) = status {

View File

@@ -2,7 +2,7 @@
command = "(( RANDOM % {{ range }} != 0 ))" command = "(( RANDOM % {{ range }} != 0 ))"
[[hosts]] [[checks]]
name = "Test (1/10)" name = "Test (1/10)"
command = "random" command = "random"
range = "10" range = "10"