Move some of the code to a lib instead. Can be used for other stuff later.
This commit is contained in:
72
src/lib.rs
Normal file
72
src/lib.rs
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
extern crate chrono;
|
||||||
|
extern crate handlebars;
|
||||||
|
extern crate humantime;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate log;
|
||||||
|
extern crate stderrlog;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate serde_derive;
|
||||||
|
extern crate subprocess;
|
||||||
|
extern crate toml;
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use log::Level::Debug;
|
||||||
|
use subprocess::Exec;
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct Config {
|
||||||
|
pub command: HashMap<String, Command>,
|
||||||
|
pub checks: Vec<Check>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct Command {
|
||||||
|
pub command: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct Check {
|
||||||
|
pub name: String,
|
||||||
|
pub command: String,
|
||||||
|
#[serde(flatten)]
|
||||||
|
pub args: HashMap<String, String>,
|
||||||
|
#[serde(skip)]
|
||||||
|
pub cmd: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Check {
|
||||||
|
pub fn status(&self) -> bool {
|
||||||
|
info!("{}: Executing check", self.name);
|
||||||
|
|
||||||
|
let result = Exec::shell(&self.cmd)
|
||||||
|
.capture()
|
||||||
|
.expect("failed to execute command");
|
||||||
|
|
||||||
|
if log_enabled!(Debug) {
|
||||||
|
let stdout = result.stdout_str();
|
||||||
|
|
||||||
|
if !stdout.is_empty() {
|
||||||
|
debug!("{} - stdout: '{}'", self.name, result.stdout_str());
|
||||||
|
} else {
|
||||||
|
debug!("{} - stdout: <empty>", self.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
let stderr = result.stderr_str();
|
||||||
|
|
||||||
|
if !stderr.is_empty() {
|
||||||
|
debug!("{} - stderr: '{}'", self.name, result.stderr_str());
|
||||||
|
} else {
|
||||||
|
debug!("{} - stderr: <empty>", self.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if result.success() {
|
||||||
|
info!("{}: Check success", self.name);
|
||||||
|
} else {
|
||||||
|
info!("{}: Check failed", self.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
result.success()
|
||||||
|
}
|
||||||
|
}
|
||||||
62
src/main.rs
62
src/main.rs
@@ -10,8 +10,8 @@ extern crate serde_derive;
|
|||||||
extern crate structopt;
|
extern crate structopt;
|
||||||
extern crate subprocess;
|
extern crate subprocess;
|
||||||
extern crate toml;
|
extern crate toml;
|
||||||
|
extern crate isp_uptime;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::{self, Read, Write};
|
use std::io::{self, Read, Write};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
@@ -21,9 +21,8 @@ use std::time::Duration;
|
|||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use handlebars::Handlebars;
|
use handlebars::Handlebars;
|
||||||
use humantime::format_duration;
|
use humantime::format_duration;
|
||||||
use log::Level::Debug;
|
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
use subprocess::Exec;
|
use isp_uptime::*;
|
||||||
|
|
||||||
#[derive(StructOpt)]
|
#[derive(StructOpt)]
|
||||||
#[structopt(name = "ips-uptime")]
|
#[structopt(name = "ips-uptime")]
|
||||||
@@ -35,63 +34,6 @@ struct Opt {
|
|||||||
config: PathBuf,
|
config: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
struct Config {
|
|
||||||
command: HashMap<String, Command>,
|
|
||||||
checks: Vec<Check>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
struct Command {
|
|
||||||
command: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
struct Check {
|
|
||||||
name: String,
|
|
||||||
command: String,
|
|
||||||
#[serde(flatten)]
|
|
||||||
args: HashMap<String, String>,
|
|
||||||
#[serde(skip)]
|
|
||||||
cmd: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Check {
|
|
||||||
fn status(&self) -> bool {
|
|
||||||
info!("{}: Executing check", self.name);
|
|
||||||
|
|
||||||
let result = Exec::shell(&self.cmd)
|
|
||||||
.capture()
|
|
||||||
.expect("failed to execute command");
|
|
||||||
|
|
||||||
if log_enabled!(Debug) {
|
|
||||||
let stdout = result.stdout_str();
|
|
||||||
|
|
||||||
if !stdout.is_empty() {
|
|
||||||
debug!("{} - stdout: '{}'", self.name, result.stdout_str());
|
|
||||||
} else {
|
|
||||||
debug!("{} - stdout: <empty>", self.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
let stderr = result.stderr_str();
|
|
||||||
|
|
||||||
if !stderr.is_empty() {
|
|
||||||
debug!("{} - stderr: '{}'", self.name, result.stderr_str());
|
|
||||||
} else {
|
|
||||||
debug!("{} - stderr: <empty>", self.name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if result.success() {
|
|
||||||
info!("{}: Check success", self.name);
|
|
||||||
} else {
|
|
||||||
info!("{}: Check failed", self.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
result.success()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let opt = Opt::from_args();
|
let opt = Opt::from_args();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user