1 use std
::{ fs
, time
::SystemTime
};
2 use sysinfo
::{ ProcessExt
, SystemExt
};
3 use chrono
::{ DateTime
, offset
::Local
};
5 #[derive(Clone, Debug)]
6 pub struct MinecraftExe
{
8 load_average_5min
: f64, // [%].
10 world_size
: u64, // [B].
11 active_players
: Vec
<String
>,
13 last_backup
: Option
<SystemTime
>,
17 pub fn format_memory(&self) -> String
{
18 format_byte_size(self.memory
, 2)
21 pub fn format_load_average(&self) -> String
{
22 format!("{:.2} %", self.load_average_5min
)
25 pub fn format_uptime(&self) -> String
{
26 let mins
= self.uptime
/ 60;
27 let hours
= mins
/ 60;
28 let days
= hours
/ 24;
29 format!("{}d {}h {}min", days
, hours
- 24 * days
, mins
- 60 * hours
)
32 pub fn format_world_size(&self) -> String
{
33 format_byte_size(self.world_size
, 2)
36 pub fn format_active_players(&self) -> String
{
37 if self.active_players
.len() == 0 {
38 String
::from("<none>")
40 self.active_players
.join(", ")
44 pub fn format_version(&self) -> String
{
48 pub fn format_last_backup(&self) -> String
{
49 match self.last_backup
{
51 let datetime
: DateTime
<Local
> = t
.into();
52 datetime
.format("%d/%m/%Y %T").to_string()
54 None
=> String
::from("?")
59 const BINARY_PREFIXES
: [&str; 8] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"];
61 fn format_byte_size(bytes
: u64, precision
: usize) -> String
{
63 let mut size
: u64 = 1;
64 size
*= 1024u64.pow(i
as u32);
67 return format!("{} {}", std
::cmp
::max(0u64, bytes
), BINARY_PREFIXES
[i
]);
69 else if bytes
< 1024 * size
{
70 return format!("{:.prec$} {}", bytes
as f64 / size
as f64, BINARY_PREFIXES
[i
], prec
= precision
);
77 const MINECRAFT_PROCESS_NAME
: &str = "java";
79 struct StatusFromRcon
{
83 fn get_status_from_rcon(rcon_password
: &str) -> StatusFromRcon
{
84 let mut client
= minecraft_client_rs
::Client
::new("127.0.0.1:25575".to_string()).unwrap();
87 match client
.authenticate(rcon_password
.to_string()) {
91 match client
.send_command("list".to_string()) {
93 match resp
.body
.find('
:'
) {
94 Some(i
) if i
< resp
.body
.len() -1 =>
95 resp
.body
[i
+ 1..resp
.body
.len()]
97 .map(|nick
| nick
.trim().to_string())
98 .filter(|nick
| !nick
.is_empty())
103 println!("Error from 'list' command");
110 println!("Authentication error");
111 StatusFromRcon
{ players
: Vec
::new() }
115 client
.close().unwrap();
120 fn get_last_backup_datetime(backup_path
: &str) -> Option
<SystemTime
> {
122 fs
::read_dir(backup_path
).ok()?
.filter_map(
125 if dir
.path().is_file() { Some(dir
.metadata().ok()?
.modified().ok()?
) } else { None
}
128 .collect
::<Vec
<SystemTime
>>();
132 Some(times
.last()?
.clone())
135 pub fn get_minecraft_executable_information(world_path
: &str, backup_path
: &str, rcon_password
: &str) -> Option
<MinecraftExe
> {
136 let mut system
= sysinfo
::System
::new_all();
137 system
.refresh_system();
138 let mut processes
= system
.processes_by_name(MINECRAFT_PROCESS_NAME
);
140 // TODO: find the correct process by checking the correct jar name in parameters.
141 if let Some(process
) = processes
.next() {
142 let world_size
= match fs_extra
::dir
::get_size(world_path
) { Ok(l
) => l
, Err(_
) => 0u64 };
144 let status_from_rcon
= get_status_from_rcon(rcon_password
);
148 memory
: process
.memory() / 3, // Because the Java garbage collector ZGC reports three times more the real memory usage: https://stackoverflow.com/a/62934057/212675
149 load_average_5min
: system
.load_average().five
/ system
.cpus().len() as f64 * 100.,
150 uptime
: std
::time
::SystemTime
::now().duration_since(std
::time
::UNIX_EPOCH
).unwrap().as_secs() - process
.start_time(),
152 active_players
: status_from_rcon
.players
,
153 version
: process
.cmd()[process
.cmd().len() - 2].clone(), // TODO: Extract the version from the .jar filename.
154 last_backup
: get_last_backup_datetime(backup_path
)