Add command for printing live servers through monitor

This commit is contained in:
Kenny Goodin 2017-11-25 20:31:58 -05:00 committed by Alexander Meech
parent b5ea6f1240
commit 4b552f0317
2 changed files with 258 additions and 159 deletions

View File

@ -13,6 +13,11 @@
<name>ServerMonitor</name> <name>ServerMonitor</name>
<artifactId>mineplex-servermonitor</artifactId> <artifactId>mineplex-servermonitor</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.jline>2.12</version.jline>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>${project.groupId}</groupId> <groupId>${project.groupId}</groupId>
@ -28,6 +33,12 @@
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId> <artifactId>httpclient</artifactId>
</dependency> </dependency>
<dependency>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
<version>${version.jline}</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -16,11 +16,15 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Optional;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.logging.FileHandler; import java.util.logging.FileHandler;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.craftbukkit.libs.com.google.common.base.Throwables;
import org.fusesource.jansi.AnsiConsole;
import mineplex.core.common.util.NautHashMap; import mineplex.core.common.util.NautHashMap;
import mineplex.serverdata.Region; import mineplex.serverdata.Region;
import mineplex.serverdata.commands.RestartCommand; import mineplex.serverdata.commands.RestartCommand;
@ -32,9 +36,11 @@ import mineplex.serverdata.servers.DedicatedServerSorter;
import mineplex.serverdata.servers.ServerManager; import mineplex.serverdata.servers.ServerManager;
import mineplex.serverdata.servers.ServerRepository; import mineplex.serverdata.servers.ServerRepository;
import jline.console.ConsoleReader;
public class ServerMonitor public class ServerMonitor
{ {
private static ServerRepository _repository = null; private static volatile ServerRepository _repository = null;
private static StatusHistoryRepository _historyRepository = null; private static StatusHistoryRepository _historyRepository = null;
private static int _count = 0; private static int _count = 0;
private static HashSet<ProcessRunner> _processes = new HashSet<ProcessRunner>(); private static HashSet<ProcessRunner> _processes = new HashSet<ProcessRunner>();
@ -42,13 +48,13 @@ public class ServerMonitor
private static Collection<MinecraftServer> _serverStatuses = null; private static Collection<MinecraftServer> _serverStatuses = null;
private static Collection<ServerGroup> _serverGroups = null; private static Collection<ServerGroup> _serverGroups = null;
private static Map<String, ServerGroup> _serverGroupMap = null; private static Map<String, ServerGroup> _serverGroupMap = null;
private static List<DedicatedServer> _dedicatedServers = null; private static volatile List<DedicatedServer> _dedicatedServers = null;
private static HashSet<String> _deadServers = new HashSet<String>(); private static HashSet<String> _deadServers = new HashSet<String>();
private static HashSet<String> _delayedKill = new HashSet<String>(); private static HashSet<String> _delayedKill = new HashSet<String>();
private static HashSet<String> _laggyServers = new HashSet<String>(); private static HashSet<String> _laggyServers = new HashSet<String>();
private static SimpleDateFormat _dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); private static volatile SimpleDateFormat _dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
private static Logger _logger = Logger.getLogger("ServerMonitor"); private static volatile Logger _logger = Logger.getLogger("ServerMonitor");
private static Timer _timer = new Timer(); private static Timer _timer = new Timer();
private static int _totalPlayers = 0; private static int _totalPlayers = 0;
@ -100,6 +106,50 @@ public class ServerMonitor
e1.printStackTrace(); e1.printStackTrace();
} }
new Thread(new Runnable()
{
@Override
public void run()
{
try
{
AnsiConsole.systemInstall();
ConsoleReader reader = new ConsoleReader();
reader.setExpandEvents(false);
String command;
while ((command = reader.readLine(">")) != null)
{
if (command.contains("printservers"))
{
String[] parts = command.split(" ");
String serverGroup = "";
if (parts.length > 1)
{
serverGroup = parts[1];
}
printServersCommand(serverGroup);
}
else if (command.equals("help"))
{
log("Commands:");
log("printservers (server-group) - prints all servers if no server group is"
+ "supplied, or all servers of a given group if a server group is supplied.");
}
else
{
log("No command " + command + " found.");
}
}
}
catch (Exception e)
{
log(Throwables.getStackTraceAsString(e));
}
}
}).start();
HashMap<String, Entry<String, Long>> serverTracker = new HashMap<String, Entry<String, Long>>(); HashMap<String, Entry<String, Long>> serverTracker = new HashMap<String, Entry<String, Long>>();
while (true) while (true)
@ -110,7 +160,7 @@ public class ServerMonitor
_serverStatuses = _repository.getServerStatuses(); _serverStatuses = _repository.getServerStatuses();
_serverGroups = _repository.getServerGroups(_serverStatuses); _serverGroups = _repository.getServerGroups(_serverStatuses);
_serverGroupMap = new HashMap<String, ServerGroup>(); _serverGroupMap = new HashMap<String, ServerGroup>();
_dedicatedServers = new ArrayList<DedicatedServer>(_repository.getDedicatedServers()); _dedicatedServers = Collections.synchronizedList(new ArrayList<>(_repository.getDedicatedServers()));
calculateTotalPlayers(); calculateTotalPlayers();
killDeadServers(); killDeadServers();
@ -432,7 +482,8 @@ public class ServerMonitor
_timer.schedule(new TimerTask() _timer.schedule(new TimerTask()
{ {
public void run() @Override
public void run()
{ {
_deadServers.add(deadServer.getName()); _deadServers.add(deadServer.getName());
_delayedKill.remove(deadServer.getName()); _delayedKill.remove(deadServer.getName());
@ -606,7 +657,8 @@ public class ServerMonitor
ProcessRunner pr = new ProcessRunner(new String[] {"/bin/sh", cmd, serverAddress, serverName}); ProcessRunner pr = new ProcessRunner(new String[] {"/bin/sh", cmd, serverAddress, serverName});
pr.start(new GenericRunnable<Boolean>() pr.start(new GenericRunnable<Boolean>()
{ {
public void run(Boolean error) @Override
public void run(Boolean error)
{ {
MinecraftServer server = null; MinecraftServer server = null;
@ -717,7 +769,8 @@ public class ServerMonitor
ProcessRunner pr = new ProcessRunner(new String[] {"/bin/sh", cmd, server.getPublicAddress(), server.getName() }); ProcessRunner pr = new ProcessRunner(new String[] {"/bin/sh", cmd, server.getPublicAddress(), server.getName() });
pr.start(new GenericRunnable<Boolean>() pr.start(new GenericRunnable<Boolean>()
{ {
public void run(Boolean error) @Override
public void run(Boolean error)
{ {
if (error) if (error)
log("[TIMING START] Errored " + server.getName() + "(" + server.getPublicAddress() + ")"); log("[TIMING START] Errored " + server.getName() + "(" + server.getPublicAddress() + ")");
@ -750,7 +803,8 @@ public class ServerMonitor
ProcessRunner pr = new ProcessRunner(new String[] {"/bin/sh", cmd, server.getPublicAddress(), server.getName() }); ProcessRunner pr = new ProcessRunner(new String[] {"/bin/sh", cmd, server.getPublicAddress(), server.getName() });
pr.start(new GenericRunnable<Boolean>() pr.start(new GenericRunnable<Boolean>()
{ {
public void run(Boolean error) @Override
public void run(Boolean error)
{ {
if (error) if (error)
log("[TIMING PASTE] Errored " + server.getName() + "(" + server.getPublicAddress() + ")"); log("[TIMING PASTE] Errored " + server.getName() + "(" + server.getPublicAddress() + ")");
@ -783,7 +837,8 @@ public class ServerMonitor
ProcessRunner pr = new ProcessRunner(new String[] {"/bin/sh", cmd, server.getPublicAddress(), server.getName() }); ProcessRunner pr = new ProcessRunner(new String[] {"/bin/sh", cmd, server.getPublicAddress(), server.getName() });
pr.start(new GenericRunnable<Boolean>() pr.start(new GenericRunnable<Boolean>()
{ {
public void run(Boolean error) @Override
public void run(Boolean error)
{ {
if (error) if (error)
log("[COPY LOG] Errored " + server.getName() + "(" + server.getPublicAddress() + ")"); log("[COPY LOG] Errored " + server.getName() + "(" + server.getPublicAddress() + ")");
@ -819,7 +874,8 @@ public class ServerMonitor
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() + "", serverGroup.getAddWorldEdit() + "" }); 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() + "", serverGroup.getAddWorldEdit() + "" });
pr.start(new GenericRunnable<Boolean>() pr.start(new GenericRunnable<Boolean>()
{ {
public void run(Boolean error) @Override
public void run(Boolean error)
{ {
if (error) if (error)
log("[" + serverName + ":" + serverAddress + " Free Resources; CPU " + serverSpace.getAvailableCpu() + " RAM " + serverSpace.getAvailableRam() + "MB] Errored " + serverName + "(" + groupPrefix+ "-" + serverNum + (free ? "-FREE" : "") + ")"); log("[" + serverName + ":" + serverAddress + " Free Resources; CPU " + serverSpace.getAvailableCpu() + " RAM " + serverSpace.getAvailableRam() + "MB] Errored " + serverName + "(" + groupPrefix+ "-" + serverNum + (free ? "-FREE" : "") + ")");
@ -844,6 +900,38 @@ public class ServerMonitor
_processes.add(pr); _processes.add(pr);
} }
private static void printServersCommand(String serverGroup)
{
// grab servers
Collection<MinecraftServer> servers = _repository.getServerStatuses();
// remove entries not within the target server group, if one is supplied
if (!serverGroup.isEmpty())
{
servers.removeIf(s -> !s.getGroup().equalsIgnoreCase(serverGroup));
}
List<MinecraftServer> serverList = new ArrayList<>(servers);
Collections.sort(serverList,
(a, b) -> Integer.compare(b.getPlayerCount(), a.getPlayerCount()));
serverList.forEach(server ->
{
Optional<DedicatedServer> opt = getDediStatusFromAddress(server.getPublicAddress());
String dediName = opt.map(dedi -> dedi.getName()).orElse("???");
String fmt = "%s: %s (%s players online), open slots: %s, free ram: %sMB";
log(String.format(fmt, server.getName(), dediName, server.getPlayerCount(),
server.getMaxPlayerCount() - server.getPlayerCount(), server.getRam()));
});
}
private static Optional<DedicatedServer> getDediStatusFromAddress(String address)
{
return _dedicatedServers.stream().filter(dedi -> dedi.getPrivateAddress().equals(address))
.findFirst();
}
private static boolean ignoreServer(String serverGroupName) private static boolean ignoreServer(String serverGroupName)
{ {
return serverGroupName.equalsIgnoreCase("Testing") || serverGroupName.equalsIgnoreCase("Clans"); return serverGroupName.equalsIgnoreCase("Testing") || serverGroupName.equalsIgnoreCase("Clans");