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 }}"
|
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/"
|
||||||
|
|||||||
23
src/main.rs
23
src/main.rs
@@ -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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user