Use -c for config. Fix #3.
This commit is contained in:
10
config.toml
10
config.toml
@@ -5,27 +5,27 @@ command = "ping -c 1 -t 3 {{ host }}" # -t should be -w on linux environments.
|
||||
command = "curl --head --connect-timeout {{ host }}"
|
||||
|
||||
|
||||
[[hosts]]
|
||||
[[checks]]
|
||||
name = "Cloudflare/APNIC DNS (primary)"
|
||||
command = "ping"
|
||||
host = "1.1.1.1"
|
||||
|
||||
[[hosts]]
|
||||
[[checks]]
|
||||
name = "Cloudflare/APNIC DNS (secondary)"
|
||||
command = "ping"
|
||||
host = "1.0.0.1"
|
||||
|
||||
[[hosts]]
|
||||
[[checks]]
|
||||
name = "Google DNS (primary)"
|
||||
command = "ping"
|
||||
host = "8.8.8.8"
|
||||
|
||||
[[hosts]]
|
||||
[[checks]]
|
||||
name = "Google DNS (secondary)"
|
||||
command = "ping"
|
||||
host = "8.8.4.4"
|
||||
|
||||
[[hosts]]
|
||||
[[checks]]
|
||||
name = "Google.com"
|
||||
command = "curl"
|
||||
host = "http://www.google.com/"
|
||||
|
||||
23
src/main.rs
23
src/main.rs
@@ -24,14 +24,14 @@ use subprocess::{Exec, NullFile};
|
||||
#[derive(StructOpt)]
|
||||
#[structopt(name = "ips-uptime")]
|
||||
struct Opt {
|
||||
#[structopt(parse(from_os_str))]
|
||||
#[structopt(short = "c", long = "config", parse(from_os_str))]
|
||||
config: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Config {
|
||||
command: HashMap<String, Command>,
|
||||
hosts: Vec<Host>,
|
||||
checks: Vec<Check>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -40,7 +40,7 @@ struct Command {
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Host {
|
||||
struct Check {
|
||||
name: String,
|
||||
command: String,
|
||||
#[serde(flatten)]
|
||||
@@ -49,7 +49,7 @@ struct Host {
|
||||
cmd: String,
|
||||
}
|
||||
|
||||
impl Host {
|
||||
impl Check {
|
||||
fn status(&self) -> bool {
|
||||
let result = Exec::shell(&self.cmd)
|
||||
.stdout(NullFile)
|
||||
@@ -76,15 +76,15 @@ fn main() {
|
||||
|
||||
handlebars.set_strict_mode(true);
|
||||
|
||||
for host in &mut config.hosts {
|
||||
for check in &mut config.checks {
|
||||
let command = config
|
||||
.command
|
||||
.get(&host.command)
|
||||
.get(&check.command)
|
||||
.map(|command| &command.command)
|
||||
.unwrap_or(&host.command);
|
||||
.unwrap_or(&check.command);
|
||||
|
||||
host.cmd = handlebars
|
||||
.render_template(command, &host.args)
|
||||
check.cmd = handlebars
|
||||
.render_template(command, &check.args)
|
||||
.expect("failed to register template");
|
||||
}
|
||||
|
||||
@@ -96,10 +96,9 @@ fn main() {
|
||||
let now = Local::now();
|
||||
|
||||
let result = config
|
||||
.hosts
|
||||
.checks
|
||||
.iter()
|
||||
// .inspect(|host| println!("{}", host.name))
|
||||
.find(|host| host.status());
|
||||
.find(|check| check.status());
|
||||
|
||||
if result.is_some() {
|
||||
if let Some(down) = status {
|
||||
|
||||
Reference in New Issue
Block a user