1 use sysinfo
::{ ProcessExt
, SystemExt
};
3 use std
::{ fs
, time
::SystemTime
};
5 use chrono
::{ DateTime
, offset
::Local
};
7 #[derive(Clone, Debug)]
8 pub struct MinecraftExe
{
10 load_average_5min
: f64, // [%].
12 world_size
: u64, // [B].
13 active_players
: Vec
<String
>,
14 last_backup
: Option
<SystemTime
>,
18 pub fn format_memory(&self) -> String
{
19 format_byte_size(self.memory
* 1024, 2)
22 pub fn format_load_average(&self) -> String
{
23 format!("{:.2} %", self.load_average_5min
)
26 pub fn format_uptime(&self) -> String
{
27 let mins
= self.uptime
/ 60;
28 let hours
= mins
/ 60;
29 let days
= hours
/ 24;
30 format!("{}d {}h {}min", days
, hours
- 24 * days
, mins
- 60 * hours
)
33 pub fn format_world_size(&self) -> String
{
34 format_byte_size(self.world_size
, 2)
37 pub fn format_active_players(&self) -> String
{
38 /* Commented because the player list isn't correct (the number is).
39 if self.active_players.len() == 0 {
40 String::from("<none>")
42 self.active_players.join(", ")
44 self.active_players
.len().to_string()
47 pub fn format_last_backup(&self) -> String
{
48 match self.last_backup
{
50 let datetime
: DateTime
<Local
> = t
.into();
51 datetime
.format("%d/%m/%Y %T").to_string()
53 None
=> String
::from("?")
58 const BINARY_PREFIXES
: [&str; 8] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"];
60 fn format_byte_size(bytes
: u64, precision
: usize) -> String
{
62 let mut size
: u64 = 1;
63 size
*= 1024u64.pow(i
as u32);
66 return format!("{} {}", std
::cmp
::max(0u64, bytes
), BINARY_PREFIXES
[i
]);
68 else if bytes
< 1024 * size
{
69 return format!("{:.prec$} {}", bytes
as f64 / size
as f64, BINARY_PREFIXES
[i
], prec
= precision
);
76 const MINECRAFT_PROCESS_NAME
: &str = "java";
78 #[cfg(target_os = "linux")]
79 const STRING_BEFORE_CHARACTER_NAME
: &str = "Got character ZDOID from";
81 #[cfg(target_os = "linux")]
82 const STRING_BEFORE_NB_OF_CONNECTIONS
: &str = "Connections";
84 // It doesn't work for the moment, it only scan the connection event and do not treat disconnections.
85 fn get_active_players(rcon_password
: &str) -> Vec
<String
> {
86 let mut client
= minecraft_client_rs
::Client
::new("127.0.0.1:25575".to_string()).unwrap();
89 match client
.authenticate(rcon_password
.to_string()) {
91 match client
.send_command("list".to_string()) {
93 println!("{}", resp
.body
);
97 println!("Error asking seed");
103 println!("Authentication error");
108 client
.close().unwrap();
113 fn get_last_backup_datetime(backup_path
: &str) -> Option
<SystemTime
> {
115 fs
::read_dir(backup_path
).ok()?
.filter_map(
118 if dir
.path().is_file() { Some(dir
.metadata().ok()?
.modified().ok()?
) } else { None
}
121 .collect
::<Vec
<SystemTime
>>();
125 Some(times
.last()?
.clone())
128 pub fn get_minecraft_executable_information(world_path
: &str, backup_path
: &str, rcon_password
: &str) -> Option
<MinecraftExe
> {
129 let mut system
= sysinfo
::System
::new_all();
130 system
.refresh_system();
131 let processes
= system
.get_process_by_name(MINECRAFT_PROCESS_NAME
);
133 // TODO: find the correct process by checking the correct jar name in parameters.
134 if processes
.len() >= 1 {
135 let process
= processes
.first().unwrap();
137 let world_size
= match std
::fs
::metadata(world_path
) { Ok(f
) => f
.len(), Err(_
) => 0u64 };
141 memory
: process
.memory(),
142 load_average_5min
: system
.get_load_average().five
/ system
.get_processors().len() as f64 * 100.,
143 uptime
: std
::time
::SystemTime
::now().duration_since(std
::time
::UNIX_EPOCH
).unwrap().as_secs() - process
.start_time(),
145 active_players
: get_active_players(rcon_password
),
146 last_backup
: get_last_backup_datetime(backup_path
)