Replace cpu usage by load average
authorGreg Burri <greg.burri@gmail.com>
Wed, 10 Mar 2021 15:22:37 +0000 (16:22 +0100)
committerGreg Burri <greg.burri@gmail.com>
Wed, 10 Mar 2021 15:22:37 +0000 (16:22 +0100)
backend/src/main.rs
backend/src/valheim_controller.rs
backend/templates/main.html

index 88424da..ce9ae9f 100644 (file)
@@ -21,7 +21,7 @@ mod valheim_controller;
 struct MainTemplate {
     text_status: String,
     memory: String,
-    cpu: String,
+    load_average: String,
     uptime: String
 }
 
@@ -41,12 +41,12 @@ async fn main_page(/*key_shared: web::Data<Mutex<String>>,*/ query: Query<Reques
             MainTemplate {
                 text_status: String::from("Valheim server is up and running :)"),
                 memory: info.format_memory(),
-                cpu: info.format_cpu_usage(),
+                load_average: info.format_load_average(),
                 uptime: info.format_uptime()
             },
         None => {
             let value_unknown = String::from(VALUE_UNKNOWN);
-            MainTemplate { text_status: String::from("Valheim server is down :("), memory: value_unknown.clone(), cpu: value_unknown.clone(), uptime: value_unknown.clone() }
+            MainTemplate { text_status: String::from("Valheim server is down :("), memory: value_unknown.clone(), load_average: value_unknown.clone(), uptime: value_unknown.clone() }
         }
     }
 
index cf18e08..fd6dcc4 100644 (file)
@@ -1,11 +1,9 @@
-use std::cmp::min;\r
-\r
 use sysinfo::{ ProcessExt, SystemExt };\r
 \r
 #[derive(Debug)]\r
 pub struct ValheimExe {\r
     memory: u64, // [kB].\r
-    cpu_usage: f32, // [%].\r
+    load_average_5min: f64, // [%].\r
     uptime: u64, // [s].\r
 }\r
 \r
@@ -13,8 +11,8 @@ impl ValheimExe {
     pub fn format_memory(&self) -> String {\r
         format_byte_size(self.memory * 1024, 2)\r
     }\r
-    pub fn format_cpu_usage(&self) -> String {\r
-        format!("{:.2} %", self.cpu_usage)\r
+    pub fn format_load_average(&self) -> String {\r
+        format!("{:.2} %", self.load_average_5min)\r
     }\r
     pub fn format_uptime(&self) -> String {\r
         let mins = self.uptime / 60;\r
@@ -44,7 +42,7 @@ fn format_byte_size(bytes: u64, precision: usize) -> String {
     String::from("")\r
 }\r
 \r
-const VALHEIM_PROCESS_NAME: &str = "valheim_server";\r
+const VALHEIM_PROCESS_NAME: &str = "firefox"; //valheim_server";\r
 \r
 pub fn get_valheim_executable_information() -> Option<ValheimExe> {\r
     let mut system = sysinfo::System::new_all();\r
@@ -57,11 +55,11 @@ pub fn get_valheim_executable_information() -> Option<ValheimExe> {
         Some(\r
             ValheimExe {\r
                 memory: process.memory(),\r
-                cpu_usage: process.cpu_usage(),\r
+                load_average_5min: system.get_load_average().five,\r
                 uptime: std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() - process.start_time()\r
             }\r
         )\r
     } else {\r
         None\r
     }\r
-}
\ No newline at end of file
+}\r
index 2d7991e..b096875 100644 (file)
@@ -10,8 +10,8 @@
     <body>\r
         <div class="container">\r
             <h1>{{ text_status }}</h1>\r
-            <p>Memory: {{ memory }}</p>\r
-            <p>CPU: {{ cpu }}</p>\r
+            <p>Memory used: {{ memory }}</p>\r
+            <p>Load average (5 min): {{ load_average }}</p>\r
             <p>Uptime: {{ uptime }}</p>\r
         </div>\r
     </body>\r