From 72ee7c19c8966973a67f5b1c8e940fb8d893c322 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sun, 20 Jun 2021 17:46:13 +0200 Subject: [PATCH 1/3] Untested remote start system Signed-off-by: Lixfel --- .../steamwar/bungeecore/LoadEvaluation.java | 92 +++++++++++++++++++ .../steamwar/bungeecore/SubserverSystem.java | 29 +++++- 2 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 src/de/steamwar/bungeecore/LoadEvaluation.java diff --git a/src/de/steamwar/bungeecore/LoadEvaluation.java b/src/de/steamwar/bungeecore/LoadEvaluation.java new file mode 100644 index 0000000..d1785af --- /dev/null +++ b/src/de/steamwar/bungeecore/LoadEvaluation.java @@ -0,0 +1,92 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bungeecore; + +import java.io.*; + +public class LoadEvaluation { + private LoadEvaluation(){} + + private static final File meminfo = new File("/proc/meminfo"); + + public static double getRamPercentage() { + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(meminfo)))) { + String memTotal = bufferedReader.readLine().replaceAll(" +", " "); + bufferedReader.readLine(); + String memAvailable = bufferedReader.readLine().replaceAll(" +", " "); + + long memTotalLong = getNumber(memTotal); + long memAvailableLong = getNumber(memAvailable); + return (memTotalLong - memAvailableLong) / (double) memTotalLong; + } catch (IOException e) { + return 1D; + } + } + + public static double getRemoteRamPercentage() { + try { + Process process = new ProcessBuilder("ssh lx cat /proc/meminfo").start(); + process.waitFor(); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String memTotal = bufferedReader.readLine().replaceAll(" +", " "); + bufferedReader.readLine(); + String memAvailable = bufferedReader.readLine().replaceAll(" +", " "); + + long memTotalLong = getNumber(memTotal); + long memAvailableLong = getNumber(memAvailable); + return (memTotalLong - memAvailableLong) / (double) memTotalLong; + } catch (IOException e) { + return 1D; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return 1D; + } + } + + public static double getCPULoad() { + try { + Process process = new ProcessBuilder("cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS=\"\" '{printf \"%.2f\\n\", ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}'").start(); + process.waitFor(); + return Double.parseDouble(new BufferedReader(new InputStreamReader(process.getInputStream())).readLine()); + } catch (IOException e) { + return 1D; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return 1D; + } + } + + public static double getRemoteCPULoad() { + try { + Process process = new ProcessBuilder("ssh lx cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS=\"\" '{printf \"%.2f\\n\", ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}'").start(); + process.waitFor(); + return Double.parseDouble(new BufferedReader(new InputStreamReader(process.getInputStream())).readLine()); + } catch (IOException e) { + return 1D; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return 1D; + } + } + + private static long getNumber(String s) { + return Long.parseLong(s.split(" ")[1]); + } +} diff --git a/src/de/steamwar/bungeecore/SubserverSystem.java b/src/de/steamwar/bungeecore/SubserverSystem.java index 2812eb3..3498008 100644 --- a/src/de/steamwar/bungeecore/SubserverSystem.java +++ b/src/de/steamwar/bungeecore/SubserverSystem.java @@ -118,8 +118,10 @@ public class SubserverSystem { Thread.currentThread().interrupt(); } + File directory = new File(SERVER_PATH, modus.getFolder()); List cmd = serverStartCommand( modus.serverJar(), + directory, worldDir, mapName, port, @@ -135,7 +137,7 @@ public class SubserverSystem { //Start server ProcessBuilder process = new ProcessBuilder(cmd); - process.directory(new File(SERVER_PATH, modus.getFolder())); + process.directory(directory); String finalMapName = mapName; if(eventFightID == -1) @@ -185,8 +187,10 @@ public class SubserverSystem { copyBauweltIfRequired(p, prototype, worldFolder + worldName); int port = freePort(4000); + File directory = new File(SERVER_PATH, serverName); List cmd = serverStartCommand( serverJar, + directory, worldDir, worldName, port, @@ -195,7 +199,7 @@ public class SubserverSystem { //Start server ProcessBuilder process = new ProcessBuilder(cmd); - process.directory(new File(SERVER_PATH, serverName)); + process.directory(directory); new Bauserver(user.getUserName() + "s Bau", owner, port, process, () -> {}).sendPlayer(p); } @@ -231,9 +235,20 @@ public class SubserverSystem { new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/bau addmember " + p.getName())); } - private static List serverStartCommand(String serverJar, String worldDir, String levelName, int port, String xmx, String... dParams){ + private static List serverStartCommand(String serverJar, File directory, String worldDir, String levelName, int port, String xmx, String... dParams){ List cmd = new ArrayList<>(); boolean jdk11 = serverJar.contains("1.15.2"); + + if(!steamwarStartAvailible() && lixfelStartAvailible()){ + cmd.add("ssh"); + cmd.add("-L"); + cmd.add(port + ":localhost:" + port); + cmd.add("lx"); + cmd.add("cd"); + cmd.add(directory.getPath()); + cmd.add(";"); + } + if(jdk11) cmd.add("/usr/lib/jvm/java-11-openjdk-amd64/bin/java"); else @@ -259,6 +274,14 @@ public class SubserverSystem { return cmd; } + private static boolean steamwarStartAvailible(){ + return LoadEvaluation.getCPULoad() < 0.8 && LoadEvaluation.getRamPercentage() < 0.8; + } + + private static boolean lixfelStartAvailible(){ + return LoadEvaluation.getRemoteCPULoad() < 0.8 && LoadEvaluation.getRemoteRamPercentage() < 0.8; + } + private static boolean bauRunning(ProxiedPlayer p, UUID owner){ for(Subserver subserver : Subserver.getServerList()){ if(subserver.getType() == Servertype.BAUSERVER && ((Bauserver)subserver).getOwner().equals(owner)){ -- 2.39.5 From 85e129ada651dd376e90d9c1474ba325dab1fe2c Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 20 Jun 2021 18:03:41 +0200 Subject: [PATCH 2/3] Fix LoadEvaluation Fix naming --- src/de/steamwar/bungeecore/LoadEvaluation.java | 8 ++++---- src/de/steamwar/bungeecore/SubserverSystem.java | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/de/steamwar/bungeecore/LoadEvaluation.java b/src/de/steamwar/bungeecore/LoadEvaluation.java index d1785af..d4acab0 100644 --- a/src/de/steamwar/bungeecore/LoadEvaluation.java +++ b/src/de/steamwar/bungeecore/LoadEvaluation.java @@ -40,9 +40,9 @@ public class LoadEvaluation { } } - public static double getRemoteRamPercentage() { + public static double getRemoteRamPercentage(String remote) { try { - Process process = new ProcessBuilder("ssh lx cat /proc/meminfo").start(); + Process process = new ProcessBuilder("ssh " + remote + " cat /proc/meminfo").start(); process.waitFor(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); String memTotal = bufferedReader.readLine().replaceAll(" +", " "); @@ -73,9 +73,9 @@ public class LoadEvaluation { } } - public static double getRemoteCPULoad() { + public static double getRemoteCPULoad(String remote) { try { - Process process = new ProcessBuilder("ssh lx cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS=\"\" '{printf \"%.2f\\n\", ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}'").start(); + Process process = new ProcessBuilder("ssh " + remote + " cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS=\"\" '{printf \"%.2f\\n\", ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}'").start(); process.waitFor(); return Double.parseDouble(new BufferedReader(new InputStreamReader(process.getInputStream())).readLine()); } catch (IOException e) { diff --git a/src/de/steamwar/bungeecore/SubserverSystem.java b/src/de/steamwar/bungeecore/SubserverSystem.java index 3498008..d002d2c 100644 --- a/src/de/steamwar/bungeecore/SubserverSystem.java +++ b/src/de/steamwar/bungeecore/SubserverSystem.java @@ -239,7 +239,7 @@ public class SubserverSystem { List cmd = new ArrayList<>(); boolean jdk11 = serverJar.contains("1.15.2"); - if(!steamwarStartAvailible() && lixfelStartAvailible()){ + if(!steamwarStartAvailable() && lixfelStartAvailable()){ cmd.add("ssh"); cmd.add("-L"); cmd.add(port + ":localhost:" + port); @@ -274,12 +274,12 @@ public class SubserverSystem { return cmd; } - private static boolean steamwarStartAvailible(){ + private static boolean steamwarStartAvailable(){ return LoadEvaluation.getCPULoad() < 0.8 && LoadEvaluation.getRamPercentage() < 0.8; } - private static boolean lixfelStartAvailible(){ - return LoadEvaluation.getRemoteCPULoad() < 0.8 && LoadEvaluation.getRemoteRamPercentage() < 0.8; + private static boolean lixfelStartAvailable(){ + return LoadEvaluation.getRemoteCPULoad("lx") < 0.8 && LoadEvaluation.getRemoteRamPercentage("lx") < 0.8; } private static boolean bauRunning(ProxiedPlayer p, UUID owner){ -- 2.39.5 From 5aa973d3889cd20689ab58be6e1bdf459a843315 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 20 Jun 2021 19:02:05 +0200 Subject: [PATCH 3/3] Fix LoadEvaluation --- src/de/steamwar/bungeecore/LoadEvaluation.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/de/steamwar/bungeecore/LoadEvaluation.java b/src/de/steamwar/bungeecore/LoadEvaluation.java index d4acab0..0732d75 100644 --- a/src/de/steamwar/bungeecore/LoadEvaluation.java +++ b/src/de/steamwar/bungeecore/LoadEvaluation.java @@ -42,7 +42,9 @@ public class LoadEvaluation { public static double getRemoteRamPercentage(String remote) { try { - Process process = new ProcessBuilder("ssh " + remote + " cat /proc/meminfo").start(); + // Attention: + // memInfo.sh needs to contain: cat /proc/meminfo + Process process = new ProcessBuilder("ssh", remote, "\"./memInfo.sh\"").start(); process.waitFor(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); String memTotal = bufferedReader.readLine().replaceAll(" +", " "); @@ -64,7 +66,7 @@ public class LoadEvaluation { try { Process process = new ProcessBuilder("cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS=\"\" '{printf \"%.2f\\n\", ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}'").start(); process.waitFor(); - return Double.parseDouble(new BufferedReader(new InputStreamReader(process.getInputStream())).readLine()); + return Double.parseDouble(new BufferedReader(new InputStreamReader(process.getInputStream())).readLine()) / 100.0; } catch (IOException e) { return 1D; } catch (InterruptedException e) { @@ -75,9 +77,11 @@ public class LoadEvaluation { public static double getRemoteCPULoad(String remote) { try { - Process process = new ProcessBuilder("ssh " + remote + " cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS=\"\" '{printf \"%.2f\\n\", ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}'").start(); + // Attention: + // cpuLoad.sh needs to contain: cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS="" '{printf "%.2f\n", ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}' + Process process = new ProcessBuilder("ssh", remote, "\"./cpuLoad.sh\"").start(); process.waitFor(); - return Double.parseDouble(new BufferedReader(new InputStreamReader(process.getInputStream())).readLine()); + return Double.parseDouble(new BufferedReader(new InputStreamReader(process.getInputStream())).readLine()) / 100.0; } catch (IOException e) { return 1D; } catch (InterruptedException e) { -- 2.39.5