From 977fa82d559f7a3d48684f59aa1af3d13cfad397 Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Sat, 4 May 2013 11:33:20 -0700 Subject: [PATCH] Status monitor for minecraft server --- minecraft/index.cgi | 7 +--- minecraft/lang/en | 8 +++++ minecraft/minecraft-lib.pl | 22 +++++++++--- minecraft/status_monitor.pl | 68 +++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 10 deletions(-) create mode 100755 minecraft/status_monitor.pl diff --git a/minecraft/index.cgi b/minecraft/index.cgi index 492327e49..3a79c3aac 100755 --- a/minecraft/index.cgi +++ b/minecraft/index.cgi @@ -38,12 +38,7 @@ elsif ($err) { } # Check if new version is out, if we haven't checked in the last 6 hours -if (time() - $config{'last_check'} > 6*60*60) { - my $sz = &check_server_download_size(); - $config{'last_check'} = time(); - $config{'last_size'} = $sz; - &save_module_config(); - } +&update_last_check(); if ($config{'last_size'}) { my $jar = $config{'minecraft_jar'} || $config{'minecraft_dir'}."/"."minecraft_server.jar"; diff --git a/minecraft/lang/en b/minecraft/lang/en index d4d56717b..246468e4d 100644 --- a/minecraft/lang/en +++ b/minecraft/lang/en @@ -293,3 +293,11 @@ log_atboot=Enabled server at boot time log_delboot=Disabled server at boot time log_enable_backup=Enabled scheduled backups to $1 log_disable_backup=Disabled scheduled backups + +monitor_up=Minecraft Server Alive +monitor_latest=Minecraft Server Up To Date +monitor_down=Server process is down +monitor_noreply=Server is not responding +monitor_notype=Unknown monitor type! +monitor_newversion=New version is available +monitor_nojar=JAR file $1 does not exist diff --git a/minecraft/minecraft-lib.pl b/minecraft/minecraft-lib.pl index 4660f9f7e..31ff90a6d 100644 --- a/minecraft/minecraft-lib.pl +++ b/minecraft/minecraft-lib.pl @@ -244,18 +244,19 @@ if (!$nolog) { } } -# execute_minecraft_command(command, [no-log]) +# execute_minecraft_command(command, [no-log], [wait-time]) # Run a command, and return output from the server log sub execute_minecraft_command { -my ($cmd, $nolog) = @_; +my ($cmd, $nolog, $wait) = @_; +$wait ||= 100; my $logfile = $config{'minecraft_dir'}."/server.log"; my $fh = "LOG"; &open_readfile($fh, $logfile); seek($fh, 0, 2); my $pos = tell($fh); &send_server_command($cmd, $nolog); -for(my $i=0; $i<100; $i++) { +for(my $i=0; $i<$wait; $i++) { select(undef, undef, undef, 0.1); my @st = stat($logfile); last if ($st[7] > $pos); @@ -645,7 +646,7 @@ my $dir = strftime($config{'backup_dir'}, @tm); # Create destination dir if (!-d $dir) { - if (!&make_dir($dir, 0700)) { + if (!&make_dir($dir, 0755)) { &send_backup_email( "Failed to create destination directory $dir : $!"); return; @@ -676,6 +677,7 @@ foreach my $w (@worlds) { "cd ".quotemeta($config{'minecraft_dir'})." && ". "zip -r ".quotemeta($file)." ".quotemeta($w->{'name'})); my $ex = $?; + &set_ownership_permissions(undef, undef, 0755, $file); if ($w->{'name'} eq $def && &is_minecraft_server_running()) { # Re-enable world writes @@ -741,4 +743,16 @@ for(my $i=0; $i<@xpmap; $i+=2) { return undef; } +# update_last_check() +# If the last check time is too old, check for the latest version +sub update_last_check +{ +if (time() - $config{'last_check'} > 6*60*60) { + my $sz = &check_server_download_size(); + $config{'last_check'} = time(); + $config{'last_size'} = $sz; + &save_module_config(); + } +} + 1; diff --git a/minecraft/status_monitor.pl b/minecraft/status_monitor.pl new file mode 100755 index 000000000..3658ea83b --- /dev/null +++ b/minecraft/status_monitor.pl @@ -0,0 +1,68 @@ + +do 'minecraft-lib.pl'; + +# status_monitor_list() +# Return a list of supported monitor types +sub status_monitor_list +{ +return ( [ "minecraft_up", $text{'monitor_up'} ], + [ "minecraft_latest", $text{'monitor_latest'} ] ); +} + +# status_monitor_status(type, &monitor, from-ui) +# Check the drive status +sub status_monitor_status +{ +my ($type, $monitor, $fromui) = @_; +if ($type eq "minecraft_up") { + # Check if server is running and can reply to commands + if (!&is_minecraft_server_running()) { + return { 'up' => 0, + 'desc' => $text{'monitor_down'} }; + } + my $out = &execute_minecraft_command("/seed", 0, 5); + if ($out !~ /^\d+/) { + return { 'up' => 0, + 'desc' => $text{'monitor_noreply'} }; + } + return { 'up' => 1 }; + } +elsif ($type eq "minecraft_latest") { + # Compare version with latest available to download + &update_last_check(); + if ($config{'last_size'}) { + my $jar = $config{'minecraft_jar'} || + $config{'minecraft_dir'}."/"."minecraft_server.jar"; + my @st = stat($jar); + if (@st && $st[7] != $config{'last_size'}) { + return { 'up' => 0, + 'desc' => $text{'monitor_newversion'} }; + } + elsif (!@st) { + return { 'up' => -1, + 'desc' => &text('monitor_nojar', $jar) }; + } + } + return { 'up' => 1 }; + } +else { + return { 'up' => -1, + 'desc' => $text{'monitor_notype'} }; + } +} + +# status_monitor_dialog(type, &monitor) +# Return form for selecting a drive +sub status_monitor_dialog +{ +return undef; +} + +# status_monitor_parse(type, &monitor, &in) +# Parse form for selecting a rule +sub status_monitor_parse +{ +} + +1; +