From 2f8ea6c56e1d0f6d481c31766fb18c883c673068 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Mon, 15 Mar 2021 11:37:19 +0100 Subject: [PATCH] Display the number of active player --- Cargo.lock | 82 +++++++++++++++++++++++++++++++ backend/Cargo.toml | 1 + backend/src/main.rs | 8 +-- backend/src/valheim_controller.rs | 41 +++++++++++++++- backend/templates/main.html | 1 + 5 files changed, 128 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0d1303a..cd8fd77 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -480,6 +480,12 @@ dependencies = [ "libc", ] +[[package]] +name = "build-env" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cf89846ef2b2674ef1c153256cec98fba587c72bf4ea2c4b2f6d91a19f55926" + [[package]] name = "byteorder" version = "1.4.3" @@ -608,6 +614,16 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "cstr-argument" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20bd4e8067c20c7c3a4dea759ef91d4b18418ddb5bd8837ef6e2f2f93ca7ccbb" +dependencies = [ + "cfg-if 0.1.10", + "memchr", +] + [[package]] name = "derive_more" version = "0.99.11" @@ -698,6 +714,33 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foreign-types" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d737d9aa519fb7b749cbc3b962edcf310a8dd1f4b67c91c4f83975dbdd17d965" +dependencies = [ + "foreign-types-macros", + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-macros" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63f713f8b2aa9e24fec85b0e290c56caee12e3b6ae0aeeda238a75b28251afd6" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "foreign-types-shared" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7684cf33bb7f28497939e8c7cf17e3e4e3b8d9a0080ffa4f8ae2f515442ee855" + [[package]] name = "form_urlencoded" version = "1.0.1" @@ -1014,6 +1057,17 @@ version = "0.2.88" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03b07a082330a35e43f63177cc01689da34fbffa0105e1246cf0311472cac73a" +[[package]] +name = "libsystemd-sys" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e03fd580bcecda68dcdcd5297085ade6a3dc552cd8b030d2b94a9b089ef7ab8" +dependencies = [ + "build-env", + "libc", + "pkg-config", +] + [[package]] name = "linked-hash-map" version = "0.5.4" @@ -1312,6 +1366,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" + [[package]] name = "ppv-lite86" version = "0.2.10" @@ -1606,6 +1666,21 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "systemd" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f722cabda922e471742300045f56dbaa53fafbb4520fca304e51258019bfe91d" +dependencies = [ + "cstr-argument", + "foreign-types", + "libc", + "libsystemd-sys", + "log", + "memchr", + "utf8-cstr", +] + [[package]] name = "thread_local" version = "1.1.3" @@ -1828,6 +1903,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "utf8-cstr" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55bcbb425141152b10d5693095950b51c3745d019363fc2929ffd8f61449b628" + [[package]] name = "v_escape" version = "0.7.4" @@ -1873,6 +1954,7 @@ dependencies = [ "ron", "serde", "sysinfo", + "systemd", ] [[package]] diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 5a4b1e1..1b5f3d0 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -14,6 +14,7 @@ ron = "0.6" # Rust object notation, to load configuration files. itertools = "0.10" sysinfo = "0.16" +systemd = "0.8" futures = "0.3" # Needed by askam with the feature 'with-actix-web'. diff --git a/backend/src/main.rs b/backend/src/main.rs index c4c51a0..84485c2 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -19,7 +19,8 @@ struct MainTemplate { memory: String, load_average: String, uptime: String, - world_size: String + world_size: String, + nb_of_players: u32, } const VALUE_UNKNOWN: &str = "-"; @@ -35,11 +36,12 @@ async fn main_page(config_shared: web::Data>) -> impl Responder { memory: info.format_memory(), load_average: info.format_load_average(), uptime: info.format_uptime(), - world_size: info.format_world_size() + world_size: info.format_world_size(), + nb_of_players: info.get_nb_of_player() }, None => { let value_unknown = String::from(VALUE_UNKNOWN); - MainTemplate { text_status: String::from("Valheim server is down :("), memory: value_unknown.clone(), load_average: value_unknown.clone(), uptime: value_unknown.clone(), world_size: 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(), world_size: value_unknown.clone(), nb_of_players: 0 } } } } diff --git a/backend/src/valheim_controller.rs b/backend/src/valheim_controller.rs index a483756..457abc1 100644 --- a/backend/src/valheim_controller.rs +++ b/backend/src/valheim_controller.rs @@ -1,4 +1,5 @@ -use sysinfo::{ProcessExt, SystemExt}; +use sysinfo::{ ProcessExt, SystemExt }; +use systemd::journal; #[derive(Debug)] pub struct ValheimExe { @@ -6,24 +7,32 @@ pub struct ValheimExe { load_average_5min: f64, // [%]. uptime: u64, // [s]. world_size: u64, // [B]. + nb_of_players: u32, } impl ValheimExe { pub fn format_memory(&self) -> String { format_byte_size(self.memory * 1024, 2) } + pub fn format_load_average(&self) -> String { format!("{:.2} %", self.load_average_5min) } + pub fn format_uptime(&self) -> String { let mins = self.uptime / 60; let hours = mins / 60; let days = hours / 24; format!("{}d{}h{}min", days, hours - 24 * days, mins - 60 * hours) } + pub fn format_world_size(&self) -> String { format_byte_size(self.world_size, 2) } + + pub fn get_nb_of_player(&self) -> u32 { + self.nb_of_players + } } const BINARY_PREFIXES: [&str; 8] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"]; @@ -46,6 +55,33 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { const VALHEIM_PROCESS_NAME: &str = "valheim_server"; +fn get_number_of_players() -> u32 { + let mut journal = + journal::OpenOptions::default().current_user(true).open().unwrap(); + + journal.seek_tail().unwrap(); + + loop { + match journal.previous_entry() { + Ok(Some(entry)) => { + if let (Some(unit), Some(mess)) = (entry.get("_SYSTEMD_UNIT"), entry.get("MESSAGE")) { + if unit == "valheim.service" { + if let Some(pos) = mess.find("Connections") { + let nb_of_connections_str = mess.get(pos+12..).unwrap(); + if let Some(pos_end) = nb_of_connections_str.find(' ') { + if let Ok(n) = nb_of_connections_str.get(0..pos_end).unwrap().parse() { + return n; + } + } + } + } + } + }, + _ => return 0 + } + } +} + pub fn get_valheim_executable_information(world_path : &str) -> Option { let mut system = sysinfo::System::new_all(); system.refresh_system(); @@ -61,7 +97,8 @@ pub fn get_valheim_executable_information(world_path : &str) -> Option

{{ text_status }}

+

Number of active player: {{ nb_of_players }}

World size: {{ world_size }}

Memory used: {{ memory }}

Load average (5 min): {{ load_average }}

-- 2.45.2