2013-10-10 18:02:21 +02:00
|
|
|
package mineplex.servermonitor;
|
|
|
|
|
2014-02-18 09:40:41 +01:00
|
|
|
import java.io.BufferedReader;
|
2014-04-27 08:26:56 +02:00
|
|
|
import java.io.File;
|
2014-07-30 06:10:52 +02:00
|
|
|
import java.io.IOException;
|
2014-02-18 09:40:41 +01:00
|
|
|
import java.io.InputStreamReader;
|
2014-07-30 06:10:52 +02:00
|
|
|
import java.text.SimpleDateFormat;
|
2014-04-27 08:26:56 +02:00
|
|
|
import java.util.AbstractMap;
|
2013-11-05 19:19:04 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
2013-11-06 10:03:02 +01:00
|
|
|
import java.util.Collections;
|
2014-07-30 06:10:52 +02:00
|
|
|
import java.util.Date;
|
2013-11-05 19:19:04 +01:00
|
|
|
import java.util.HashMap;
|
2014-01-08 21:18:52 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
2013-11-02 08:56:31 +01:00
|
|
|
import java.util.List;
|
2014-09-25 14:37:10 +02:00
|
|
|
import java.util.Map;
|
2014-04-27 08:26:56 +02:00
|
|
|
import java.util.Map.Entry;
|
2014-07-30 06:10:52 +02:00
|
|
|
import java.util.logging.FileHandler;
|
|
|
|
import java.util.logging.Logger;
|
2013-10-10 18:02:21 +02:00
|
|
|
|
2014-08-28 23:01:37 +02:00
|
|
|
import mineplex.core.common.util.NautHashMap;
|
2014-08-01 23:56:29 +02:00
|
|
|
import mineplex.serverdata.DedicatedServerSorter;
|
|
|
|
import mineplex.serverdata.Region;
|
|
|
|
import mineplex.serverdata.ServerManager;
|
|
|
|
import mineplex.serverdata.ServerRepository;
|
2015-03-14 01:53:36 +01:00
|
|
|
import mineplex.serverdata.data.DedicatedServer;
|
|
|
|
import mineplex.serverdata.data.MinecraftServer;
|
|
|
|
import mineplex.serverdata.data.ServerGroup;
|
2015-01-23 08:19:40 +01:00
|
|
|
import mineplex.serverdata.transfers.RestartCommand;
|
|
|
|
import mineplex.serverdata.transfers.SuicideCommand;
|
2014-08-01 23:56:29 +02:00
|
|
|
|
2013-10-10 18:02:21 +02:00
|
|
|
public class ServerMonitor
|
|
|
|
{
|
2014-08-01 23:56:29 +02:00
|
|
|
private static ServerRepository _repository = null;
|
2015-01-29 16:05:50 +01:00
|
|
|
private static StatusHistoryRepository _historyRepository = null;
|
2013-10-15 23:19:14 +02:00
|
|
|
private static int _count = 0;
|
2014-01-08 21:18:52 +01:00
|
|
|
private static HashSet<ProcessRunner> _processes = new HashSet<ProcessRunner>();
|
2014-02-18 09:40:41 +01:00
|
|
|
private static HashMap<String, Boolean> _badServers = new HashMap<String, Boolean>();
|
2014-12-23 06:49:52 +01:00
|
|
|
private static Collection<MinecraftServer> _serverStatuses = null;
|
|
|
|
private static Collection<ServerGroup> _serverGroups = null;
|
|
|
|
private static Map<String, ServerGroup> _serverGroupMap = null;
|
|
|
|
private static List<DedicatedServer> _dedicatedServers = null;
|
2015-01-06 17:39:18 +01:00
|
|
|
private static HashSet<String> _deadServers = new HashSet<String>();
|
2015-01-23 08:19:40 +01:00
|
|
|
private static HashSet<String> _laggyServers = new HashSet<String>();
|
2013-10-10 18:02:21 +02:00
|
|
|
|
2014-07-30 06:10:52 +02:00
|
|
|
private static SimpleDateFormat _dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
|
|
|
|
private static Logger _logger = Logger.getLogger("ServerMonitor");
|
|
|
|
|
2014-11-08 01:04:06 +01:00
|
|
|
private static int _totalPlayers = 0;
|
2014-12-14 02:22:01 +01:00
|
|
|
private static Region _region;
|
2014-11-08 01:04:06 +01:00
|
|
|
|
2014-12-15 04:33:38 +01:00
|
|
|
private static boolean _debug = false;
|
2015-01-03 08:58:04 +01:00
|
|
|
|
2013-10-10 18:02:21 +02:00
|
|
|
public static void main (String args[])
|
2014-04-27 08:26:56 +02:00
|
|
|
{
|
2015-03-14 01:53:36 +01:00
|
|
|
/*
|
|
|
|
MinecraftPingReply data = null;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
data = new MinecraftPing().getPing(new MinecraftPingOptions().setHostname("127.0.0.1").setPort(25565));
|
|
|
|
}
|
|
|
|
catch (IOException e2)
|
|
|
|
{
|
|
|
|
e2.printStackTrace();
|
|
|
|
}
|
|
|
|
System.out.println(data.getDescription() + " " + data.getPlayers().getOnline());
|
|
|
|
*/
|
2014-12-14 02:22:01 +01:00
|
|
|
_region = !new File("eu.dat").exists() ? Region.US : Region.EU;
|
2015-01-10 22:19:33 +01:00
|
|
|
_debug = new File("debug.dat").exists();
|
2014-12-14 02:22:01 +01:00
|
|
|
_repository = ServerManager.getServerRepository(_region); // Fetches and connects to server repo
|
2015-01-29 16:05:50 +01:00
|
|
|
_historyRepository = new StatusHistoryRepository();
|
2015-01-23 08:19:40 +01:00
|
|
|
|
2014-07-30 06:10:52 +02:00
|
|
|
File logFile = new File("monitor.log");
|
|
|
|
|
|
|
|
if (!logFile.exists())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
logFile.createNewFile();
|
|
|
|
}
|
|
|
|
catch (IOException e1)
|
|
|
|
{
|
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2014-12-23 06:49:52 +01:00
|
|
|
FileHandler fileHandler = new FileHandler("monitor.log", true);
|
2014-07-30 06:10:52 +02:00
|
|
|
fileHandler.setFormatter(new CustomFormatter());
|
|
|
|
_logger.addHandler(fileHandler);
|
|
|
|
_logger.setUseParentHandlers(false);
|
|
|
|
}
|
|
|
|
catch (SecurityException | IOException e1)
|
|
|
|
{
|
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2014-04-27 08:26:56 +02:00
|
|
|
HashMap<String, Entry<String, Long>> serverTracker = new HashMap<String, Entry<String, Long>>();
|
2013-10-10 18:02:21 +02:00
|
|
|
|
|
|
|
while (true)
|
2014-04-27 08:26:56 +02:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
try
|
2014-09-25 14:37:10 +02:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
_totalPlayers = 0;
|
|
|
|
_serverStatuses = _repository.getServerStatuses();
|
|
|
|
_serverGroups = _repository.getServerGroups(_serverStatuses);
|
|
|
|
_serverGroupMap = new HashMap<String, ServerGroup>();
|
|
|
|
_dedicatedServers = new ArrayList<DedicatedServer>(_repository.getDedicatedServers());
|
|
|
|
|
|
|
|
calculateTotalPlayers();
|
|
|
|
killDeadServers();
|
|
|
|
|
|
|
|
double totalCPU = 0.0;
|
|
|
|
double totalRAM = 0.0;
|
|
|
|
double availableCPU = 0.0;
|
|
|
|
double availableRAM = 0.0;
|
|
|
|
|
|
|
|
for (DedicatedServer server : _dedicatedServers)
|
2014-10-11 13:40:53 +02:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
totalCPU += server.getAvailableCpu();
|
|
|
|
totalRAM += server.getAvailableRam();
|
2014-10-11 13:40:53 +02:00
|
|
|
}
|
|
|
|
|
2015-02-14 09:47:03 +01:00
|
|
|
for (MinecraftServer minecraftServer : _serverStatuses)
|
2014-09-25 14:37:10 +02:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
if (minecraftServer.getMotd().contains("Finished") || (minecraftServer.getGroup().equalsIgnoreCase("UltraHardcore") && minecraftServer.getMotd().contains("Restarting") && minecraftServer.getPlayerCount() == 0))
|
|
|
|
{
|
|
|
|
killServer(minecraftServer.getName(), minecraftServer.getPublicAddress(), minecraftServer.getPlayerCount(), "[KILLED] [FINISHED] " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress(), true);
|
|
|
|
|
|
|
|
handleUserServerGroup(_serverGroupMap.get(minecraftServer.getGroup()));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (DedicatedServer server : _dedicatedServers)
|
2014-09-25 14:37:10 +02:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
if (_serverGroupMap.containsKey(minecraftServer.getGroup()) && minecraftServer.getPublicAddress().equalsIgnoreCase(server.getPrivateAddress()))
|
|
|
|
{
|
|
|
|
ServerGroup serverGroup = _serverGroupMap.get(minecraftServer.getGroup());
|
|
|
|
server.incrementServerCount(serverGroup);
|
|
|
|
}
|
2014-09-25 14:37:10 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-01 23:56:29 +02:00
|
|
|
|
2015-02-14 09:47:03 +01:00
|
|
|
if (_count % 15 == 0)
|
|
|
|
{
|
|
|
|
_badServers.clear();
|
|
|
|
|
|
|
|
for (DedicatedServer serverData : _dedicatedServers)
|
2014-02-18 09:40:41 +01:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
if (isServerOffline(serverData))
|
|
|
|
{
|
|
|
|
log("------=[OFFLINE]=------=[" + serverData.getName() + ":" + serverData.getPublicAddress() + "]=------=[OFFLINE]=------");
|
|
|
|
_badServers.put(serverData.getName(), true);
|
|
|
|
}
|
2014-02-18 09:40:41 +01:00
|
|
|
}
|
2015-02-14 09:47:03 +01:00
|
|
|
|
|
|
|
log(_badServers.size() + " bad servers.");
|
2014-02-18 09:40:41 +01:00
|
|
|
}
|
2014-07-01 19:23:22 +02:00
|
|
|
|
2015-02-14 09:47:03 +01:00
|
|
|
for (Iterator<DedicatedServer> iterator = _dedicatedServers.iterator(); iterator.hasNext();)
|
|
|
|
{
|
|
|
|
DedicatedServer serverData = iterator.next();
|
|
|
|
|
|
|
|
if (_badServers.containsKey(serverData.getName()))
|
|
|
|
iterator.remove();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
availableCPU += serverData.getAvailableCpu();
|
|
|
|
availableRAM += serverData.getAvailableRam();
|
|
|
|
}
|
2015-01-23 08:19:40 +01:00
|
|
|
}
|
2014-01-08 21:18:52 +01:00
|
|
|
|
2015-02-14 09:47:03 +01:00
|
|
|
double usedCpuPercent = Math.round((1 - availableCPU / totalCPU) * 10000.0) / 100.0;
|
|
|
|
double usedRamPercent = Math.round((1 - availableRAM / totalRAM) * 10000.0) / 100.0;
|
|
|
|
|
|
|
|
log("Using " + usedCpuPercent + "% of available CPU (" + availableCPU + " Free) and " + usedRamPercent + "% of available RAM (" + availableRAM / 1000 + "GB Free)");
|
|
|
|
|
|
|
|
_historyRepository.saveDedicatedServerStats(_dedicatedServers);
|
|
|
|
log("Saved Dedicated Server Stats.");
|
|
|
|
_historyRepository.saveServerGroupStats((int)totalCPU, (int)totalRAM, _serverGroupMap.values());
|
|
|
|
log("Saved ServerGroup Stats.");
|
2015-03-14 02:11:06 +01:00
|
|
|
//_historyRepository.saveNetworkStats(usedCpuPercent, usedRamPercent, availableCPU, availableRAM, _region);
|
2015-02-14 09:47:03 +01:00
|
|
|
log("Saved Network Stats.");
|
|
|
|
|
|
|
|
for (ServerGroup groupStatus : _serverGroups)
|
2014-01-08 21:18:52 +01:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
NautHashMap<Integer, MinecraftServer> serverMap = new NautHashMap<Integer, MinecraftServer>();
|
|
|
|
|
|
|
|
for (Iterator<MinecraftServer> serverIterator = groupStatus.getServers().iterator(); serverIterator.hasNext();)
|
2014-08-28 23:01:37 +02:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
try
|
2015-01-31 23:19:33 +01:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
MinecraftServer server = serverIterator.next();
|
|
|
|
int serverNum = Integer.parseInt(server.getName().split("-")[1]);
|
|
|
|
|
|
|
|
if (serverMap.containsKey(serverNum))
|
|
|
|
{
|
|
|
|
killServer(server.getName(), server.getPublicAddress(), server.getPlayerCount(), "[KILLED] [DUPLICATE] " + server.getName() + ":" + server.getPublicAddress(), true);
|
|
|
|
serverIterator.remove();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
serverMap.put(serverNum, server);
|
|
|
|
}
|
2015-01-31 23:19:33 +01:00
|
|
|
}
|
2015-02-14 09:47:03 +01:00
|
|
|
catch (Exception exception)
|
2015-01-31 23:19:33 +01:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
exception.printStackTrace();
|
2015-01-31 23:19:33 +01:00
|
|
|
}
|
2014-08-28 23:01:37 +02:00
|
|
|
}
|
2015-02-14 09:47:03 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
if (groupStatus.getHost() == null || groupStatus.getHost().isEmpty())
|
2014-08-28 23:01:37 +02:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
if (groupStatus.getName().startsWith("0"))
|
|
|
|
{
|
|
|
|
int serverCount = groupStatus.getServers().size();
|
|
|
|
log(groupStatus.getName() + " : " + groupStatus.getPlayerCount() + " players on " + serverCount + " servers " + String.format("%.2f", ((double)serverCount * (double)groupStatus.getRequiredCpu() / totalCPU)) + "% CPU," + String.format("%.2f", ((double)serverCount * (double)groupStatus.getRequiredRam() / totalRAM)) + "% RAM", false);
|
|
|
|
}
|
2014-08-28 23:01:37 +02:00
|
|
|
}
|
2015-02-14 09:47:03 +01:00
|
|
|
*/
|
2014-04-27 08:26:56 +02:00
|
|
|
}
|
2015-01-03 08:58:04 +01:00
|
|
|
|
2015-02-14 09:47:03 +01:00
|
|
|
HashSet<String> onlineServers = new HashSet<String>();
|
|
|
|
HashSet<String> laggyServers = new HashSet<String>();
|
|
|
|
laggyServers.addAll(_laggyServers);
|
|
|
|
_laggyServers.clear();
|
2014-08-16 08:37:06 +02:00
|
|
|
|
2015-02-14 09:47:03 +01:00
|
|
|
for (MinecraftServer minecraftServer : _serverStatuses)
|
2015-01-23 08:19:40 +01:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
onlineServers.add(minecraftServer.getName());
|
|
|
|
|
|
|
|
if (minecraftServer.getTps() <= 17)
|
2015-01-23 08:19:40 +01:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
if (minecraftServer.getTps() <= 10)
|
2015-01-23 08:19:40 +01:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
if (laggyServers.contains(minecraftServer.getName()))
|
|
|
|
{
|
|
|
|
new RestartCommand(minecraftServer.getName(), _region).publish();
|
|
|
|
log("[RESTART LAGGY] : " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_laggyServers.add(minecraftServer.getName());
|
|
|
|
log("[IMPENDING RESTART LAGGY] : " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress());
|
|
|
|
}
|
2015-01-23 08:19:40 +01:00
|
|
|
}
|
|
|
|
else
|
2015-02-14 09:47:03 +01:00
|
|
|
log("[Performance] " + minecraftServer.getName() + ":" + minecraftServer.getPublicAddress() + "] Running poorly at " + minecraftServer.getTps() + " TPS");
|
2015-01-23 08:19:40 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-21 06:24:23 +01:00
|
|
|
|
2015-02-14 09:47:03 +01:00
|
|
|
for (Iterator<Entry<String, Entry<String, Long>>> iterator = serverTracker.entrySet().iterator(); iterator.hasNext();)
|
2014-02-14 20:39:24 +01:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
Entry<String, Entry<String, Long>> entry = iterator.next();
|
2014-02-14 20:39:24 +01:00
|
|
|
|
2015-02-14 09:47:03 +01:00
|
|
|
if (onlineServers.contains(entry.getKey()))
|
|
|
|
iterator.remove();
|
|
|
|
else if (System.currentTimeMillis() - entry.getValue().getValue() > 35000)
|
|
|
|
{
|
|
|
|
String serverName = entry.getKey();
|
|
|
|
String serverAddress = entry.getValue().getKey();
|
|
|
|
killServer(serverName, serverAddress, 0, "[KILLED] [SLOW-STARTUP] " + serverName + ":" + serverAddress, true);
|
2014-02-14 20:39:24 +01:00
|
|
|
iterator.remove();
|
2015-02-14 09:47:03 +01:00
|
|
|
}
|
2014-02-14 20:39:24 +01:00
|
|
|
}
|
|
|
|
|
2015-02-14 09:47:03 +01:00
|
|
|
for (ServerGroup serverGroup : _serverGroups)
|
2014-02-14 20:39:24 +01:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
if (serverGroup.getName().equals("Testing"))
|
|
|
|
continue;
|
|
|
|
|
2014-02-18 09:40:41 +01:00
|
|
|
try
|
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
handleGroupChanges(serverTracker, serverGroup, false);
|
|
|
|
}
|
|
|
|
catch (Exception exception)
|
2014-02-18 09:40:41 +01:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
log(exception.getMessage());
|
|
|
|
log("Can't handle group changes for " + serverGroup.getName());
|
2014-02-18 09:40:41 +01:00
|
|
|
}
|
2014-02-14 20:39:24 +01:00
|
|
|
}
|
2015-02-14 09:47:03 +01:00
|
|
|
|
|
|
|
int processWaits = 0;
|
2014-02-14 20:39:24 +01:00
|
|
|
|
2015-02-14 09:47:03 +01:00
|
|
|
while (_processes.size() > 0)
|
2014-02-14 20:39:24 +01:00
|
|
|
{
|
|
|
|
for (Iterator<ProcessRunner> iterator = _processes.iterator(); iterator.hasNext();)
|
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
ProcessRunner pr = iterator.next();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
pr.join(100);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pr.isDone())
|
|
|
|
iterator.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_processes.size() > 0)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
log("Sleeping while processes run...");
|
|
|
|
Thread.sleep(6000);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2014-02-14 20:39:24 +01:00
|
|
|
}
|
2015-02-14 09:47:03 +01:00
|
|
|
|
|
|
|
if (processWaits >= 10)
|
|
|
|
{
|
|
|
|
log("Killing stale processes.");
|
|
|
|
|
|
|
|
for (Iterator<ProcessRunner> iterator = _processes.iterator(); iterator.hasNext();)
|
|
|
|
{
|
|
|
|
iterator.next().abort();
|
|
|
|
iterator.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
processWaits++;
|
|
|
|
}
|
|
|
|
|
|
|
|
processWaits = 0;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
log("Natural sleep.");
|
|
|
|
Thread.sleep(10000);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
2014-02-14 20:39:24 +01:00
|
|
|
}
|
|
|
|
|
2015-02-14 09:47:03 +01:00
|
|
|
_count++;
|
2014-02-14 20:39:24 +01:00
|
|
|
}
|
2015-02-14 09:47:03 +01:00
|
|
|
catch (Exception ex)
|
2013-10-10 18:02:21 +02:00
|
|
|
{
|
2015-02-14 09:47:03 +01:00
|
|
|
log("Error doing something : " + ex.getMessage());
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Thread.sleep(1000);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2013-10-10 18:02:21 +02:00
|
|
|
}
|
2014-02-18 09:40:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 06:49:52 +01:00
|
|
|
private static void killDeadServers()
|
|
|
|
{
|
2015-01-06 17:39:18 +01:00
|
|
|
HashSet<String> deadServers = new HashSet<String>();
|
|
|
|
deadServers.addAll(_deadServers);
|
|
|
|
|
|
|
|
_deadServers.clear();
|
2014-12-23 06:49:52 +01:00
|
|
|
for (MinecraftServer deadServer : _repository.getDeadServers())
|
|
|
|
{
|
2015-01-23 08:19:40 +01:00
|
|
|
if (deadServer.getUptime() <= 10)
|
|
|
|
continue;
|
|
|
|
|
2015-03-02 22:42:34 +01:00
|
|
|
if (_count == 0 || deadServers.contains(deadServer.getName()))
|
2015-01-06 17:39:18 +01:00
|
|
|
{
|
2015-01-10 20:53:51 +01:00
|
|
|
killServer(deadServer.getName(), deadServer.getPublicAddress(), deadServer.getPlayerCount(), "[KILLED] [DEAD] " + deadServer.getName() + ":" + deadServer.getPublicAddress(), true);
|
2015-01-06 17:39:18 +01:00
|
|
|
|
|
|
|
handleUserServerGroup(_serverGroupMap.get(deadServer.getGroup()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_deadServers.add(deadServer.getName());
|
2015-01-10 20:53:51 +01:00
|
|
|
log("[IMPENDING DEATH] : " + deadServer.getName() + ":" + deadServer.getPublicAddress());
|
2015-01-06 17:39:18 +01:00
|
|
|
}
|
2014-12-23 06:49:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void handleUserServerGroup(ServerGroup serverGroup)
|
|
|
|
{
|
2015-01-03 11:27:28 +01:00
|
|
|
if (serverGroup != null && serverGroup.getHost() != null && !serverGroup.getHost().isEmpty() && serverGroup.getServerCount() <= 1)
|
2014-12-23 06:49:52 +01:00
|
|
|
{
|
|
|
|
_repository.removeServerGroup(serverGroup);
|
|
|
|
_serverGroupMap.remove(serverGroup);
|
|
|
|
_serverGroups.remove(serverGroup);
|
|
|
|
System.out.println("Removed ServerGroup : " + serverGroup.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void calculateTotalPlayers()
|
|
|
|
{
|
|
|
|
for (ServerGroup serverGroup : _serverGroups)
|
|
|
|
{
|
|
|
|
_serverGroupMap.put(serverGroup.getName(), serverGroup);
|
|
|
|
_totalPlayers += serverGroup.getPlayerCount();
|
|
|
|
}
|
|
|
|
|
2015-01-23 08:19:40 +01:00
|
|
|
log("Total Players : " + _totalPlayers);
|
2014-12-23 06:49:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void handleGroupChanges(HashMap<String, Entry<String, Long>> serverTracker, ServerGroup serverGroup, boolean free)
|
2014-08-01 08:37:39 +02:00
|
|
|
{
|
2014-08-14 11:10:11 +02:00
|
|
|
int serverNum = 0;
|
2014-08-05 01:14:04 +02:00
|
|
|
int requiredTotal = serverGroup.getRequiredTotalServers();
|
|
|
|
int requiredJoinable = serverGroup.getRequiredJoinableServers();
|
|
|
|
int joinableServers = serverGroup.getJoinableCount();
|
|
|
|
int totalServers = serverGroup.getServerCount();
|
2014-12-15 04:33:38 +01:00
|
|
|
int serversToAdd = Math.max(0, Math.max(requiredTotal - totalServers, requiredJoinable - joinableServers));
|
2014-08-05 01:14:04 +02:00
|
|
|
int serversToKill = (totalServers > requiredTotal && joinableServers > requiredJoinable) ? Math.min(joinableServers - requiredJoinable, serverGroup.getEmptyServers().size()) : 0;
|
2015-01-23 08:19:40 +01:00
|
|
|
int serversToRestart = 0;
|
2014-08-01 08:37:39 +02:00
|
|
|
|
|
|
|
// Minimum 1500 slot bufferzone
|
2014-08-05 01:14:04 +02:00
|
|
|
if (serverGroup.getName().equalsIgnoreCase("Lobby"))
|
2014-08-01 08:37:39 +02:00
|
|
|
{
|
2014-08-18 21:46:23 +02:00
|
|
|
int availableSlots = serverGroup.getMaxPlayerCount() - serverGroup.getPlayerCount();
|
|
|
|
|
|
|
|
if (availableSlots < 1500)
|
2014-09-25 14:37:10 +02:00
|
|
|
{
|
2014-08-26 11:02:31 +02:00
|
|
|
serversToAdd = Math.max(1, (1500 - availableSlots) / serverGroup.getMaxPlayers());
|
2014-09-25 14:37:10 +02:00
|
|
|
serversToKill = 0;
|
|
|
|
}
|
2014-08-18 21:46:23 +02:00
|
|
|
else if (serversToKill > 0)
|
|
|
|
serversToKill = Math.min(serversToKill, (availableSlots - 1500) / 80);
|
2015-01-23 08:19:40 +01:00
|
|
|
else if (serversToAdd == 0 && joinableServers > requiredJoinable && totalServers > requiredTotal)
|
|
|
|
{
|
|
|
|
serversToRestart = Math.min(joinableServers - requiredJoinable, joinableServers - requiredTotal);
|
|
|
|
serversToRestart = Math.min(serversToRestart, (availableSlots - 1500) / 80);
|
|
|
|
|
|
|
|
if (serversToRestart <= 5)
|
|
|
|
serversToRestart = 0;
|
|
|
|
}
|
2014-08-01 08:37:39 +02:00
|
|
|
}
|
2014-10-24 08:58:13 +02:00
|
|
|
else if (serverGroup.getName().equalsIgnoreCase("Halloween"))
|
|
|
|
{
|
2014-12-14 02:22:01 +01:00
|
|
|
if (serverGroup.getServers().size() > (_region == Region.US ? 300 : 100))
|
|
|
|
{
|
|
|
|
serversToAdd = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (serverGroup.getName().equalsIgnoreCase("Christmas"))
|
|
|
|
{
|
|
|
|
if (serverGroup.getServers().size() > (_region == Region.US ? 300 : 100))
|
2014-10-24 08:58:13 +02:00
|
|
|
{
|
|
|
|
serversToAdd = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-11-08 01:04:06 +01:00
|
|
|
else if (serverGroup.getName().equalsIgnoreCase("UltraHardcore"))
|
|
|
|
{
|
|
|
|
int maxUHC = Math.max(1, _totalPlayers / 6000);
|
|
|
|
|
|
|
|
if (serversToAdd > 0)
|
|
|
|
serversToAdd = maxUHC - joinableServers;
|
|
|
|
|
|
|
|
if (joinableServers > maxUHC)
|
|
|
|
serversToKill = maxUHC - joinableServers;
|
|
|
|
}
|
2015-03-13 11:21:50 +01:00
|
|
|
else if (serverGroup.getName().equalsIgnoreCase("Testing"))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-08-01 08:37:39 +02:00
|
|
|
|
2014-12-23 06:49:52 +01:00
|
|
|
// KILL, CLEAN, THEN ADD
|
|
|
|
while (serversToKill > 0)
|
|
|
|
{
|
|
|
|
List<MinecraftServer> emptyServers = new ArrayList<MinecraftServer>(serverGroup.getEmptyServers());
|
|
|
|
MinecraftServer emptyServer = emptyServers.get(serversToKill - 1);
|
|
|
|
killServer(emptyServer, "[KILLED] [EXCESS] " + emptyServer.getName() + ":" + emptyServer.getPublicAddress());
|
|
|
|
serversToKill--;
|
|
|
|
}
|
|
|
|
|
2014-08-01 08:37:39 +02:00
|
|
|
while (serversToAdd > 0)
|
|
|
|
{
|
2014-08-14 11:10:11 +02:00
|
|
|
serverNum = serverGroup.generateUniqueId(serverNum + 1);
|
2015-01-23 08:19:40 +01:00
|
|
|
|
|
|
|
while (_deadServers.contains(serverGroup.getPrefix() + "-" + serverNum))
|
|
|
|
{
|
|
|
|
serverNum = serverGroup.generateUniqueId(serverNum + 1);
|
|
|
|
}
|
|
|
|
|
2014-12-23 06:49:52 +01:00
|
|
|
Collections.sort(_dedicatedServers, new DedicatedServerSorter());
|
|
|
|
DedicatedServer bestServer = getBestDedicatedServer(_dedicatedServers, serverGroup);
|
2014-11-08 01:04:06 +01:00
|
|
|
|
2014-08-01 08:37:39 +02:00
|
|
|
if (bestServer == null)
|
|
|
|
{
|
2015-03-02 22:42:34 +01:00
|
|
|
log("!!!!!!!!!!!!!!!!!!!!!!!!!!!! NO DEDICATED SERVER AVAILABLE FOR GROUP " + serverGroup.getName() + " !!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
2014-08-01 08:37:39 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-08-05 01:14:04 +02:00
|
|
|
if (serverTracker.containsKey(serverGroup.getPrefix() + "-" + serverNum))
|
2015-03-02 22:42:34 +01:00
|
|
|
log("[WAITING] On " + serverGroup.getPrefix() + "-" + serverNum + " to finish starting...");
|
2014-08-01 08:37:39 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
startServer(bestServer, serverGroup, serverNum, free);
|
2014-08-05 01:14:04 +02:00
|
|
|
serverTracker.put(serverGroup.getPrefix() + "-" + serverNum, new AbstractMap.SimpleEntry<String, Long>(bestServer.getPublicAddress(), System.currentTimeMillis()));
|
2014-08-01 08:37:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
serversToAdd--;
|
|
|
|
}
|
2015-01-23 08:19:40 +01:00
|
|
|
|
|
|
|
List<MinecraftServer> servers = new ArrayList<MinecraftServer>();
|
|
|
|
servers.addAll(serverGroup.getServers());
|
|
|
|
Collections.sort(servers, new ServerSorter());
|
|
|
|
|
|
|
|
while (serversToRestart > 0)
|
|
|
|
{
|
|
|
|
MinecraftServer server = servers.get(servers.size() - serversToRestart);
|
|
|
|
new SuicideCommand(server.getName(), _region).publish();
|
|
|
|
log("[RESTART/KILL EXCESS] : " + server.getName() + ":" + server.getPublicAddress() + " " + server.getPlayerCount() + " players");
|
|
|
|
serversToRestart--;
|
|
|
|
}
|
2014-08-01 08:37:39 +02:00
|
|
|
}
|
|
|
|
|
2015-01-10 20:53:51 +01:00
|
|
|
private static void killServer(final String serverName, final String serverAddress, final int players, final String message, final boolean announce)
|
2014-02-18 09:40:41 +01:00
|
|
|
{
|
2014-12-15 04:33:38 +01:00
|
|
|
if (_debug)
|
|
|
|
return;
|
|
|
|
|
2014-02-18 09:40:41 +01:00
|
|
|
String cmd = "/home/mineplex/easyRemoteKillServer.sh";
|
|
|
|
|
2014-08-01 23:56:29 +02:00
|
|
|
ProcessRunner pr = new ProcessRunner(new String[] {"/bin/sh", cmd, serverAddress, serverName});
|
2014-02-18 09:40:41 +01:00
|
|
|
pr.start(new GenericRunnable<Boolean>()
|
|
|
|
{
|
|
|
|
public void run(Boolean error)
|
|
|
|
{
|
2015-01-10 20:53:51 +01:00
|
|
|
MinecraftServer server = null;
|
|
|
|
|
2014-02-18 09:40:41 +01:00
|
|
|
if (!error)
|
2014-08-01 23:56:29 +02:00
|
|
|
{
|
2015-01-10 20:53:51 +01:00
|
|
|
server = _repository.getServerStatus(serverName);
|
2014-08-01 23:56:29 +02:00
|
|
|
|
|
|
|
if (server != null)
|
|
|
|
{
|
|
|
|
_repository.removeServerStatus(server);
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 09:40:41 +01:00
|
|
|
|
|
|
|
if (announce)
|
|
|
|
{
|
|
|
|
if (error)
|
2014-08-26 08:56:58 +02:00
|
|
|
log("[" + serverName + ":" + serverAddress + "] Kill errored.");
|
2014-02-18 09:40:41 +01:00
|
|
|
else
|
2015-01-10 20:53:51 +01:00
|
|
|
log(message + " Players: " + players);
|
2014-02-18 09:40:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2015-03-02 22:42:34 +01:00
|
|
|
pr.join(50);
|
2014-02-18 09:40:41 +01:00
|
|
|
}
|
|
|
|
catch (InterruptedException e1)
|
|
|
|
{
|
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!pr.isDone())
|
|
|
|
_processes.add(pr);
|
|
|
|
}
|
|
|
|
|
2014-08-01 23:56:29 +02:00
|
|
|
private static boolean isServerOffline(DedicatedServer serverData)
|
2014-02-18 09:40:41 +01:00
|
|
|
{
|
|
|
|
boolean success = false;
|
|
|
|
|
2014-12-15 04:33:38 +01:00
|
|
|
if (_debug)
|
2015-01-03 08:58:04 +01:00
|
|
|
return false;
|
2014-12-15 04:33:38 +01:00
|
|
|
|
2014-02-18 09:40:41 +01:00
|
|
|
Process process = null;
|
|
|
|
String cmd = "/home/mineplex/isServerOnline.sh";
|
|
|
|
|
2014-08-01 23:56:29 +02:00
|
|
|
ProcessBuilder processBuilder = new ProcessBuilder(new String[] {"/bin/sh", cmd, serverData.getPublicAddress()});
|
2014-02-18 09:40:41 +01:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
process = processBuilder.start();
|
|
|
|
process.waitFor();
|
|
|
|
|
|
|
|
BufferedReader reader=new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
|
|
String line = reader.readLine();
|
|
|
|
|
|
|
|
while(line != null)
|
|
|
|
{
|
|
|
|
success = line.equals("Success");
|
|
|
|
|
|
|
|
line=reader.readLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e1)
|
|
|
|
{
|
|
|
|
e1.printStackTrace();
|
2013-10-10 18:02:21 +02:00
|
|
|
}
|
2014-02-18 09:40:41 +01:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
process.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
return !success;
|
2013-10-10 18:02:21 +02:00
|
|
|
}
|
2013-11-06 10:03:02 +01:00
|
|
|
|
2014-08-01 23:56:29 +02:00
|
|
|
private static DedicatedServer getBestDedicatedServer(Collection<DedicatedServer> dedicatedServers, ServerGroup serverGroup)
|
2013-11-06 10:03:02 +01:00
|
|
|
{
|
2014-08-01 23:56:29 +02:00
|
|
|
DedicatedServer bestServer = null;
|
2013-11-06 10:03:02 +01:00
|
|
|
|
2014-08-01 23:56:29 +02:00
|
|
|
for (DedicatedServer serverData : dedicatedServers)
|
2013-11-06 10:03:02 +01:00
|
|
|
{
|
2014-08-01 23:56:29 +02:00
|
|
|
if (serverData.getAvailableRam() > serverGroup.getRequiredRam()
|
|
|
|
&& serverData.getAvailableCpu() > serverGroup.getRequiredCpu())
|
2013-11-06 10:03:02 +01:00
|
|
|
{
|
2014-11-08 01:04:06 +01:00
|
|
|
if (bestServer == null || serverData.getServerCount(serverGroup) < bestServer.getServerCount(serverGroup))
|
2013-11-06 10:03:02 +01:00
|
|
|
{
|
|
|
|
bestServer = serverData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bestServer;
|
|
|
|
}
|
2014-08-01 23:56:29 +02:00
|
|
|
|
2014-08-18 21:46:23 +02:00
|
|
|
private static void killServer(final MinecraftServer serverToKill, String message)
|
2013-11-08 19:52:56 +01:00
|
|
|
{
|
2015-01-10 20:53:51 +01:00
|
|
|
killServer(serverToKill.getName(), serverToKill.getPublicAddress(), serverToKill.getPlayerCount(), message, true);
|
2013-11-08 19:52:56 +01:00
|
|
|
}
|
|
|
|
|
2014-08-05 01:14:04 +02:00
|
|
|
private static void startServer(final DedicatedServer serverSpace, final ServerGroup serverGroup, final int serverNum, final boolean free)
|
2013-11-08 19:52:56 +01:00
|
|
|
{
|
2014-12-15 04:33:38 +01:00
|
|
|
if (_debug)
|
|
|
|
return;
|
|
|
|
|
2014-11-06 09:25:49 +01:00
|
|
|
String cmd = "/home/mineplex/easyRemoteStartServerCustom.sh";
|
2014-08-01 23:56:29 +02:00
|
|
|
final String groupPrefix = serverGroup.getPrefix();
|
|
|
|
final String serverName = serverSpace.getName();
|
|
|
|
final String serverAddress = serverSpace.getPublicAddress();
|
2014-08-14 11:10:11 +02:00
|
|
|
|
2014-11-06 09:25:49 +01:00
|
|
|
ProcessRunner pr = new ProcessRunner(new String[] {"/bin/sh", cmd, serverAddress, serverSpace.getPrivateAddress(), (serverGroup.getPortSection() + serverNum) + "", serverGroup.getRequiredRam() + "", serverGroup.getWorldZip(), serverGroup.getPlugin(), serverGroup.getConfigPath(), serverGroup.getName(), serverGroup.getPrefix() + "-" + serverNum, serverSpace.isUsRegion() ? "true" : "false", serverGroup.getAddNoCheat() + "" });
|
2014-02-18 09:40:41 +01:00
|
|
|
pr.start(new GenericRunnable<Boolean>()
|
2014-01-08 21:18:52 +01:00
|
|
|
{
|
2014-02-18 09:40:41 +01:00
|
|
|
public void run(Boolean error)
|
2014-01-08 21:18:52 +01:00
|
|
|
{
|
2014-02-18 09:40:41 +01:00
|
|
|
if (error)
|
2014-11-08 01:04:06 +01:00
|
|
|
log("[" + serverName + ":" + serverAddress + " Free Resources; CPU " + serverSpace.getAvailableCpu() + " RAM " + serverSpace.getAvailableRam() + "MB] Errored " + serverName + "(" + groupPrefix+ "-" + serverNum + (free ? "-FREE" : "") + ")");
|
2014-02-18 09:40:41 +01:00
|
|
|
else
|
2014-11-08 01:04:06 +01:00
|
|
|
log("[" + serverName + ":" + serverAddress + " Free Resources; CPU " + serverSpace.getAvailableCpu() + " RAM " + serverSpace.getAvailableRam() + "MB] Added " + serverName + "(" + groupPrefix+ "-" + serverNum + (free ? "-FREE" : "") + ")");
|
2014-08-05 01:14:04 +02:00
|
|
|
|
2014-01-08 21:18:52 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2015-03-02 22:42:34 +01:00
|
|
|
pr.join(100);
|
2014-01-08 21:18:52 +01:00
|
|
|
}
|
|
|
|
catch (InterruptedException e1)
|
|
|
|
{
|
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2014-08-01 23:56:29 +02:00
|
|
|
serverSpace.incrementServerCount(serverGroup);
|
2014-01-08 21:18:52 +01:00
|
|
|
|
|
|
|
if (!pr.isDone())
|
|
|
|
_processes.add(pr);
|
2013-11-08 19:52:56 +01:00
|
|
|
}
|
2014-07-30 06:10:52 +02:00
|
|
|
|
|
|
|
private static void log(String message)
|
2015-01-03 08:58:04 +01:00
|
|
|
{
|
|
|
|
log(message, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void log(String message, boolean fileOnly)
|
2014-07-30 06:10:52 +02:00
|
|
|
{
|
|
|
|
_logger.info("[" + _dateFormat.format(new Date()) + "] " + message);
|
2015-01-03 08:58:04 +01:00
|
|
|
|
|
|
|
if (!fileOnly)
|
|
|
|
System.out.println("[" + _dateFormat.format(new Date()) + "] " + message);
|
2014-07-30 06:10:52 +02:00
|
|
|
}
|
2013-10-10 18:02:21 +02:00
|
|
|
}
|