2013-10-10 18:02:21 +02:00
package mineplex.servermonitor ;
import java.io.BufferedReader ;
import java.io.InputStreamReader ;
2013-11-05 19:19:04 +01:00
import java.util.ArrayList ;
import java.util.Collection ;
2013-11-06 10:03:02 +01:00
import java.util.Collections ;
2013-11-05 19:19:04 +01:00
import java.util.HashMap ;
2013-11-02 08:56:31 +01:00
import java.util.List ;
2013-10-10 18:02:21 +02:00
public class ServerMonitor
{
private static Repository _repository = new Repository ( ) ;
2013-10-15 23:19:14 +02:00
private static int _count = 0 ;
2013-10-10 18:02:21 +02:00
public static void main ( String args [ ] )
{
_repository . initialize ( ) ;
while ( true )
{
2013-11-08 19:52:56 +01:00
if ( _count % 10 = = 0 )
2013-10-10 18:02:21 +02:00
{
2013-10-15 23:19:14 +02:00
for ( ServerStatusData statusData : _repository . retrieveOldServerStatuses ( ) )
{
2013-11-14 06:01:41 +01:00
System . out . println ( " ----Old Server Status----> " + statusData . Address + " , " + statusData . Name ) ;
2013-11-08 19:52:56 +01:00
killServer ( statusData ) ;
2013-10-15 23:19:14 +02:00
}
2013-11-08 19:52:56 +01:00
2013-11-06 10:03:02 +01:00
List < DynamicServerData > dynamicServers = new ArrayList < DynamicServerData > ( _repository . retrieveDynamicServers ( ) ) ;
Collection < ServerGroupData > serverGroups = _repository . retrieveServerGroups ( ) ;
HashMap < String , GroupStatusData > groupStatusList = _repository . retrieveGroupStatusData ( ) ;
2013-11-02 08:56:31 +01:00
2013-11-06 10:03:02 +01:00
for ( ServerGroupData serverGroup : serverGroups )
{
if ( ! groupStatusList . containsKey ( serverGroup . Name ) )
{
groupStatusList . put ( serverGroup . Name , new GroupStatusData ( ) ) ;
}
GroupStatusData groupStatus = groupStatusList . get ( serverGroup . Name ) ;
2013-11-08 19:52:56 +01:00
int serversToAdd = Math . max ( serverGroup . RequiredTotalServers - groupStatus . getTotalServers ( ) , serverGroup . RequiredJoinableServers - groupStatus . getJoinableCount ( ) ) ;
int serversToKill = ( groupStatus . getTotalServers ( ) > serverGroup . RequiredTotalServers & & groupStatus . getJoinableCount ( ) > serverGroup . RequiredJoinableServers ) ? Math . min ( groupStatus . getJoinableCount ( ) - serverGroup . RequiredJoinableServers , groupStatus . EmptyServers . size ( ) ) : 0 ;
while ( serversToAdd > 0 )
2013-11-06 10:03:02 +01:00
{
2013-11-08 19:52:56 +01:00
int serverNum = groupStatus . getNextServerNumber ( ) ;
Collections . sort ( dynamicServers , new DynamicServerSorter ( ) ) ;
DynamicServerData bestServer = getBestDynamicServer ( dynamicServers , serverGroup ) ;
if ( bestServer = = null )
2013-11-06 10:03:02 +01:00
{
2013-11-08 19:52:56 +01:00
System . out . println ( " No best dynamic server available for group " + serverGroup . Name ) ;
break ;
2013-11-06 10:03:02 +01:00
}
2013-11-08 19:52:56 +01:00
System . out . println ( " Adding server for Server Group " + serverGroup . Name + " Req Total: " + serverGroup . RequiredTotalServers + " Req Joinable: " + serverGroup . RequiredJoinableServers + " | Actual Total: " + groupStatus . getTotalServers ( ) + " Actual Joinable: " + groupStatus . getJoinableCount ( ) ) ;
startServer ( bestServer , serverGroup , serverNum ) ;
serversToAdd - - ;
2013-11-06 10:03:02 +01:00
}
2013-11-08 19:52:56 +01:00
while ( serversToKill > 0 )
2013-11-06 10:03:02 +01:00
{
2013-11-08 19:52:56 +01:00
System . out . println ( " Killing excess server for Server Group " + serverGroup . Name + " Req Total: " + serverGroup . RequiredTotalServers + " Req Joinable: " + serverGroup . RequiredJoinableServers + " | Actual Total: " + groupStatus . getTotalServers ( ) + " Actual Joinable: " + groupStatus . getJoinableCount ( ) ) ;
killServer ( groupStatus . EmptyServers . get ( 0 ) ) ;
serversToKill - - ;
2013-11-06 10:03:02 +01:00
}
}
2013-10-10 18:02:21 +02:00
}
try
{
2013-10-15 23:19:14 +02:00
Thread . sleep ( 1000 ) ;
2013-10-10 18:02:21 +02:00
}
catch ( InterruptedException e )
{
e . printStackTrace ( ) ;
}
2013-10-15 23:19:14 +02:00
_count + + ;
2013-11-08 19:52:56 +01:00
_count % = 10000 ;
2013-10-10 18:02:21 +02:00
}
}
2013-11-06 10:03:02 +01:00
private static DynamicServerData getBestDynamicServer ( Collection < DynamicServerData > dynamicServers , ServerGroupData serverGroup )
{
DynamicServerData bestServer = null ;
for ( DynamicServerData serverData : dynamicServers )
{
2013-11-08 23:46:05 +01:00
System . out . println ( " Checking " + serverData . Name + " Cpu: " + serverData . AvailableCPU + " Ram: " + serverData . AvailableRAM ) ;
2013-11-06 10:03:02 +01:00
if ( serverData . AvailableRAM > serverGroup . RequiredRAM & & serverData . AvailableCPU > serverGroup . RequiredCPU )
{
if ( bestServer = = null )
{
bestServer = serverData ;
if ( ! serverData . ServerGroupCount . containsKey ( serverGroup . Name ) )
break ;
}
else if ( serverData . ServerGroupCount . containsKey ( serverGroup . Name ) )
{
if ( serverData . ServerGroupCount . get ( serverGroup . Name ) < bestServer . ServerGroupCount . get ( serverGroup . Name ) )
bestServer = serverData ;
}
}
}
return bestServer ;
}
2013-11-08 19:52:56 +01:00
private static void killServer ( ServerStatusData serverToKill )
{
String cmd = " /home/mineplex/easyRemoteKillServer.sh " ;
Process process = null ;
try
{
2013-11-08 23:46:05 +01:00
process = new ProcessBuilder ( new String [ ] { " /bin/sh " , cmd , serverToKill . Address , serverToKill . Name } ) . start ( ) ;
2013-11-08 19:52:56 +01:00
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 )
2013-11-14 06:01:41 +01:00
{
2013-11-08 19:52:56 +01:00
e . printStackTrace ( ) ;
}
finally
{
if ( process ! = null )
{
process . destroy ( ) ;
}
}
_repository . removeServerRecord ( serverToKill ) ;
System . out . println ( " Sent kill command to " + serverToKill . Address + " for " + serverToKill . Name ) ;
}
private static void startServer ( DynamicServerData serverSpace , ServerGroupData serverGroup , int serverNum )
{
String cmd = " /home/mineplex/easyRemoteStartServer.sh " ;
Process process = null ;
try
{
process = new ProcessBuilder ( new String [ ] { " /bin/sh " , cmd , serverSpace . Address , serverGroup . ScriptName , serverGroup . Prefix + " - " + serverNum , " 1 " , serverSpace . US ? " us " : " eu " } ) . 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 ( ) ;
}
}
serverSpace . setServerGroupCount ( serverGroup , serverSpace . ServerGroupCount . containsKey ( serverGroup . Name ) ? ( serverSpace . ServerGroupCount . get ( serverGroup . Name ) + 1 ) : 1 ) ;
2013-11-08 23:46:05 +01:00
System . out . println ( " Sent start command ( " + serverSpace . Address + " , " + serverGroup . ScriptName + " , " + serverGroup . Prefix + " - " + serverNum + " , 1, " + ( serverSpace . US ? " us " : " eu " ) + " ) " ) ;
2013-11-08 19:52:56 +01:00
}
2013-10-10 18:02:21 +02:00
}