2013-10-10 18:02:21 +02:00
|
|
|
package mineplex.servermonitor;
|
|
|
|
|
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;
|
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;
|
2013-10-10 18:02:21 +02:00
|
|
|
|
|
|
|
public class ServerMonitor
|
|
|
|
{
|
|
|
|
private static Repository _repository = new Repository();
|
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>();
|
2013-10-10 18:02:21 +02:00
|
|
|
|
|
|
|
public static void main (String args[])
|
|
|
|
{
|
|
|
|
_repository.initialize();
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2014-01-08 21:18:52 +01:00
|
|
|
while (_processes.size() > 0)
|
2013-10-10 18:02:21 +02:00
|
|
|
{
|
2014-01-08 21:18:52 +01:00
|
|
|
for (Iterator<ProcessRunner> iterator = _processes.iterator(); iterator.hasNext();)
|
2013-10-15 23:19:14 +02:00
|
|
|
{
|
2014-01-08 21:18:52 +01:00
|
|
|
ProcessRunner pr = iterator.next();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
pr.join(100);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pr.isDone())
|
|
|
|
iterator.remove();
|
2013-10-15 23:19:14 +02:00
|
|
|
}
|
2013-11-08 19:52:56 +01:00
|
|
|
|
2014-01-08 21:18:52 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
System.out.println("Sleeping while processes run...");
|
|
|
|
Thread.sleep(6000);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2013-11-02 08:56:31 +01:00
|
|
|
|
2014-01-08 21:18:52 +01:00
|
|
|
if (_count >= 10)
|
2013-11-06 10:03:02 +01:00
|
|
|
{
|
2014-01-08 21:18:52 +01:00
|
|
|
System.out.println("Killing stale processes.");
|
|
|
|
|
|
|
|
for (Iterator<ProcessRunner> iterator = _processes.iterator(); iterator.hasNext();)
|
2013-11-06 10:03:02 +01:00
|
|
|
{
|
2014-01-08 21:18:52 +01:00
|
|
|
iterator.next().abort();
|
|
|
|
iterator.remove();
|
2013-11-06 10:03:02 +01:00
|
|
|
}
|
2014-01-08 21:18:52 +01:00
|
|
|
}
|
2013-11-06 10:03:02 +01:00
|
|
|
|
2014-01-08 21:18:52 +01:00
|
|
|
_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
_count = 0;
|
|
|
|
|
|
|
|
for (ServerStatusData statusData : _repository.retrieveOldServerStatuses())
|
|
|
|
{
|
|
|
|
System.out.println("----Old Server Status----> " + statusData.Address + ", " + statusData.Name);
|
|
|
|
killServer(statusData);
|
|
|
|
//restartServer(statusData);
|
|
|
|
}
|
|
|
|
|
|
|
|
List<DynamicServerData> dynamicServers = new ArrayList<DynamicServerData>(_repository.retrieveDynamicServers());
|
|
|
|
Collection<ServerGroupData> serverGroups = _repository.retrieveServerGroups();
|
|
|
|
HashMap<String, GroupStatusData> 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;
|
2013-11-08 19:52:56 +01:00
|
|
|
|
2014-01-08 21:18:52 +01:00
|
|
|
while (serversToAdd > 0)
|
|
|
|
{
|
|
|
|
int serverNum = groupStatus.getNextServerNumber();
|
|
|
|
Collections.sort(dynamicServers, new DynamicServerSorter());
|
|
|
|
DynamicServerData bestServer = getBestDynamicServer(dynamicServers, serverGroup);
|
2013-11-08 19:52:56 +01:00
|
|
|
|
2014-01-08 21:18:52 +01:00
|
|
|
if (bestServer == null)
|
2013-11-06 10:03:02 +01:00
|
|
|
{
|
2014-01-08 21:18:52 +01:00
|
|
|
System.out.println("No best dynamic server available for group " + serverGroup.Name);
|
|
|
|
break;
|
2013-11-06 10:03:02 +01:00
|
|
|
}
|
2014-01-08 21:18:52 +01:00
|
|
|
|
|
|
|
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--;
|
2013-11-06 10:03:02 +01:00
|
|
|
}
|
2013-10-10 18:02:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2014-01-08 21:18:52 +01:00
|
|
|
System.out.println("Natural sleep.");
|
|
|
|
Thread.sleep(5000);
|
2013-10-10 18:02:21 +02:00
|
|
|
}
|
|
|
|
catch (InterruptedException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-06 10:03:02 +01:00
|
|
|
|
|
|
|
private static DynamicServerData getBestDynamicServer(Collection<DynamicServerData> 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;
|
|
|
|
}
|
2013-11-08 19:52:56 +01:00
|
|
|
|
2014-01-03 07:37:39 +01:00
|
|
|
private static void restartServer(final ServerStatusData serverToKill)
|
|
|
|
{
|
|
|
|
String cmd = "/home/mineplex/restartServer.sh";
|
|
|
|
|
2014-01-08 21:18:52 +01:00
|
|
|
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);
|
2014-01-03 07:37:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void killServer(final ServerStatusData serverToKill)
|
2013-11-08 19:52:56 +01:00
|
|
|
{
|
|
|
|
String cmd = "/home/mineplex/easyRemoteKillServer.sh";
|
|
|
|
|
2014-01-08 21:18:52 +01:00
|
|
|
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);
|
2013-11-08 19:52:56 +01:00
|
|
|
}
|
|
|
|
|
2014-01-03 07:37:39 +01:00
|
|
|
private static void startServer(final DynamicServerData serverSpace, final ServerGroupData serverGroup, final int serverNum)
|
2013-11-08 19:52:56 +01:00
|
|
|
{
|
|
|
|
String cmd = "/home/mineplex/easyRemoteStartServer.sh";
|
|
|
|
|
2014-01-08 21:18:52 +01:00
|
|
|
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);
|
2013-11-08 19:52:56 +01:00
|
|
|
}
|
2013-10-10 18:02:21 +02:00
|
|
|
}
|