0aa92de483b41e70f70f060938d8868e44ee4b3c
1 use sysinfo
::{ ProcessExt
, SystemExt
};
3 use std
::{ fs
, time
::SystemTime
};
5 use chrono
::{ DateTime
, offset
::Local
};
7 #[cfg(target_os = "linux")]
10 #[derive(Clone, Debug)]
11 pub struct ValheimExe
{
13 load_average_5min
: f64, // [%].
15 world_size
: u64, // [B].
16 active_players
: Vec
<String
>,
17 last_backup
: Option
<SystemTime
>,
21 pub fn format_memory(&self) -> String
{
22 format_byte_size(self.memory
* 1024, 2)
25 pub fn format_load_average(&self) -> String
{
26 format!("{:.2} %", self.load_average_5min
)
29 pub fn format_uptime(&self) -> String
{
30 let mins
= self.uptime
/ 60;
31 let hours
= mins
/ 60;
32 let days
= hours
/ 24;
33 format!("{}d {}h {}min", days
, hours
- 24 * days
, mins
- 60 * hours
)
36 pub fn format_world_size(&self) -> String
{
37 format_byte_size(self.world_size
, 2)
40 pub fn format_active_players(&self) -> String
{
41 self.active_players
.join(", ")
44 pub fn format_last_backup(&self) -> String
{
45 match self.last_backup
{
47 let datetime
: DateTime
<Local
> = t
.into();
48 datetime
.format("%d/%m/%Y %T").to_string()
50 None
=> String
::from("?")
55 const BINARY_PREFIXES
: [&str; 8] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"];
57 fn format_byte_size(bytes
: u64, precision
: usize) -> String
{
59 let mut size
: u64 = 1;
60 size
*= 1024u64.pow(i
as u32);
63 return format!("{} {}", std
::cmp
::max(0u64, bytes
), BINARY_PREFIXES
[i
]);
65 else if bytes
< 1024 * size
{
66 return format!("{:.prec$} {}", bytes
as f64 / size
as f64, BINARY_PREFIXES
[i
], prec
= precision
);
73 const VALHEIM_PROCESS_NAME
: &str = "valheim_server";
75 #[cfg(target_os = "linux")]
76 fn get_active_players() -> Vec
<String
> {
78 journal
::OpenOptions
::default().current_user(true).open().unwrap();
80 journal
.seek_tail().unwrap();
82 let mut number_of_connections
= -1;
83 let mut players
: Vec
<String
> = Vec
::new();
86 match journal
.previous_entry() {
88 if let (Some(unit
), Some(mess
)) = (entry
.get("_SYSTEMD_UNIT"), entry
.get("MESSAGE")) {
89 if unit
== "valheim.service" {
90 //"Got character ZDOID from {}"
91 if let Some(pos
) = mess
.find("Got character ZDOID from") {
92 let character_str
= mess
.get(pos
+25..).unwrap();
93 if let Some(pos_end
) = character_str
.find(" : ") {
94 let player_name
= String
::from(character_str
.get(0..pos_end
));
95 if !players
.contains(player_name
) {
96 players
.push(player_name
);
97 if players
.len() == number_of_connections
{
103 else if let Some(pos
) = mess
.find("Connections") {
104 let nb_of_connections_str
= mess
.get(pos
+12..).unwrap();
105 if let Some(pos_end
) = nb_of_connections_str
.find(' '
) {
106 if let Ok(n
) = nb_of_connections_str
.get(0..pos_end
).unwrap().parse() {
107 number_of_connections
= n
;
108 if players
.len() >= number_of_connections
{
122 #[cfg(target_os = "windows")]
123 fn get_active_players() -> Vec
<String
> {
127 fn get_last_backup_datetime(backup_path
: &str) -> Option
<SystemTime
> {
129 fs
::read_dir(backup_path
).ok()?
.filter_map(
132 if dir
.path().is_file() { Some(dir
.metadata().ok()?
.modified().ok()?
) } else { None
}
135 .collect
::<Vec
<SystemTime
>>();
139 Some(times
.last()?
.clone())
142 pub fn get_valheim_executable_information(world_path
: &str, backup_path
: &str) -> Option
<ValheimExe
> {
143 let mut system
= sysinfo
::System
::new_all();
144 system
.refresh_system();
145 let processes
= system
.get_process_by_name(VALHEIM_PROCESS_NAME
);
147 if processes
.len() >= 1 {
148 let process
= processes
.first().unwrap();
150 let world_size
= match std
::fs
::metadata(world_path
) { Ok(f
) => f
.len(), Err(_
) => 0u64 };
154 memory
: process
.memory(),
155 load_average_5min
: system
.get_load_average().five
/ system
.get_processors().len() as f64 * 100.,
156 uptime
: std
::time
::SystemTime
::now().duration_since(std
::time
::UNIX_EPOCH
).unwrap().as_secs() - process
.start_time(),
158 active_players
: get_active_players(),
159 last_backup
: get_last_backup_datetime(backup_path
)