diff --git a/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/Repository.java b/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/Repository.java index 993be5504..1c76989fb 100644 --- a/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/Repository.java +++ b/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/Repository.java @@ -16,20 +16,23 @@ public class Repository private String _connectionString = "jdbc:mysql://db.mineplex.com:3306/ServerStatus"; private String _userName = "root"; private String _password = "tAbechAk3wR7tuTh"; + private boolean _us = true; private static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ServerStatus (id INT NOT NULL AUTO_INCREMENT, serverName VARCHAR(256), serverGroup VARCHAR(256), address VARCHAR(256), port VARCHAR(11), updated LONG, lastTimeWithPlayers LONG, motd VARCHAR(256), players INT, maxPlayers INT, tps INT, ram INT, maxRam INT, PRIMARY KEY (id));"; - private static String RETRIEVE_OLD_SERVER_STATUSES = "SELECT ServerStatus.serverName, DynamicServers.address, ServerStatus.address, ServerStatus.port, motd, players, maxPlayers, now(), updated FROM ServerStatus INNER JOIN DynamicServers ON ServerStatus.address = DynamicServers.privateAddress WHERE DynamicServers.US = false;"; + private static String RETRIEVE_OLD_SERVER_STATUSES = "SELECT ServerStatus.serverName, DynamicServers.address, ServerStatus.address, ServerStatus.port, motd, players, maxPlayers, now(), updated FROM ServerStatus INNER JOIN DynamicServers ON ServerStatus.address = DynamicServers.privateAddress WHERE DynamicServers.US = ?;"; private static String CREATE_DYNAMIC_TABLE = "CREATE TABLE IF NOT EXISTS DynamicServers (id INT NOT NULL AUTO_INCREMENT, serverName VARCHAR(256), address VARCHAR(256), privateAddress VARCHAR(256), US BOOLEAN NOT NULL DEFAULT 'true', PRIMARY KEY (id));"; - private static String RETRIEVE_AVAILABLE_SERVERS = "SELECT DynamicServers.serverName, DynamicServers.address, DynamicServers.privateAddress, DynamicServers.US, DynamicServers.availableCpu, DynamicServers.availableRam, ServerStatus.serverGroup, COUNT(*) As serverCount FROM DynamicServers LEFT JOIN ServerStatus ON ServerStatus.address = DynamicServers.privateAddress WHERE DynamicServers.US = false GROUP BY DynamicServers.address, ServerStatus.serverGroup;"; - private static String RETRIEVE_SERVERGROUP_STATUSES = "SELECT ServerStatus.serverName, serverGroup, motd, DynamicServers.address, ServerStatus.address, ServerStatus.port, players, maxPlayers, case when TIME_TO_SEC(TIMEDIFF(now(), ServerStatus.lastTimeWithPlayers)) > 300 then 1 else 0 end as empty, now(), updated FROM ServerStatus INNER JOIN DynamicServers ON ServerStatus.address = DynamicServers.privateAddress WHERE DynamicServers.US = false"; + private static String RETRIEVE_AVAILABLE_SERVERS = "SELECT DynamicServers.serverName, DynamicServers.address, DynamicServers.privateAddress, DynamicServers.US, DynamicServers.availableCpu, DynamicServers.availableRam, ServerStatus.serverGroup, COUNT(*) As serverCount FROM DynamicServers LEFT JOIN ServerStatus ON ServerStatus.address = DynamicServers.privateAddress WHERE DynamicServers.US = ? GROUP BY DynamicServers.address, ServerStatus.serverGroup;"; + private static String RETRIEVE_SERVERGROUP_STATUSES = "SELECT ServerStatus.serverName, serverGroup, motd, DynamicServers.address, ServerStatus.address, ServerStatus.port, players, maxPlayers, case when TIME_TO_SEC(TIMEDIFF(now(), ServerStatus.lastTimeWithPlayers)) > 300 then 1 else 0 end as empty, now(), updated FROM ServerStatus INNER JOIN DynamicServers ON ServerStatus.address = DynamicServers.privateAddress WHERE DynamicServers.US = ?"; private static String RETRIEVE_SERVER_GROUP_DATA = "SELECT groupName, prefix, scriptName, requiredRam, cpuRequired, requiredTotal, requiredJoinable FROM ServerGroups;"; private static String DELETE_SERVER_STATUS = "DELETE FROM ServerStatus WHERE address = ? AND port = ? AND serverName = ?;"; public static Connection connection; - public void initialize() + public void initialize(boolean us) { + _us = us; + PreparedStatement preparedStatement = null; try @@ -105,6 +108,8 @@ public class Repository connection = DriverManager.getConnection(_connectionString, _userName, _password); preparedStatement = connection.prepareStatement(RETRIEVE_OLD_SERVER_STATUSES); + preparedStatement.setBoolean(1, _us); + resultSet = preparedStatement.executeQuery(); while (resultSet.next()) @@ -174,6 +179,8 @@ public class Repository connection = DriverManager.getConnection(_connectionString, _userName, _password); preparedStatement = connection.prepareStatement(RETRIEVE_SERVERGROUP_STATUSES); + preparedStatement.setBoolean(1, _us); + resultSet = preparedStatement.executeQuery(); while (resultSet.next()) @@ -271,6 +278,8 @@ public class Repository resultSet.close(); preparedStatement = connection.prepareStatement(RETRIEVE_AVAILABLE_SERVERS); + preparedStatement.setBoolean(1, _us); + resultSet = preparedStatement.executeQuery(); while (resultSet.next()) diff --git a/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/ServerMonitor.java b/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/ServerMonitor.java index dc67135d3..56ba4a52e 100644 --- a/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/ServerMonitor.java +++ b/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/ServerMonitor.java @@ -1,7 +1,9 @@ package mineplex.servermonitor; import java.io.BufferedReader; +import java.io.File; import java.io.InputStreamReader; +import java.util.AbstractMap; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -9,6 +11,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Map.Entry; public class ServerMonitor { @@ -18,92 +21,149 @@ public class ServerMonitor private static HashMap _badServers = new HashMap(); public static void main (String args[]) - { - _repository.initialize(); + { + boolean us = !new File("eu.dat").exists(); + + _repository.initialize(us); + HashMap> serverTracker = new HashMap>(); while (true) - { - 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()); - - if (_count % 15 == 0) - { - _badServers.clear(); - - for (DynamicServerData serverData : dynamicServers) - { - if (isServerOffline(serverData)) - { - System.out.println("------=[OFFLINE]=------=[" + serverData.Name + ":" + serverData.Address + "]=------=[OFFLINE]=------"); - _badServers.put(serverData.Name, true); - } - } - - System.out.println(_badServers.size() + " bad servers."); - } - - for (Iterator iterator = dynamicServers.iterator(); iterator.hasNext();) - { - DynamicServerData serverData = iterator.next(); - - if (_badServers.containsKey(serverData.Name)) - iterator.remove(); - } - + { Collection serverGroups = _repository.retrieveServerGroups(); HashMap groupStatusList = _repository.retrieveGroupStatusData(); - - for (GroupStatusData groupStatus : groupStatusList.values()) + + for (ServerStatusData statusData : _repository.retrieveOldServerStatuses()) { - for (ServerStatusData serverToKill : groupStatus.KillServers) + if (us) { - System.out.println("----DUPLICATE SERVER----> " + serverToKill.Address + ", " + serverToKill.Name); - killServer(serverToKill); + if (!serverTracker.containsKey(statusData.Name)) + { + restartServer(statusData); + serverTracker.put(statusData.Name, new AbstractMap.SimpleEntry(statusData.Address, System.currentTimeMillis())); + } + else if (System.currentTimeMillis() - serverTracker.get(statusData.Name).getValue() > 15000) + { + serverTracker.remove(statusData.Name); + System.out.println("-=[SERVER RESTART TOO SLOW]=- " + statusData.Name + " (Putting back in restart queue)"); + } } + else + killServer(statusData); } - for (ServerGroupData serverGroup : serverGroups) + if (us) { - if (!groupStatusList.containsKey(serverGroup.Name)) + // Remove successfully restarted US servers + for (GroupStatusData groupStatus : groupStatusList.values()) { - 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) + for (ServerStatusData serverToKill : groupStatus.Servers.values()) { - System.out.println("No best dynamic server available for group " + serverGroup.Name); - break; + if (serverTracker.containsKey(serverToKill.Name)) + serverTracker.remove(serverToKill.Name); } - - 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); + } + } + else + { + List dynamicServers = new ArrayList(_repository.retrieveDynamicServers()); + + if (_count % 15 == 0) + { + _badServers.clear(); - serversToAdd--; + for (DynamicServerData serverData : dynamicServers) + { + if (isServerOffline(serverData)) + { + System.out.println("------=[OFFLINE]=------=[" + serverData.Name + ":" + serverData.Address + "]=------=[OFFLINE]=------"); + _badServers.put(serverData.Name, true); + } + } + + System.out.println(_badServers.size() + " bad servers."); } - while (serversToKill > 0) + for (Iterator iterator = dynamicServers.iterator(); iterator.hasNext();) { - 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--; + DynamicServerData serverData = iterator.next(); + + if (_badServers.containsKey(serverData.Name)) + iterator.remove(); + } + + for (GroupStatusData groupStatus : groupStatusList.values()) + { + for (ServerStatusData serverToKill : groupStatus.KillServers) + { + System.out.println("----DUPLICATE SERVER----> " + serverToKill.Address + ", " + serverToKill.Name); + killServer(serverToKill); + } + + for (ServerStatusData serverToKill : groupStatus.Servers.values()) + { + if (serverTracker.containsKey(serverToKill.Name)) + serverTracker.remove(serverToKill.Name); + } + } + + for (Iterator>> iterator = serverTracker.entrySet().iterator(); iterator.hasNext();) + { + Entry> entry = iterator.next(); + + if (System.currentTimeMillis() - entry.getValue().getValue() > 15000) + { + System.out.println("-=[SERVER STARTUP TOO SLOW]=- " + entry.getKey()); + + ServerStatusData serverToKill = new ServerStatusData(); + serverToKill.Name = entry.getKey(); + serverToKill.Address = entry.getValue().getKey(); + + killServer(serverToKill); + iterator.remove(); + } + } + + 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; + } + + if (serverTracker.containsKey(serverGroup.Prefix + "-" + serverNum)) + System.out.println("[WAITING] On " + serverGroup.Prefix + "-" + serverNum + " to finish starting..."); + else + { + startServer(bestServer, serverGroup, serverNum); + serverTracker.put(serverGroup.Prefix + "-" + serverNum, new AbstractMap.SimpleEntry(bestServer.Address, System.currentTimeMillis())); + } + + 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--; + } } }