2 use std
::{ fs
, time
::SystemTime
};
3 use sysinfo
::{ ProcessExt
, SystemExt
};
4 use chrono
::{ DateTime
, offset
::Local
};
6 #[cfg(target_os = "linux")]
9 #[derive(Clone, Debug)]
10 pub struct ValheimExe
{
12 load_average_5min
: f64, // [%].
14 world_size
: u64, // [B].
15 active_players
: Vec
<String
>,
16 last_backup
: Option
<SystemTime
>,
20 pub fn format_memory(&self) -> String
{
21 format_byte_size(self.memory
, 2)
24 pub fn format_load_average(&self) -> String
{
25 format!("{:.2} %", self.load_average_5min
)
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
)
35 pub fn format_world_size(&self) -> String
{
36 format_byte_size(self.world_size
, 2)
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>")
44 self.active_players.join(", ")
46 self.active_players
.len().to_string()
49 pub fn format_last_backup(&self) -> String
{
50 match self.last_backup
{
52 let datetime
: DateTime
<Local
> = t
.into();
53 datetime
.format("%d/%m/%Y %T").to_string()
55 None
=> String
::from("?")
60 const BINARY_PREFIXES
: [&str; 8] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"];
62 fn format_byte_size(bytes
: u64, precision
: usize) -> String
{
64 let mut size
: u64 = 1;
65 size
*= 1024u64.pow(i
as u32);
68 return format!("{} {}", std
::cmp
::max(0u64, bytes
), BINARY_PREFIXES
[i
]);
70 else if bytes
< 1024 * size
{
71 return format!("{:.prec$} {}", bytes
as f64 / size
as f64, BINARY_PREFIXES
[i
], prec
= precision
);
78 const VALHEIM_PROCESS_NAME
: &str = "valheim_server";
80 #[cfg(target_os = "linux")]
81 const STRING_BEFORE_CHARACTER_NAME
: &str = "Got character ZDOID from";
83 #[cfg(target_os = "linux")]
84 const STRING_BEFORE_NB_OF_CONNECTIONS
: &str = "Connections";
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
> {
90 journal
::OpenOptions
::default().current_user(true).open().unwrap();
92 journal
.seek_tail().unwrap();
94 let mut number_of_connections
= -1i32;
95 let mut players
: Vec
<String
> = Vec
::new();
98 match journal
.previous_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
{
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
;
121 if players
.len() as i32 >= number_of_connections
{
136 #[cfg(target_os = "windows")]
137 fn get_active_players() -> Vec
<String
> {
141 fn get_last_backup_datetime(backup_path
: &str) -> Option
<SystemTime
> {
143 fs
::read_dir(backup_path
).ok()?
.filter_map(
146 if dir
.path().is_file() { Some(dir
.metadata().ok()?
.modified().ok()?
) } else { None
}
149 .collect
::<Vec
<SystemTime
>>();
153 Some(times
.last()?
.clone())
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
);
161 if let Some(process
) = processes
.next() {
163 let world_size
= match std
::fs
::metadata(world_path
) { Ok(f
) => f
.len(), Err(_
) => 0u64 };
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(),
171 active_players
: get_active_players(),
172 last_backup
: get_last_backup_datetime(backup_path
)