#![warn(missing_docs)]
use anyhow::Result;
use clap::{app_from_crate, arg};
use nagiosplugin::{Metric, Resource, Runner, ServiceState, TriggerIfValue, Unit};
use brandmeister::last_seen_seconds;
struct Config {
repeater_id: u32,
warn_seconds: Option<i64>,
critical_seconds: Option<i64>,
}
fn get_config() -> Result<Config> {
let matches = app_from_crate!()
.arg(arg!(
-r --repeater <id> "Sets repeater id to check"
))
.arg(
arg!(
-w --warning <seconds> "Threshold for warning state"
)
.validator(|s| s.parse::<u32>())
.required(false),
)
.arg(
arg!(
-c --critical <seconds> "Threshold for critical state"
)
.validator(|s| s.parse::<u32>())
.required(false),
)
.arg(
arg!(
-H --host <hostname> "Ignored, for compatibility with nagios Host"
)
.required(false),
)
.get_matches();
Ok(Config {
repeater_id: matches.value_of_t("repeater").expect("required"),
warn_seconds: matches.value_of_t("warning").ok(),
critical_seconds: matches.value_of_t("critical").ok(),
})
}
fn do_check() -> anyhow::Result<Resource, anyhow::Error> {
let config = get_config()?;
let seconds = last_seen_seconds(config.repeater_id)?;
let resource = Resource::new(format!("BrandMeister repeater {}", config.repeater_id))
.with_description("online status")
.with_result(
Metric::new("last_seen", seconds)
.with_minimum(0)
.with_unit(Unit::Seconds)
.with_thresholds(
config.warn_seconds,
config.critical_seconds,
TriggerIfValue::Greater,
),
);
Ok(resource)
}
fn main() {
Runner::new()
.on_error(|e| (ServiceState::Unknown, e))
.safe_run(do_check)
.print_and_exit();
}