diff --git a/Plugins/Mineplex.ServerMonitor/.classpath b/Plugins/Mineplex.ServerMonitor/.classpath new file mode 100644 index 000000000..fb5011632 --- /dev/null +++ b/Plugins/Mineplex.ServerMonitor/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/Plugins/Mineplex.ServerMonitor/.settings/org.eclipse.jdt.core.prefs b/Plugins/Mineplex.ServerMonitor/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..7341ab168 --- /dev/null +++ b/Plugins/Mineplex.ServerMonitor/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/Repository.java b/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/Repository.java new file mode 100644 index 000000000..d2c5aa09e --- /dev/null +++ b/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/Repository.java @@ -0,0 +1,117 @@ +package mineplex.servermonitor; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class Repository +{ + private String _connectionString = "jdbc:mysql://localhost:3306/ServerStatus"; + private String _userName = "root"; + private String _password = "y2D4atu3Pene2asw"; + + private static String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS ServerStatus (id INT NOT NULL AUTO_INCREMENT, serverName VARCHAR(256), serverGroup VARCHAR(256), address VARCHAR(256), updated LONG, motd VARCHAR(256), players INT, maxPlayers INT, tps INT, ram INT, maxRam INT, PRIMARY KEY (id));"; + private static String RETRIEVE_SERVER_STATUSES = "SELECT serverName, address, motd, players, maxPlayers FROM ServerStatus WHERE TIME_TO_SEC(TIMEDIFF(now(), ServerStatus.updated)) > 10;"; + + public void initialize() + { + + Connection connection = null; + + try + { + Class.forName("com.mysql.jdbc.Driver"); + + connection = DriverManager.getConnection(_connectionString, _userName, _password); + + // Create table + PreparedStatement preparedStatement = connection.prepareStatement(CREATE_TABLE); + preparedStatement.execute(); + } + catch (Exception exception) + { + exception.printStackTrace(); + } + finally + { + if (connection != null) + { + try + { + connection.close(); + } + catch (SQLException e) + { + e.printStackTrace(); + } + } + } + } + + public List retrieveServerStatuses() + { + Connection connection = null; + ResultSet resultSet = null; + List serverData = new ArrayList(); + + try + { + connection = DriverManager.getConnection(_connectionString, _userName, _password); + + PreparedStatement preparedStatement = connection.prepareStatement(RETRIEVE_SERVER_STATUSES); + resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) + { + ServerStatusData serverStatusData = new ServerStatusData(); + + serverStatusData.Name = resultSet.getString(1); + + String addressPortString = resultSet.getString(2); + serverStatusData.Address = addressPortString.split(":")[0]; + serverStatusData.Port = Integer.parseInt(addressPortString.split(":")[1]); + serverStatusData.Motd = resultSet.getString(3); + serverStatusData.Players = resultSet.getInt(4); + serverStatusData.MaxPlayers = resultSet.getInt(5); + + serverData.add(serverStatusData); + } + } + catch (Exception exception) + { + exception.printStackTrace(); + } + finally + { + if (resultSet != null) + { + try + { + resultSet.close(); + } + catch (SQLException e) + { + e.printStackTrace(); + } + } + + if (connection != null) + { + try + { + connection.close(); + } + catch (SQLException e) + { + e.printStackTrace(); + } + } + } + + return serverData; + } +} diff --git a/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/ServerMonitor.java b/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/ServerMonitor.java new file mode 100644 index 000000000..1c5e83227 --- /dev/null +++ b/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/ServerMonitor.java @@ -0,0 +1,61 @@ +package mineplex.servermonitor; + +import java.io.BufferedReader; +import java.io.InputStreamReader; + +public class ServerMonitor +{ + private static Repository _repository = new Repository(); + + public static void main (String args[]) + { + _repository.initialize(); + + while (true) + { + for (ServerStatusData statusData : _repository.retrieveServerStatuses()) + { + String key = statusData.Address + " " + statusData.Name; + + String cmd = "/home/mineplex/restartServer.sh"; + Process process = null; + + try + { + process = new ProcessBuilder(new String[] {"/bin/sh", "-x", cmd, statusData.Address, statusData.Name}).start(); + process.waitFor(); + BufferedReader reader=new BufferedReader(new InputStreamReader(process.getInputStream())); + String line = reader.readLine(); + + while(line != null) + { + System.out.println(line); + line=reader.readLine(); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + finally + { + if (process != null) + { + process.destroy(); + } + } + + System.out.println("Sent restart command to " + key + ""); + } + + try + { + Thread.sleep(5000); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + } + } +} diff --git a/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/ServerStatusData.java b/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/ServerStatusData.java new file mode 100644 index 000000000..425525f6a --- /dev/null +++ b/Plugins/Mineplex.ServerMonitor/src/mineplex/servermonitor/ServerStatusData.java @@ -0,0 +1,11 @@ +package mineplex.servermonitor; + +public class ServerStatusData +{ + public String Name; + public String Motd; + public int Players; + public int MaxPlayers; + public String Address; + public int Port; +}