Added file logger to ServerMonitor.

Fixed Statifier pulling inactive bungee records.
This commit is contained in:
Jonathan Williams 2014-07-29 21:10:52 -07:00
parent 9c952a7b04
commit ff5bc3f485
3 changed files with 58 additions and 3 deletions

View File

@ -0,0 +1,13 @@
package mineplex.servermonitor;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
public class CustomFormatter extends Formatter
{
@Override
public String format(LogRecord record)
{
return record.getMessage() + "\n";
}
}

View File

@ -2,16 +2,21 @@ package mineplex.servermonitor;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
public class ServerMonitor
{
@ -21,8 +26,37 @@ public class ServerMonitor
private static HashSet<ProcessRunner> _processes = new HashSet<ProcessRunner>();
private static HashMap<String, Boolean> _badServers = new HashMap<String, Boolean>();
private static SimpleDateFormat _dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
private static Logger _logger = Logger.getLogger("ServerMonitor");
public static void main (String args[])
{
File logFile = new File("monitor.log");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
try
{
FileHandler fileHandler = new FileHandler("monitor.log");
fileHandler.setFormatter(new CustomFormatter());
_logger.addHandler(fileHandler);
_logger.setUseParentHandlers(false);
}
catch (SecurityException | IOException e1)
{
e1.printStackTrace();
}
_us = !new File("eu.dat").exists();
_repository.initialize(_us);
@ -82,11 +116,10 @@ public class ServerMonitor
if (isServerOffline(serverData))
{
System.out.println("------=[OFFLINE]=------=[" + serverData.Name + ":" + serverData.Address + "]=------=[OFFLINE]=------");
log(serverData.Name + ":" + serverData.Address + " is OFFLINE.");
_badServers.put(serverData.Name, true);
}
}
System.out.println(_badServers.size() + " bad servers.");
}
for (Iterator<DynamicServerData> iterator = dynamicServers.iterator(); iterator.hasNext();)
@ -102,6 +135,7 @@ public class ServerMonitor
for (ServerStatusData serverToKill : groupStatus.KillServers)
{
System.out.println("----DUPLICATE SERVER----> " + serverToKill.Address + ", " + serverToKill.Name);
log("Duplicate server " + serverToKill.Name + ":" + serverToKill.Address + " is being killed.");
killServer(serverToKill);
}
@ -124,6 +158,8 @@ public class ServerMonitor
serverToKill.Name = entry.getKey();
serverToKill.Address = entry.getValue().getKey();
log("Server startup too slow " + serverToKill.Name + ":" + serverToKill.Address + " is being killed.");
killServer(serverToKill);
iterator.remove();
}
@ -174,6 +210,7 @@ public class ServerMonitor
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());
log("Excess server " + groupStatus.EmptyServers.get(0).Name + ":" + groupStatus.EmptyServers.get(0).Address + " is being killed.");
killServer(groupStatus.EmptyServers.remove(0));
serversToKill--;
}
@ -406,4 +443,9 @@ public class ServerMonitor
if (!pr.isDone())
_processes.add(pr);
}
private static void log(String message)
{
_logger.info("[" + _dateFormat.format(new Date()) + "] " + message);
}
}

View File

@ -23,7 +23,7 @@ public class StatusSnapshotRepository
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_SERVERGROUP_STATUSES = "SELECT ServerStatus.serverName, DynamicServers.US, 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";
private static String RETRIEVE_BUNGEE_STATUSES = "SELECT address, US, players, maxPlayers FROM BungeeServers";
private static String RETRIEVE_BUNGEE_STATUSES = "SELECT address, US, players, maxPlayers FROM BungeeServers WHERE TIME_TO_SEC(TIMEDIFF(now(), updated)) < 15";
public static Connection serverStatusConnection;
public static Connection bungeeConnection;