package mineplex.servermonitor; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; public class ServerMonitor { private static Repository _repository = new Repository(); private static int _count = 0; private static HashSet _processes = new HashSet(); public static void main (String args[]) { _repository.initialize(); while (true) { while (_processes.size() > 0) { for (Iterator iterator = _processes.iterator(); iterator.hasNext();) { ProcessRunner pr = iterator.next(); try { pr.join(100); } catch (InterruptedException e) { e.printStackTrace(); } if (pr.isDone()) iterator.remove(); } try { System.out.println("Sleeping while processes run..."); Thread.sleep(6000); } catch (InterruptedException e) { e.printStackTrace(); } if (_count >= 10) { System.out.println("Killing stale processes."); for (Iterator iterator = _processes.iterator(); iterator.hasNext();) { iterator.next().abort(); iterator.remove(); } } _count++; } _count = 0; for (ServerStatusData statusData : _repository.retrieveOldServerStatuses()) { System.out.println("----Old Server Status----> " + statusData.Address + ", " + statusData.Name); killServer(statusData); //restartServer(statusData); } List dynamicServers = new ArrayList(_repository.retrieveDynamicServers()); Collection serverGroups = _repository.retrieveServerGroups(); HashMap groupStatusList = _repository.retrieveGroupStatusData(); for (ServerGroupData serverGroup : serverGroups) { if (!groupStatusList.containsKey(serverGroup.Name)) { groupStatusList.put(serverGroup.Name, new GroupStatusData()); } GroupStatusData groupStatus = groupStatusList.get(serverGroup.Name); int serversToAdd = Math.max(serverGroup.RequiredTotalServers - groupStatus.getTotalServers(), serverGroup.RequiredJoinableServers - groupStatus.getJoinableCount()); int serversToKill = (groupStatus.getTotalServers() > serverGroup.RequiredTotalServers && groupStatus.getJoinableCount() > serverGroup.RequiredJoinableServers) ? Math.min(groupStatus.getJoinableCount() - serverGroup.RequiredJoinableServers, groupStatus.EmptyServers.size()) : 0; while (serversToAdd > 0) { int serverNum = groupStatus.getNextServerNumber(); Collections.sort(dynamicServers, new DynamicServerSorter()); DynamicServerData bestServer = getBestDynamicServer(dynamicServers, serverGroup); if (bestServer == null) { System.out.println("No best dynamic server available for group " + serverGroup.Name); break; } System.out.println("[" + bestServer.Name + ":" + bestServer.Address + "] Adding " + serverGroup.Name + " Req Total: " + serverGroup.RequiredTotalServers + " Req Joinable: " + serverGroup.RequiredJoinableServers + " | Actual Total: " + groupStatus.getTotalServers() + " Actual Joinable: " + groupStatus.getJoinableCount()); startServer(bestServer, serverGroup, serverNum); serversToAdd--; } while (serversToKill > 0) { System.out.println("[" + groupStatus.EmptyServers.get(0).Name + ":" + groupStatus.EmptyServers.get(0).Address + "] Killing " + serverGroup.Name + " Req Total: " + serverGroup.RequiredTotalServers + " Req Joinable: " + serverGroup.RequiredJoinableServers + " | Actual Total: " + groupStatus.getTotalServers() + " Actual Joinable: " + groupStatus.getJoinableCount()); killServer(groupStatus.EmptyServers.remove(0)); serversToKill--; } } try { System.out.println("Natural sleep."); Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } } private static DynamicServerData getBestDynamicServer(Collection dynamicServers, ServerGroupData serverGroup) { DynamicServerData bestServer = null; for (DynamicServerData serverData : dynamicServers) { if (serverData.AvailableRAM > serverGroup.RequiredRAM && serverData.AvailableCPU > serverGroup.RequiredCPU) { if (bestServer == null) { bestServer = serverData; if (!serverData.ServerGroupCount.containsKey(serverGroup.Name)) break; } else if (serverData.ServerGroupCount.containsKey(serverGroup.Name)) { if (serverData.ServerGroupCount.get(serverGroup.Name) < bestServer.ServerGroupCount.get(serverGroup.Name)) bestServer = serverData; } } } return bestServer; } private static void restartServer(final ServerStatusData serverToKill) { String cmd = "/home/mineplex/restartServer.sh"; ProcessRunner pr = new ProcessRunner(new String[] {"/bin/sh", cmd, serverToKill.Address, serverToKill.Name}); pr.start(new Runnable() { public void run() { System.out.println("Restart command to " + serverToKill.Address + " for " + serverToKill.Name + " completed"); } }); try { pr.join(500); } catch (InterruptedException e1) { e1.printStackTrace(); } if (!pr.isDone()) _processes.add(pr); } private static void killServer(final ServerStatusData serverToKill) { String cmd = "/home/mineplex/easyRemoteKillServer.sh"; ProcessRunner pr = new ProcessRunner(new String[] {"/bin/sh", cmd, serverToKill.Address, serverToKill.Name}); pr.start(new Runnable() { public void run() { _repository.removeServerRecord(serverToKill); System.out.println("Sent kill command to " + serverToKill.Address + " for " + serverToKill.Name + " completed"); } }); try { pr.join(500); } catch (InterruptedException e1) { e1.printStackTrace(); } if (!pr.isDone()) _processes.add(pr); } private static void startServer(final DynamicServerData serverSpace, final ServerGroupData serverGroup, final int serverNum) { String cmd = "/home/mineplex/easyRemoteStartServer.sh"; ProcessRunner pr = new ProcessRunner(new String[] {"/bin/sh", cmd, serverSpace.Address, serverSpace.PrivateAddress, serverGroup.ScriptName, serverGroup.Prefix + "-" + serverNum, "1", serverSpace.US ? "us" : "eu"}); pr.start(new Runnable() { public void run() { serverSpace.setServerGroupCount(serverGroup, serverSpace.ServerGroupCount.containsKey(serverGroup.Name) ? (serverSpace.ServerGroupCount.get(serverGroup.Name) + 1) : 1); System.out.println("Start command (" + serverSpace.Address + "," + serverGroup.ScriptName + "," + serverGroup.Prefix + "-" + serverNum + ", 1," + (serverSpace.US ? "us" : "eu") + ") completed"); } }); try { pr.join(500); } catch (InterruptedException e1) { e1.printStackTrace(); } if (!pr.isDone()) _processes.add(pr); } }