changeset 24:4d99295b2745

Add loadavg metric
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 14 Feb 2016 19:56:19 +0100
parents d588dc82eacc
children d3a29c57367a
files src/main.rs src/metrics/load.rs src/metrics/mod.rs
diffstat 3 files changed, 63 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/main.rs	Sun Feb 14 19:55:42 2016 +0100
+++ b/src/main.rs	Sun Feb 14 19:56:19 2016 +0100
@@ -120,8 +120,9 @@
 }
 
 fn register_metrics(registry: &mut AvailableMetrics) {
+    use metrics::load;
+    use metrics::net;
     use metrics::time;
-    use metrics::net;
 
     // List of codes: https://lifthrasiir.github.io/rust-chrono/chrono/format/strftime/index.html
     registry.register_metric("clock",
@@ -132,7 +133,11 @@
                              "Shows total received/transmitted bytes for network interaces",
                              "eth0,lo",
                              net::make_net_metric());
-
+    registry.register_metric("load",
+                             "Shows the last three load averages over the last (1, 5, 15) \
+                              minutes.",
+                             "",
+                             load::make_load_metric());
 }
 
 fn main() {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/metrics/load.rs	Sun Feb 14 19:56:19 2016 +0100
@@ -0,0 +1,55 @@
+use std::str::FromStr;
+
+extern crate regex;
+use self::regex::Regex;
+
+use framework::*;
+use helper::read_procfs_file;
+
+struct LoadAvg;
+
+impl LoadAvg {
+    fn read_load_avg() -> (String, Color) {
+        let loads = read_procfs_file(String::from("loadavg"))
+                        .unwrap_or(String::from("0.0 0.0 0.0  "));
+        let re = Regex::new(r"([0-9\.]+)\s+([0-9\.]+)\s+([0-9\.]+).*").unwrap();
+
+        match re.captures(&loads) {
+            None => (String::from("0.0 0.0 0.0"), Color::Purple),
+            Some(caps) => {
+                (format!("{} {} {}",
+                         caps.at(1).unwrap(),
+                         caps.at(2).unwrap(),
+                         caps.at(3).unwrap()),
+                 LoadAvg::get_color(caps.at(1).unwrap()))
+            }
+        }
+    }
+
+    // load is a string containing one float number.
+    fn get_color(load: &str) -> Color {
+        let f = f64::from_str(load).unwrap_or(0.);
+
+        // TODO: Make color thresholds configurable
+        if f >= 0. && f < 1.5 {
+            Color::Green
+        } else if f >= 1.5 && f < 3. {
+            Color::Orange
+        } else if f >= 3. {
+            Color::Red
+        } else {
+            Color::Default
+        }
+    }
+}
+
+impl Metric for LoadAvg {
+    fn render(&self, _: &mut MetricState) -> RenderResult {
+        let (loads, color) = LoadAvg::read_load_avg();
+        RenderResult::new(loads, color)
+    }
+}
+
+pub fn make_load_metric() -> Box<Metric> {
+    Box::new(LoadAvg)
+}
--- a/src/metrics/mod.rs	Sun Feb 14 19:55:42 2016 +0100
+++ b/src/metrics/mod.rs	Sun Feb 14 19:56:19 2016 +0100
@@ -1,2 +1,3 @@
+pub mod load;
 pub mod net;
 pub mod time;