Forgot to divide the memory by 3...
[minecraft_web.git] / backend / src / minecraft_controller.rs
1 use std::{ fs, time::SystemTime };
2 use sysinfo::{ ProcessExt, SystemExt };
3 use chrono::{ DateTime, offset::Local };
4
5 #[derive(Clone, Debug)]
6 pub struct MinecraftExe {
7 memory: u64, // [B].
8 load_average_5min: f64, // [%].
9 uptime: u64, // [s].
10 world_size: u64, // [B].
11 active_players: Vec<String>,
12 version: String,
13 last_backup: Option<SystemTime>,
14 }
15
16 impl MinecraftExe {
17 pub fn format_memory(&self) -> String {
18 format_byte_size(self.memory, 2)
19 }
20
21 pub fn format_load_average(&self) -> String {
22 format!("{:.2} %", self.load_average_5min)
23 }
24
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)
30 }
31
32 pub fn format_world_size(&self) -> String {
33 format_byte_size(self.world_size, 2)
34 }
35
36 pub fn format_active_players(&self) -> String {
37 if self.active_players.len() == 0 {
38 String::from("<none>")
39 } else {
40 self.active_players.join(", ")
41 }
42 }
43
44 pub fn format_version(&self) -> String {
45 self.version.clone()
46 }
47
48 pub fn format_last_backup(&self) -> String {
49 match self.last_backup {
50 Some(t) => {
51 let datetime: DateTime<Local> = t.into();
52 datetime.format("%d/%m/%Y %T").to_string()
53 },
54 None => String::from("?")
55 }
56 }
57 }
58
59 const BINARY_PREFIXES: [&str; 8] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"];
60
61 fn format_byte_size(bytes: u64, precision: usize) -> String {
62 for i in 0 .. 8 {
63 let mut size: u64 = 1;
64 size *= 1024u64.pow(i as u32);
65
66 if bytes < 1024 {
67 return format!("{} {}", std::cmp::max(0u64, bytes), BINARY_PREFIXES[i]);
68 }
69 else if bytes < 1024 * size {
70 return format!("{:.prec$} {}", bytes as f64 / size as f64, BINARY_PREFIXES[i], prec = precision);
71 }
72 }
73
74 String::from("")
75 }
76
77 const MINECRAFT_PROCESS_NAME: &str = "java";
78
79 struct StatusFromRcon {
80 players: Vec<String>
81 }
82
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();
85
86 let status =
87 match client.authenticate(rcon_password.to_string()) {
88 Ok(_) => {
89 StatusFromRcon {
90 players:
91 match client.send_command("list".to_string()) {
92 Ok(resp) =>
93 match resp.body.find(':') {
94 Some(i) if i < resp.body.len() -1 =>
95 resp.body[i + 1..resp.body.len()]
96 .split(',')
97 .map(|nick| nick.trim().to_string())
98 .filter(|nick| !nick.is_empty())
99 .collect(),
100 _ => Vec::new()
101 },
102 Err(_e) => {
103 println!("Error from 'list' command");
104 Vec::new()
105 },
106 }
107 }
108 },
109 Err(_e) => {
110 println!("Authentication error");
111 StatusFromRcon { players: Vec::new() }
112 },
113 };
114
115 client.close().unwrap();
116
117 status
118 }
119
120 fn get_last_backup_datetime(backup_path: &str) -> Option<SystemTime> {
121 let mut times =
122 fs::read_dir(backup_path).ok()?.filter_map(
123 |e| {
124 let dir = e.ok()?;
125 if dir.path().is_file() { Some(dir.metadata().ok()?.modified().ok()?) } else { None }
126 }
127 )
128 .collect::<Vec<SystemTime>>();
129
130 times.sort();
131
132 Some(times.last()?.clone())
133 }
134
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);
139
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 };
143
144 let status_from_rcon = get_status_from_rcon(rcon_password);
145
146 Some(
147 MinecraftExe {
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(),
151 world_size,
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)
155 }
156 )
157 } else {
158 None
159 }
160 }