'memory' is now in bytes.
[valheim_web.git] / backend / src / valheim_controller.rs
1
2 use std::{ fs, time::SystemTime };
3 use sysinfo::{ ProcessExt, SystemExt };
4 use chrono::{ DateTime, offset::Local };
5
6 #[cfg(target_os = "linux")]
7 use systemd::journal;
8
9 #[derive(Clone, Debug)]
10 pub struct ValheimExe {
11 memory: u64, // [B].
12 load_average_5min: f64, // [%].
13 uptime: u64, // [s].
14 world_size: u64, // [B].
15 active_players: Vec<String>,
16 last_backup: Option<SystemTime>,
17 }
18
19 impl ValheimExe {
20 pub fn format_memory(&self) -> String {
21 format_byte_size(self.memory, 2)
22 }
23
24 pub fn format_load_average(&self) -> String {
25 format!("{:.2} %", self.load_average_5min)
26 }
27
28 pub fn format_uptime(&self) -> String {
29 let mins = self.uptime / 60;
30 let hours = mins / 60;
31 let days = hours / 24;
32 format!("{}d {}h {}min", days, hours - 24 * days, mins - 60 * hours)
33 }
34
35 pub fn format_world_size(&self) -> String {
36 format_byte_size(self.world_size, 2)
37 }
38
39 pub fn format_active_players(&self) -> String {
40 /* Commented because the player list isn't correct (the number is).
41 if self.active_players.len() == 0 {
42 String::from("<none>")
43 } else {
44 self.active_players.join(", ")
45 }*/
46 self.active_players.len().to_string()
47 }
48
49 pub fn format_last_backup(&self) -> String {
50 match self.last_backup {
51 Some(t) => {
52 let datetime: DateTime<Local> = t.into();
53 datetime.format("%d/%m/%Y %T").to_string()
54 },
55 None => String::from("?")
56 }
57 }
58 }
59
60 const BINARY_PREFIXES: [&str; 8] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"];
61
62 fn format_byte_size(bytes: u64, precision: usize) -> String {
63 for i in 0 .. 8 {
64 let mut size: u64 = 1;
65 size *= 1024u64.pow(i as u32);
66
67 if bytes < 1024 {
68 return format!("{} {}", std::cmp::max(0u64, bytes), BINARY_PREFIXES[i]);
69 }
70 else if bytes < 1024 * size {
71 return format!("{:.prec$} {}", bytes as f64 / size as f64, BINARY_PREFIXES[i], prec = precision);
72 }
73 }
74
75 String::from("")
76 }
77
78 const VALHEIM_PROCESS_NAME: &str = "valheim_server";
79
80 #[cfg(target_os = "linux")]
81 const STRING_BEFORE_CHARACTER_NAME: &str = "Got character ZDOID from";
82
83 #[cfg(target_os = "linux")]
84 const STRING_BEFORE_NB_OF_CONNECTIONS: &str = "Connections";
85
86 // It doesn't work for the moment, it only scan the connection event and do not treat disconnections.
87 #[cfg(target_os = "linux")]
88 fn get_active_players() -> Vec<String> {
89 let mut journal =
90 journal::OpenOptions::default().current_user(true).open().unwrap();
91
92 journal.seek_tail().unwrap();
93
94 let mut number_of_connections = -1i32;
95 let mut players : Vec<String> = Vec::new();
96
97 loop {
98 match journal.previous_entry() {
99 Ok(Some(entry)) => {
100 if let (Some(unit), Some(mess)) = (entry.get("_SYSTEMD_UNIT"), entry.get("MESSAGE")) {
101 if unit == "valheim.service" {
102 if let Some(pos) = mess.find(STRING_BEFORE_CHARACTER_NAME) {
103 let character_str = mess.get(pos+STRING_BEFORE_CHARACTER_NAME.len()+1..).unwrap();
104 if let Some(pos_end) = character_str.find(" : ") {
105 let player_name = String::from(character_str.get(0..pos_end).unwrap());
106 if !players.contains(&player_name) {
107 players.push(player_name);
108 if players.len() as i32 == number_of_connections {
109 return players;
110 }
111 }
112 }
113 }
114 else if let Some(pos) = mess.find(STRING_BEFORE_NB_OF_CONNECTIONS) {
115 let nb_of_connections_str = mess.get(pos+STRING_BEFORE_NB_OF_CONNECTIONS.len()+1..).unwrap();
116 if let Some(pos_end) = nb_of_connections_str.find(' ') {
117 if let Ok(n) = nb_of_connections_str.get(0..pos_end).unwrap().parse::<i32>() {
118 if number_of_connections == -1 {
119 number_of_connections = n;
120
121 if players.len() as i32 >= number_of_connections {
122 return players;
123 }
124 }
125 }
126 }
127 }
128 }
129 }
130 },
131 _ => return players
132 }
133 }
134 }
135
136 #[cfg(target_os = "windows")]
137 fn get_active_players() -> Vec<String> {
138 Vec::new()
139 }
140
141 fn get_last_backup_datetime(backup_path: &str) -> Option<SystemTime> {
142 let mut times =
143 fs::read_dir(backup_path).ok()?.filter_map(
144 |e| {
145 let dir = e.ok()?;
146 if dir.path().is_file() { Some(dir.metadata().ok()?.modified().ok()?) } else { None }
147 }
148 )
149 .collect::<Vec<SystemTime>>();
150
151 times.sort();
152
153 Some(times.last()?.clone())
154 }
155
156 pub fn get_valheim_executable_information(world_path: &str, backup_path: &str) -> Option<ValheimExe> {
157 let mut system = sysinfo::System::new_all();
158 system.refresh_system();
159 let mut processes = system.processes_by_name(VALHEIM_PROCESS_NAME);
160
161 if let Some(process) = processes.next() {
162
163 let world_size = match std::fs::metadata(world_path) { Ok(f) => f.len(), Err(_) => 0u64 };
164
165 Some(
166 ValheimExe {
167 memory: process.memory(),
168 load_average_5min: system.load_average().five / system.cpus().len() as f64 * 100.,
169 uptime: std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() - process.start_time(),
170 world_size,
171 active_players: get_active_players(),
172 last_backup: get_last_backup_datetime(backup_path)
173 }
174 )
175 } else {
176 None
177 }
178 }