Updated ServerMonitor status reporting for network.

This commit is contained in:
Jonathan Williams 2015-09-05 01:53:37 -07:00
parent 2536d8a437
commit 4ad95b54cf

View File

@ -3,16 +3,19 @@ package mineplex.servermonitor;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator;
import java.util.List; import java.util.List;
import mineplex.serverdata.Region; import mineplex.serverdata.Region;
import mineplex.serverdata.data.BungeeServer;
import mineplex.serverdata.data.DataRepository;
import mineplex.serverdata.data.DedicatedServer; import mineplex.serverdata.data.DedicatedServer;
import mineplex.serverdata.data.ServerGroup; import mineplex.serverdata.data.ServerGroup;
import mineplex.servermonitor.data.BungeeStatusData; import mineplex.serverdata.redis.RedisDataRepository;
import mineplex.serverdata.servers.ServerManager;
public class StatusHistoryRepository public class StatusHistoryRepository
{ {
@ -24,8 +27,6 @@ public class StatusHistoryRepository
private static String CREATE_DEDICATED_TABLE = "CREATE TABLE IF NOT EXISTS DedicatedServerStats (id INT NOT NULL AUTO_INCREMENT, serverName VARCHAR(100), address VARCHAR(25), updated LONG, cpu TINYINT, ram MEDIUMINT, usedCpuPercent DOUBLE(4,2), usedRamPercent DOUBLE(4,2), US BOOLEAN NOT NULL DEFAULT '1', PRIMARY KEY (id));"; private static String CREATE_DEDICATED_TABLE = "CREATE TABLE IF NOT EXISTS DedicatedServerStats (id INT NOT NULL AUTO_INCREMENT, serverName VARCHAR(100), address VARCHAR(25), updated LONG, cpu TINYINT, ram MEDIUMINT, usedCpuPercent DOUBLE(4,2), usedRamPercent DOUBLE(4,2), US BOOLEAN NOT NULL DEFAULT '1', PRIMARY KEY (id));";
private static String CREATE_BUNGEE_TABLE = "CREATE TABLE IF NOT EXISTS BungeeStats (id INT NOT NULL AUTO_INCREMENT, address VARCHAR(25), updated LONG, players INT, maxPlayers INT, alive BOOLEAN NOT NULL, online BOOLEAN NOT NULL, US BOOLEAN NOT NULL DEFAULT '1', PRIMARY KEY (id));"; private static String CREATE_BUNGEE_TABLE = "CREATE TABLE IF NOT EXISTS BungeeStats (id INT NOT NULL AUTO_INCREMENT, address VARCHAR(25), updated LONG, players INT, maxPlayers INT, alive BOOLEAN NOT NULL, online BOOLEAN NOT NULL, US BOOLEAN NOT NULL DEFAULT '1', PRIMARY KEY (id));";
private static String CREATE_NETWORKSTATS_TABLE = "CREATE TABLE IF NOT EXISTS NetworkStats (id INT NOT NULL AUTO_INCREMENT, updated LONG, players INT, totalNetworkCpuUsage DOUBLE(4,2), totalNetworkRamUsage DOUBLE(4,2), totalCpu MEDIUMINT, totalRam MEDIUMINT, US BOOLEAN NOT NULL DEFAULT '1', PRIMARY KEY (id));"; private static String CREATE_NETWORKSTATS_TABLE = "CREATE TABLE IF NOT EXISTS NetworkStats (id INT NOT NULL AUTO_INCREMENT, updated LONG, players INT, totalNetworkCpuUsage DOUBLE(4,2), totalNetworkRamUsage DOUBLE(4,2), totalCpu MEDIUMINT, totalRam MEDIUMINT, US BOOLEAN NOT NULL DEFAULT '1', PRIMARY KEY (id));";
private static String RETRIEVE_BUNGEE_STATUSES = "SELECT bungeeStatus.address, bungeeStatus.players, bungeeStatus.maxPlayers, bungeeStatus.US, onlineStatus.online, now(), bungeeStatus.updated FROM BungeeServers AS bungeeStatus INNER JOIN bungeeOnlineStatus AS onlineStatus ON onlineStatus.address = bungeeStatus.address";
private static String INSERT_SERVERGROUP_STATS = "INSERT INTO ServerGroupStats (serverGroup, updated, players, maxPlayers, totalNetworkCpuUsage, totalNetworkRamUsage, totalCpu, totalRam, US) VALUES (?, now(), ?, ?, ?, ?, ?, ?, ?);"; private static String INSERT_SERVERGROUP_STATS = "INSERT INTO ServerGroupStats (serverGroup, updated, players, maxPlayers, totalNetworkCpuUsage, totalNetworkRamUsage, totalCpu, totalRam, US) VALUES (?, now(), ?, ?, ?, ?, ?, ?, ?);";
private static String INSERT_DEDICATEDSERVER_STATS = "INSERT INTO DedicatedServerStats (serverName, address, updated, cpu, ram, usedCpuPercent, usedRamPercent, US) VALUES (?, ?, now(), ?, ?, ?, ?, ?);"; private static String INSERT_DEDICATEDSERVER_STATS = "INSERT INTO DedicatedServerStats (serverName, address, updated, cpu, ram, usedCpuPercent, usedRamPercent, US) VALUES (?, ?, now(), ?, ?, ?, ?, ?);";
@ -33,7 +34,8 @@ public class StatusHistoryRepository
private static String INSERT_NETWORK_STATS = "INSERT INTO NetworkStats (updated, players, totalNetworkCpuUsage, totalNetworkRamUsage, totalCpu, totalRam, US) VALUES (now(), ?, ?, ?, ?, ?, ?);"; private static String INSERT_NETWORK_STATS = "INSERT INTO NetworkStats (updated, players, totalNetworkCpuUsage, totalNetworkRamUsage, totalCpu, totalRam, US) VALUES (now(), ?, ?, ?, ?, ?, ?);";
private static Connection _connection; private static Connection _connection;
private static Connection _bungeeconnection;
private static DataRepository<BungeeServer> _repository;
public StatusHistoryRepository() public StatusHistoryRepository()
{ {
@ -65,6 +67,9 @@ public class StatusHistoryRepository
preparedStatement = _connection.prepareStatement(CREATE_NETWORKSTATS_TABLE); preparedStatement = _connection.prepareStatement(CREATE_NETWORKSTATS_TABLE);
preparedStatement.execute(); preparedStatement.execute();
preparedStatement.close(); preparedStatement.close();
_repository = new RedisDataRepository<BungeeServer>(ServerManager.getConnection(true, ServerManager.SERVER_STATUS_LABEL), ServerManager.getConnection(false, ServerManager.SERVER_STATUS_LABEL),
Region.ALL, BungeeServer.class, "bungeeServers");
} }
catch (Exception exception) catch (Exception exception)
{ {
@ -185,58 +190,16 @@ public class StatusHistoryRepository
public void saveNetworkStats(double usedCpuPercent, double usedRamPercent, double availableCPU, double availableRAM, Region region) public void saveNetworkStats(double usedCpuPercent, double usedRamPercent, double availableCPU, double availableRAM, Region region)
{ {
/*
int totalPlayers = 0; int totalPlayers = 0;
List<BungeeStatusData> bungeeStatuses = new ArrayList<BungeeStatusData>(); List<BungeeServer> bungeeServers = new ArrayList<BungeeServer>(_repository.getElements());
PreparedStatement retrieveStatement = null;
try for (Iterator<BungeeServer> bungeeIterator = bungeeServers.iterator(); bungeeIterator.hasNext();)
{ {
if (_bungeeconnection == null || _bungeeconnection.isClosed()) BungeeServer server = bungeeIterator.next();
_bungeeconnection = DriverManager.getConnection(_bungeeConnectionString, _userName, _password);
retrieveStatement = _bungeeconnection.prepareStatement(RETRIEVE_BUNGEE_STATUSES); if (server.getPublicAddress().equalsIgnoreCase("127.0.0.1") || server.getPublicAddress().equalsIgnoreCase("0.0.0.0"))
ResultSet resultSet = retrieveStatement.executeQuery(); bungeeIterator.remove();
while (resultSet.next())
{
BungeeStatusData bungeeData = new BungeeStatusData();
bungeeData.Address = resultSet.getString(1);
bungeeData.Players = resultSet.getInt(2);
bungeeData.MaxPlayers = resultSet.getInt(3);
bungeeData.US = resultSet.getBoolean(4);
bungeeData.Online = resultSet.getBoolean(5);
long now = resultSet.getLong(6);
long updated = resultSet.getLong(7);
bungeeData.Alive = (now - updated) <= 15;
if (bungeeData.Alive && bungeeData.US == (region == Region.US))
totalPlayers += bungeeData.Players;
if (bungeeData.US == (region == Region.US))
bungeeStatuses.add(bungeeData);
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
if (retrieveStatement != null)
{
try
{
retrieveStatement.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
} }
PreparedStatement preparedStatement = null; PreparedStatement preparedStatement = null;
@ -248,15 +211,16 @@ public class StatusHistoryRepository
preparedStatement = _connection.prepareStatement(INSERT_BUNGEE_STATS); preparedStatement = _connection.prepareStatement(INSERT_BUNGEE_STATS);
for (BungeeStatusData bungeeStatusData : bungeeStatuses) for (BungeeServer bungeeStatusData : bungeeServers)
{ {
preparedStatement.setString(1, bungeeStatusData.Address); totalPlayers += bungeeStatusData.getPlayerCount();
preparedStatement.setInt(2, bungeeStatusData.Players); preparedStatement.setString(1, bungeeStatusData.getPublicAddress());
preparedStatement.setInt(3, bungeeStatusData.MaxPlayers); preparedStatement.setInt(2, bungeeStatusData.getPlayerCount());
preparedStatement.setBoolean(4, bungeeStatusData.Alive); preparedStatement.setInt(3, bungeeStatusData.getPlayerCount());
preparedStatement.setBoolean(5, bungeeStatusData.Online); preparedStatement.setBoolean(4, true);
preparedStatement.setBoolean(6, bungeeStatusData.US); preparedStatement.setBoolean(5, true);
preparedStatement.addBatch(); preparedStatement.setBoolean(6, bungeeStatusData.getRegion() == Region.US);
preparedStatement.addBatch();
} }
preparedStatement.executeBatch(); preparedStatement.executeBatch();
@ -315,6 +279,5 @@ public class StatusHistoryRepository
} }
} }
} }
*/
} }
} }