#[macro_use]
extern crate derive_more;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate scan_fmt;
extern crate libc;
extern crate nix;
extern crate regex;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
use std::process;
use std::fmt;
use std::str::FromStr;
pub mod linux;
pub mod procfs;
pub mod sys;
pub mod scripts;
#[derive(Debug)]
pub enum TabinError {
UnknownValue(String),
}
impl fmt::Display for TabinError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self {
&TabinError::UnknownValue(ref msg) => write!(f, "Unknown Value: {}", msg),
}
}
}
pub type TabinResult<T> = Result<T, TabinError>;
#[must_use]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Deserialize)]
pub enum Status {
Unknown,
Ok,
Warning,
Critical,
}
impl Status {
pub fn exit(&self) -> ! {
use self::Status::*;
match *self {
Ok => process::exit(0),
Warning => process::exit(1),
Critical => process::exit(2),
Unknown => process::exit(3),
}
}
pub fn str_values() -> [&'static str; 4] {
["ok", "warning", "critical", "unknown"]
}
}
impl FromStr for Status {
type Err = TabinError;
fn from_str(s: &str) -> TabinResult<Status> {
use Status::{Critical, Unknown, Warning};
match s {
"ok" => Ok(Status::Ok),
"warning" => Ok(Warning),
"critical" => Ok(Critical),
"unknown" => Ok(Unknown),
_ => Err(TabinError::UnknownValue(format!(
"Unexpected exit status: {}",
s
))),
}
}
}
impl fmt::Display for Status {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Status::*;
let msg = match *self {
Ok => "OK",
Unknown => "UNKNOWN",
Warning => "WARNING",
Critical => "CRITICAL",
};
write!(f, "{}", msg)
}
}
#[test]
fn comparison_is_as_expected() {
use Status::*;
assert!(Ok < Critical);
assert!(Ok < Warning);
assert_eq!(std::cmp::max(Warning, Critical), Critical)
}