Added ServerMonitor
This commit is contained in:
parent
fc18d0fbb1
commit
923a066013
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
|
@ -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
|
|
@ -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<ServerStatusData> retrieveServerStatuses()
|
||||
{
|
||||
Connection connection = null;
|
||||
ResultSet resultSet = null;
|
||||
List<ServerStatusData> serverData = new ArrayList<ServerStatusData>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue