load/
load.rs

1extern crate simple_munin_plugin;
2
3mod load {
4    use std::io::Read;
5    use std::fs::File;
6    use simple_munin_plugin::*;
7
8
9    const FIELD: &'static str = "load";
10
11    pub struct LoadPlugin;
12
13    impl LoadPlugin {
14        pub fn new() -> LoadPlugin {
15            LoadPlugin
16        }
17
18        pub fn parse(s: &str) -> Option<&str> {
19            s.split_whitespace().nth(1)
20        }
21    }
22
23    impl MuninNodePlugin for LoadPlugin {
24        fn config(&self) {
25            println!(r#"graph_title Load average
26graph_args --base 1000 -l 0
27graph_vlabel load
28graph_scale no
29graph_category system
30graph_info Load average (written in Rust)
31{0}.label load
32{0}.info 5 minutes average"#, FIELD);
33            Self::print_if_env(Level::Warning, FIELD);
34            Self::print_if_env(Level::Critical, FIELD);
35        }
36
37        fn run(&self) {
38            let mut file = File::open("/proc/loadavg").expect("couldn't open file");
39            let mut content = String::new();
40            file.read_to_string(&mut content).expect("couldn't read content");
41            //    println!("content: {}\n", content);
42            println!("{}.value {}", FIELD, Self::parse(&content).expect("couldn't parse content"));
43        }
44    }
45}
46
47use simple_munin_plugin::*;
48
49fn main() {
50    let plugin = load::LoadPlugin::new();
51    std::process::exit(plugin.start());
52}
53
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59
60    #[test]
61    fn test_parse() {
62        assert_eq!(Some("1.02"), LoadPlugin::parse("0.01 1.02 3.02 1/183 9236\n"));
63        assert_eq!(None, LoadPlugin::parse(""));
64    }
65
66}