extern crate simple_munin_plugin;
mod load {
use std::io::Read;
use std::fs::File;
use simple_munin_plugin::*;
const FIELD: &'static str = "load";
pub struct LoadPlugin;
impl LoadPlugin {
pub fn new() -> LoadPlugin {
LoadPlugin
}
pub fn parse(s: &str) -> Option<&str> {
s.split_whitespace().nth(1)
}
}
impl MuninNodePlugin for LoadPlugin {
fn config(&self) {
println!(r#"graph_title Load average
graph_args --base 1000 -l 0
graph_vlabel load
graph_scale no
graph_category system
graph_info Load average (written in Rust)
{0}.label load
{0}.info 5 minutes average"#, FIELD);
Self::print_if_env(Level::Warning, FIELD);
Self::print_if_env(Level::Critical, FIELD);
}
fn run(&self) {
let mut file = File::open("/proc/loadavg").expect("couldn't open file");
let mut content = String::new();
file.read_to_string(&mut content).expect("couldn't read content");
println!("{}.value {}", FIELD, Self::parse(&content).expect("couldn't parse content"));
}
}
}
use simple_munin_plugin::*;
fn main() {
let plugin = load::LoadPlugin::new();
std::process::exit(plugin.start());
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse() {
assert_eq!(Some("1.02"), LoadPlugin::parse("0.01 1.02 3.02 1/183 9236\n"));
assert_eq!(None, LoadPlugin::parse(""));
}
}